import { Effect, Layer } from "effect";

import { ExtensionContext } from "../platform/Storage.ts";
import { Uri } from "./TestVsCode.ts";

export class Memento {
  #map = new Map<string, unknown>();
  keys() {
    return Array.from(this.#map.keys());
  }
  // oxlint-disable-next-line typescript-eslint/no-unnecessary-type-parameters
  get<T>(key: string): T | undefined;
  get<T>(key: string, defaultValue: T): T;
  get<T>(key: string, defaultValue?: T): T | undefined {
    // @ts-expect-error - typing is unsafe in VS Code
    return this.#map.get(key) ?? defaultValue;
  }
  async update(key: string, value: unknown) {
    this.#map.set(key, value);
  }
  setKeysForSync() {}

  toJSON() {
    return Object.fromEntries(this.#map.entries());
  }
}

const testExtensionContext = {
  globalState: new Memento(),
  workspaceState: new Memento(),
  extensionUri: Uri.parse("file:///test/extension/path", true),
  globalStorageUri: Uri.parse("file://test/extension/libs", true),
};

export const TestExtensionContextLive = Layer.succeed(
  ExtensionContext,
  testExtensionContext,
);

export function getTestExtensionContext() {
  return Effect.succeed(testExtensionContext);
}
