import { expect, test } from "bun:test";
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
  compileWireArtifactsAsync,
  setCompileCacheDir,
  setCompileImportOptions,
} from "../src/pipeline.ts";

test("development browser artifacts bundle local client imports", async () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-client-bundle-"));
  const app = join(root, "app");
  const cache = join(root, ".wrnexus");
  mkdirSync(app, { recursive: true });
  writeFileSync(join(app, "helper.ts"), "export function save(value: unknown) { return value; }\n");
  const page = join(app, "settings.wrn");
  writeFileSync(
    page,
    `import { save } from "./helper.ts"
page Settings {
  state { value = 1 }
  functions { client function persist() { return save(value) } }
  view { <button @click='persist()'>Save</button> }
}`,
  );

  try {
    setCompileCacheDir(cache);
    const artifacts = await compileWireArtifactsAsync(page);
    const browser = readFileSync(artifacts.browser, "utf8");
    expect(browser).toContain("wrnexus-client-bundled");
    expect(browser).not.toContain("file://");
    expect(browser).not.toContain('from "./helper.ts"');
  } finally {
    setCompileCacheDir(null);
    rmSync(root, { recursive: true, force: true });
  }
});

test("development browser artifacts bundle configured alias imports", async () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-client-alias-bundle-"));
  const app = join(root, "app");
  const cache = join(root, ".wrnexus");
  mkdirSync(join(app, "client"), { recursive: true });
  writeFileSync(
    join(app, "client", "storage.ts"),
    "export function persist(value: unknown) { return value; }\n",
  );
  const page = join(app, "settings.wrn");
  writeFileSync(
    page,
    `import { persist } from "~/client/storage.ts"
page Settings {
  state { value = 1 }
  functions { client function save() { return persist(value) } }
  view { <button @click='save()'>Save</button> }
}`,
  );

  try {
    setCompileCacheDir(cache);
    setCompileImportOptions(root, {
      mode: "explicit",
      aliases: { "~": "./app" },
      autoImport: false,
    });
    const artifacts = await compileWireArtifactsAsync(page);
    const browser = readFileSync(artifacts.browser, "utf8");
    expect(browser).toContain("wrnexus-client-bundled");
    expect(browser).not.toContain("~/client/storage.ts");
    expect(browser).not.toContain("file://");

    mkdirSync(join(root, "alternate", "client"), { recursive: true });
    writeFileSync(
      join(root, "alternate", "client", "storage.ts"),
      "export function persist(value: unknown) { return value; }\n",
    );
    setCompileImportOptions(root, {
      mode: "explicit",
      aliases: { "~": "./alternate" },
      autoImport: false,
    });
    const changed = await compileWireArtifactsAsync(page);
    expect(changed.browser).not.toBe(artifacts.browser);
  } finally {
    setCompileCacheDir(null);
    rmSync(root, { recursive: true, force: true });
  }
});
