import { describe, expect, it } from "vitest";

if (!(globalThis as any).document) {
  const { Window } = await import("happy-dom");
  const window = new Window();
  (globalThis as any).window = window;
  (globalThis as any).document = window.document;
  Object.defineProperty(globalThis, "navigator", {
    configurable: true,
    value: window.navigator,
  });
  (globalThis as any).Node = window.Node;
  (globalThis as any).Element = window.Element;
  (globalThis as any).HTMLElement = window.HTMLElement;
  (globalThis as any).SVGElement = window.SVGElement;
  (globalThis as any).Event = window.Event;
  (globalThis as any).CustomEvent = window.CustomEvent;
  (globalThis as any).MouseEvent = window.MouseEvent;
  (globalThis as any).KeyboardEvent = window.KeyboardEvent;
  (globalThis as any).getComputedStyle = window.getComputedStyle.bind(window);
}

(globalThis as any).getSelection = () => ({ toString: () => "" });

globalThis.requestAnimationFrame = ((cb: FrameRequestCallback) => {
  cb(0);
  return 1;
}) as typeof globalThis.requestAnimationFrame;
globalThis.cancelAnimationFrame = (() => {}) as typeof globalThis.cancelAnimationFrame;

const { defineComponent, h, nextTick } = await import("vue");
const { createTerminalApp } = await import("@simon_he/vue-tui");
const { DimSdkProviderConnectDialog } = await import("../src/dimsdk/ui/components/ProviderConnectDialog.ts");

async function flushApp(app: {
  scheduler: { flush: () => void };
}) {
  await nextTick();
  await nextTick();
  app.scheduler.flush();
}

function snapshotText(app: {
  terminal: { snapshot: () => { lines: string[] } };
}): string {
  return app.terminal.snapshot().lines.join("\n");
}

describe("provider connect dialog input clear tip", () => {
  it("shows the clear tip on the focused base url field", async () => {
    const App = defineComponent({
      name: "ProviderConnectDialogInputClearTipApp",
      setup() {
        return () =>
          h(DimSdkProviderConnectDialog as any, {
            modelValue: true,
            providerName: "OpenAI",
            w: 72,
            h: 16,
            baseUrl: "https://api.example.com/v1",
            apiKeyDraft: "",
            apiKeyMasked: "",
            showBaseUrl: true,
            showApiKey: true,
            focusKey: false,
            "onUpdate:modelValue": () => {},
            "onUpdate:baseUrl": () => {},
            "onUpdate:apiKeyDraft": () => {},
            onApply: () => {},
            onClearKey: () => {},
            onCancel: () => {},
          });
      },
    });

    const app = createTerminalApp({
      cols: 90,
      rows: 24,
      component: App as never,
      defaultStyle: { fg: "whiteBright", bg: "black" },
    });

    try {
      app.mount();
      await flushApp(app);

      const snapshot = snapshotText(app);
      expect(snapshot).toContain("Tab switch");
      expect(snapshot).toContain("Ctrl+C clear");
      expect(snapshot).toContain("Base URL • Ctrl+C clear");
    } finally {
      app.dispose();
    }
  });

  it("renders readonly base url without the input clear tip", async () => {
    const App = defineComponent({
      name: "ReadonlyProviderConnectDialogApp",
      setup() {
        return () =>
          h(DimSdkProviderConnectDialog as any, {
            modelValue: true,
            providerName: "DeepSeek",
            w: 72,
            h: 16,
            baseUrl: "https://api.deepseek.com/v1",
            apiKeyDraft: "",
            apiKeyMasked: "",
            baseUrlMode: "readonly",
            showApiKey: true,
            focusKey: true,
            "onUpdate:modelValue": () => {},
            "onUpdate:baseUrl": () => {},
            "onUpdate:apiKeyDraft": () => {},
            onApply: () => {},
            onClearKey: () => {},
            onCancel: () => {},
          });
      },
    });

    const app = createTerminalApp({
      cols: 90,
      rows: 24,
      component: App as never,
      defaultStyle: { fg: "whiteBright", bg: "black" },
    });

    try {
      app.mount();
      await flushApp(app);

      const snapshot = snapshotText(app);
      expect(snapshot).toContain("Base URL");
      expect(snapshot).toContain("https://api.deepseek.com/v1");
      expect(snapshot).not.toContain("Base URL • Ctrl+C clear");
    } finally {
      app.dispose();
    }
  });
});
