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

function createStore(): any {
  return {
    state: {
      ui: {
        onboarding: {
          lastSeenVersion: "",
        },
        providerFunnel: {
          counts: {},
          events: [],
        },
        workspaceSelectionByCwd: {},
      },
      provider: {
        activeId: "openai",
        connections: {},
        customProviders: [],
        activeModelByProvider: {},
      },
      config: { model: "" },
      context: { env: {}, cwd: "." },
    },
    setEnvVar: () => {},
  };
}

describe("provider dialogs next api gate", () => {
  it("hides Next API OAuth from the provider picker by default", () => {
    const prev = process.env.DIMCODE_ENABLE_NEXT_API_AUTH;
    delete process.env.DIMCODE_ENABLE_NEXT_API_AUTH;
    try {
      const dialogs = useProviderDialogs({
        store: createStore(),
        nodeLike: true,
        showToast: () => {},
        openCustomSettings: () => {},
        openNextApiOAuth: () => {},
      });

      const option = dialogs.providerOptions.value.find((entry) =>
        (entry.keywords ?? []).some((keyword) => keyword.includes("next-api") || keyword.includes("oauth")),
      );
      expect(option).toBeUndefined();
    } finally {
      if (prev == null) {
        delete process.env.DIMCODE_ENABLE_NEXT_API_AUTH;
      } else {
        process.env.DIMCODE_ENABLE_NEXT_API_AUTH = prev;
      }
    }
  });
});
