import { describe, expect, it } from "vitest";
import { useProviderDialogs } from "../src/dimsdk/pages/utils/provider-dialogs.ts";

function createStore(
  input?: Partial<{
    activeId: string;
    connections: Record<string, any>;
    customProviders: any[];
    activeModelByProvider: Record<string, string>;
    env: Record<string, string>;
    cwd: string;
    model: string;
    contextWindow: number;
    sessionId: string;
  }>,
): any {
  const env = { ...input?.env };
  return {
    state: {
      ui: {
        onboarding: {
          lastSeenVersion: "",
        },
        providerFunnel: {
          counts: {},
          events: [],
        },
        workspaceSelectionByCwd: {},
      },
      provider: {
        activeId: input?.activeId ?? "openai",
        connections: input?.connections ?? {},
        customProviders: input?.customProviders ?? [],
        activeModelByProvider: input?.activeModelByProvider ?? {},
      },
      config: {
        model: input?.model ?? "",
        context_window: input?.contextWindow ?? 128000,
      },
      sessionId: input?.sessionId ?? "",
      context: { env, cwd: input?.cwd ?? "." },
    },
    setEnvVar: (key: string, value: string | undefined) => {
      if (value == null) {
        delete env[key];
      } else {
        env[key] = String(value);
      }
    },
  };
}

function findProviderOption(dialogs: ReturnType<typeof useProviderDialogs>, keyword: string) {
  return dialogs.providerOptions.value.find((option) => (option.keywords ?? []).includes(keyword));
}

describe("dimsdk connect dialog cancel", () => {
  it("returns to provider picker and preserves selection", () => {
    const store = createStore({ activeId: "openai" });
    const dialogs = useProviderDialogs({
      store,
      nodeLike: false,
      showToast: () => {},
      openCustomSettings: () => {},
    });

    dialogs.openConnect();

    const deepseekOption = findProviderOption(dialogs, "deepseek");
    expect(deepseekOption).toBeTruthy();
    const deepseekLabel = deepseekOption!.label;

    const deepseekIndex = dialogs.providerOptions.value.findIndex(
      (option) => option.label === deepseekLabel,
    );
    expect(deepseekIndex).toBeGreaterThanOrEqual(0);

    dialogs.providerIndex.value = deepseekIndex;
    dialogs.onSelectProvider(deepseekLabel);

    expect(dialogs.showConnect.value).toBe(false);
    expect(dialogs.showConnectDialog.value).toBe(true);
    const firstProviderName = dialogs.connectProviderName.value;
    expect(firstProviderName).toBeTruthy();

    dialogs.cancelConnectDialog();

    expect(dialogs.showConnectDialog.value).toBe(false);
    expect(dialogs.showConnect.value).toBe(true);
    expect(dialogs.providerIndex.value).toBe(deepseekIndex);

    const preservedOption = dialogs.providerOptions.value[dialogs.providerIndex.value];
    expect(preservedOption?.label).toBe(deepseekLabel);

    dialogs.onSelectProvider(preservedOption?.label ?? null);

    expect(dialogs.showConnect.value).toBe(false);
    expect(dialogs.showConnectDialog.value).toBe(true);
    expect(dialogs.connectProviderName.value).toBe(firstProviderName);
  });
});
