import type { Awaitable } from "@vueuse/core";

export class Mock {
  private mocks: Map<string, () => Awaitable<any>>;
  constructor() {
    this.mocks = new Map();
  }

  get keys(): string[] {
    return Array.from(this.mocks.keys());
  }

  register(key: string, data: () => Awaitable<any>) {
    this.mocks.set(key, data);
  }

  unregister(key: string) {
    this.mocks.delete(key);
  }

  get(key: string): (() => Awaitable<any>) | undefined {
    return this.mocks.get(key);
  }
}

export function useMocks() {
  const singletonKey = "__mocks_singleton__";
  if (!(globalThis as any)[singletonKey]) {
    (globalThis as any)[singletonKey] = new Mock();
  }
  return (globalThis as any)[singletonKey] as Mock;
}

export function setupMocks() {
  return new Promise<void>((resolve) => {
    if (import.meta.env.DEV) {
      import("./data");
    }
    resolve();
  });
}
