import test from "node:test";
import assert from "node:assert/strict";
import {
  isLocale,
  normalizePath,
  publicPath,
  resolvePublicPath,
  switchLocale,
  toInternalPath,
  toPublicFromInternal,
} from "./routes.ts";

test("isLocale", () => {
  assert.equal(isLocale("de"), true);
  assert.equal(isLocale("en"), true);
  assert.equal(isLocale("fr"), false);
  assert.equal(isLocale(""), false);
});

test("normalizePath", () => {
  assert.equal(normalizePath(""), "/");
  assert.equal(normalizePath("/"), "/");
  assert.equal(normalizePath("/projekte/"), "/projekte");
  assert.equal(normalizePath("/en/about"), "/en/about");
});

test("publicPath: home/about/projects/contact/legal/privacy", () => {
  assert.equal(publicPath("de", "home"), "/");
  assert.equal(publicPath("en", "home"), "/en");
  assert.equal(publicPath("de", "about"), "/ueber-mich");
  assert.equal(publicPath("en", "about"), "/en/about");
  assert.equal(publicPath("de", "projects"), "/projekte");
  assert.equal(publicPath("en", "projects"), "/en/projects");
});

test("publicPath: projectItem", () => {
  assert.equal(publicPath("de", "projectItem", "hermes"), "/projekte/hermes");
  assert.equal(publicPath("en", "projectItem", "hermes"), "/en/projects/hermes");
});

test("resolvePublicPath: home/about/projects/contact/legal/privacy in beiden Sprachen", () => {
  assert.deepEqual(resolvePublicPath("/"), { locale: "de", route: "home" });
  assert.deepEqual(resolvePublicPath("/en"), { locale: "en", route: "home" });
  assert.deepEqual(resolvePublicPath("/ueber-mich"), { locale: "de", route: "about" });
  assert.deepEqual(resolvePublicPath("/en/about"), { locale: "en", route: "about" });
  assert.deepEqual(resolvePublicPath("/projekte"), { locale: "de", route: "projects" });
  assert.deepEqual(resolvePublicPath("/en/projects"), { locale: "en", route: "projects" });
  assert.deepEqual(resolvePublicPath("/kontakt"), { locale: "de", route: "contact" });
  assert.deepEqual(resolvePublicPath("/impressum"), { locale: "de", route: "legal" });
  assert.deepEqual(resolvePublicPath("/en/legal"), { locale: "en", route: "legal" });
  assert.deepEqual(resolvePublicPath("/datenschutz"), { locale: "de", route: "privacy" });
});

test("resolvePublicPath: projectItem", () => {
  assert.deepEqual(resolvePublicPath("/projekte/hermes"), { locale: "de", route: "projectItem", slug: "hermes" });
  assert.deepEqual(resolvePublicPath("/en/projects/casino-bot"), { locale: "en", route: "projectItem", slug: "casino-bot" });
});

test("resolvePublicPath: admin login", () => {
  assert.deepEqual(resolvePublicPath("/admin/login"), { locale: "de", route: "adminLogin" });
  assert.deepEqual(resolvePublicPath("/admin"), { locale: "de", route: "adminLogin" });
});

test("resolvePublicPath: ungültige Pfade", () => {
  assert.equal(resolvePublicPath("/foo"), null);
  assert.equal(resolvePublicPath("/en/foo/bar"), null);
  assert.equal(resolvePublicPath("/projekte/hermes/extra"), null);
});

test("toInternalPath + toPublicFromInternal sind inverse", () => {
  for (const path of ["/", "/en", "/ueber-mich", "/en/about", "/projekte", "/en/projects", "/projekte/hermes", "/en/projects/hermes"]) {
    const internal = toInternalPath(path);
    assert.ok(internal, `internal für ${path}`);
    assert.equal(toPublicFromInternal(internal!), path);
  }
});

test("switchLocale wechselt Sprache auf gleicher Seite", () => {
  assert.equal(switchLocale("/ueber-mich", "en"), "/en/about");
  assert.equal(switchLocale("/projekte/hermes", "en"), "/en/projects/hermes");
  assert.equal(switchLocale("/en/contact", "de"), "/kontakt");
});

test("switchLocale ignoriert Admin-Login", () => {
  assert.equal(switchLocale("/admin/login", "en"), null);
});
