import { IRepositoryManager, RepositoryManager } from "../domain";
import { ISequelizeAdapter, createSequelizeAdapter } from "./sequelize";

export interface IContext {
  adapter: ISequelizeAdapter;
  repositoryManager: IRepositoryManager;
}

export class ContextFactory {
  private static instance: ContextFactory | null = null;

  public static getInstance(): IContext {
    if (!ContextFactory.instance) {
      ContextFactory.instance = new ContextFactory({
        adapter: createSequelizeAdapter(),
        repositoryManager: RepositoryManager.getInstance(),
      });
    }

    return ContextFactory.instance.context;
  }

  private context: IContext;

  private constructor(context: IContext) {
    this.context = context;
  }
}

// ContextFactory.ts
/*export interface IContext {
  adapter: ISequelizeAdapter;
  repositoryManager: IRepositoryManager;
  services: T;
}

export class ContextFactory {
  private static instances: Map<string, ContextFactory<any>> = new Map();

  public static getInstance(constructor: new () => T): ContextFactory {
    const key = constructor.name;
    if (!ContextFactory.instances.has(key)) {
      ContextFactory.instances.set(key, new ContextFactory(constructor));
    }

    return ContextFactory.instances.get(key)! as ContextFactory;
  }

  private context: IContext;

  private constructor(constructor: new () => T) {
    this.context = {
      adapter: createSequelizeAdapter(),
      repositoryManager: RepositoryManager.getInstance(),
      services: new constructor(),
    };
  }

  public getContext(): IContext {
    return this.context;
  }
}*/
