// Auto-generated by scripts/gen_parity_tests.py from docs/runtime-parity.md.
// Module: node:url
// This file is a byte-for-byte parity test against `node --experimental-strip-types`.

import * as url from "node:url";

// ── Class: URL (WHATWG) ──

try {
  const u = new URL("https://user:pass@example.com:8080/p/q?k=v&k2=v2#frag");
  console.log("u.hash:", u.hash);
  console.log("u.host:", u.host);
  console.log("u.hostname:", u.hostname);
  console.log("u.href:", u.href);
  console.log("u.origin:", u.origin);
  console.log("u.password:", u.password);
  console.log("u.pathname:", u.pathname);
  console.log("u.port:", u.port);
  console.log("u.protocol:", u.protocol);
  console.log("u.search:", u.search);
  console.log("u.username:", u.username);
  console.log("u.toString():", u.toString());
  console.log("u.toJSON():", u.toJSON());
} catch (e) {
  console.log("URL fields ERROR:", (e as Error).message);
}

try {
  const u2 = new URL("/path", "https://example.com");
  console.log("relative href:", u2.href);
} catch (e) {
  console.log("relative href ERROR:", (e as Error).message);
}

try {
  const u3 = new URL("https://example.com/");
  u3.pathname = "/changed";
  u3.search = "?x=1";
  u3.hash = "#h";
  console.log("mutated:", u3.href);
} catch (e) {
  console.log("mutated ERROR:", (e as Error).message);
}

try {
  console.log("canParse valid:", URL.canParse("https://example.com"));
  console.log("canParse invalid:", URL.canParse("not a url"));
  console.log("canParse with base:", URL.canParse("/p", "https://example.com"));
} catch (e) {
  console.log("canParse ERROR:", (e as Error).message);
}

try {
  const parsed = URL.parse("https://example.com/x");
  console.log("URL.parse href:", parsed === null || parsed === undefined ? parsed : parsed.href);
  console.log("URL.parse invalid:", URL.parse("not a url"));
} catch (e) {
  console.log("URL.parse ERROR:", (e as Error).message);
}

// URL.createObjectURL / revokeObjectURL — need Blob; deferred.

// ── Class: URLSearchParams ──

try {
  const sp1 = new URLSearchParams();
  sp1.append("a", "1");
  sp1.append("b", "2");
  sp1.append("a", "3");
  console.log("sp1.toString:", sp1.toString());
  console.log("sp1.size:", sp1.size);
  console.log("sp1.get a:", sp1.get("a"));
  console.log("sp1.getAll a:", sp1.getAll("a"));
  console.log("sp1.has a:", sp1.has("a"));
  console.log("sp1.has missing:", sp1.has("missing"));

  const sp2 = new URLSearchParams("foo=1&bar=2&foo=3");
  console.log("sp2 from string:", sp2.toString());

  const sp3 = new URLSearchParams({ key: "val", other: "thing" });
  console.log("sp3 from obj:", sp3.toString());

  const sp4 = new URLSearchParams([["a", "1"], ["b", "2"]]);
  console.log("sp4 from iter:", sp4.toString());

  sp1.set("a", "only");
  console.log("sp1 after set:", sp1.toString());

  sp1.delete("b");
  console.log("sp1 after delete:", sp1.toString());

  const sp5 = new URLSearchParams("c=3&a=1&b=2");
  sp5.sort();
  console.log("sp5 sorted:", sp5.toString());
} catch (e) {
  console.log("URLSearchParams basics ERROR:", (e as Error).message);
}

try {
  const sp6 = new URLSearchParams("a=1&b=2");
  const keys: string[] = [];
  for (const k of sp6.keys()) keys.push(k);
  console.log("sp6 keys:", keys.join(","));

  const values: string[] = [];
  for (const v of sp6.values()) values.push(v);
  console.log("sp6 values:", values.join(","));

  const entries: string[] = [];
  for (const [k, v] of sp6.entries()) entries.push(`${k}=${v}`);
  console.log("sp6 entries:", entries.join(","));

} catch (e) {
  console.log("URLSearchParams iter ERROR:", (e as Error).message);
}

// ── Module-level functions ──

try {
  console.log("domainToASCII bücher:", url.domainToASCII("bücher.example"));
  console.log("domainToASCII ascii:", url.domainToASCII("example.com"));
  console.log("domainToUnicode xn:", url.domainToUnicode("xn--bcher-kva.example"));
} catch (e) {
  console.log("domainToASCII/Unicode ERROR:", (e as Error).message);
}

try {
  console.log("fileURLToPath posix:", url.fileURLToPath("file:///foo/bar.txt"));
  const back = url.pathToFileURL("/foo/bar.txt");
  console.log("pathToFileURL href:", back.href);
} catch (e) {
  console.log("fileURLToPath/pathToFileURL ERROR:", (e as Error).message);
}

try {
  const opts = url.urlToHttpOptions(new URL("https://user:pw@example.com:8080/p?q=1"));
  console.log("urlToHttpOptions host:", opts.host);
  console.log("urlToHttpOptions hostname:", opts.hostname);
  console.log("urlToHttpOptions port:", opts.port);
  console.log("urlToHttpOptions path:", opts.path);
  console.log("urlToHttpOptions auth:", opts.auth);
} catch (e) {
  console.log("urlToHttpOptions ERROR:", (e as Error).message);
}

try {
  console.log("format URL:", url.format(new URL("https://example.com/path?q=1#h"), { fragment: false, search: false }));
} catch (e) {
  console.log("format URL ERROR:", (e as Error).message);
}

// ── Legacy URL API (deprecated since Node 11) ──

try {
  const legacy = url.parse("https://user:pw@example.com:8080/p?q=1#h", true);
  console.log("legacy host:", legacy.host);
  console.log("legacy hostname:", legacy.hostname);
  console.log("legacy port:", legacy.port);
  console.log("legacy pathname:", legacy.pathname);
  console.log("legacy search:", legacy.search);
  console.log("legacy query.q:", (legacy.query as Record<string, string>).q);
  console.log("legacy hash:", legacy.hash);
  console.log("legacy auth:", legacy.auth);
} catch (e) {
  console.log("legacy parse ERROR:", (e as Error).message);
}

try {
  const legacyFormat = url.format({
    protocol: "https:",
    hostname: "example.com",
    port: "8080",
    pathname: "/x",
    query: { a: "1" },
  });
  console.log("legacy format:", legacyFormat);
} catch (e) {
  console.log("legacy format ERROR:", (e as Error).message);
}

try {
  console.log("legacy resolve abs:", url.resolve("/a/b/c", "/d"));
  console.log("legacy resolve rel:", url.resolve("https://example.com/a/", "b/c"));
} catch (e) {
  console.log("legacy resolve ERROR:", (e as Error).message);
}
