/* @vitest-environment node */

import { describe, it, expect } from "vitest";
import { SEARCH_SCOPE } from "@/lib/frontend/search/urlState";
import {
	buildSearchHref,
	buildSearchKey,
	getScopeLabel,
	needsMultiBranchSelectionHint,
	buildNextStateForScopeChange,
	buildNextStateForToggleBranch,
	buildNextStateForClearAllBranches,
	buildHrefForSingleBranchSwitch,
} from "./pageHelpers.js";

describe("lib/frontend/search/pageHelpers", () => {
	it("buildSearchHref builds /:branch/search with query string", () => {
		const href = buildSearchHref({
			routeBranch: "NL01",
			state: { q: "x", scope: SEARCH_SCOPE.SINGLE, limit: 200 },
		});
		expect(href).toBe("/NL01/search?q=x&limit=200");
	});

	it("buildSearchKey includes routeBranch", () => {
		const key = buildSearchKey({
			routeBranch: "NL01",
			urlState: { q: "x", scope: SEARCH_SCOPE.SINGLE, limit: 200 },
		});
		expect(key).toBe("NL01|q=x&limit=200");
	});

	it("getScopeLabel returns correct labels", () => {
		expect(
			getScopeLabel({
				routeBranch: "NL01",
				urlState: { scope: SEARCH_SCOPE.SINGLE },
			})
		).toBe("Niederlassung NL01");

		expect(
			getScopeLabel({
				routeBranch: "NL01",
				urlState: { scope: SEARCH_SCOPE.ALL },
			})
		).toBe("Alle Niederlassungen");

		expect(
			getScopeLabel({
				routeBranch: "NL01",
				urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01", "NL02"] },
			})
		).toBe("2 Niederlassungen");
	});

	it("needsMultiBranchSelectionHint only when q exists and branches empty", () => {
		expect(
			needsMultiBranchSelectionHint({
				isAdminDev: true,
				urlState: { scope: SEARCH_SCOPE.MULTI, q: "x", branches: [] },
			})
		).toBe(true);

		expect(
			needsMultiBranchSelectionHint({
				isAdminDev: true,
				urlState: { scope: SEARCH_SCOPE.MULTI, q: null, branches: [] },
			})
		).toBe(false);
	});

	it("buildNextStateForScopeChange keeps branches only for MULTI", () => {
		const base = { q: "x", branches: ["NL01"] };

		expect(
			buildNextStateForScopeChange({
				urlState: base,
				nextScope: SEARCH_SCOPE.ALL,
			})
		).toMatchObject({ scope: SEARCH_SCOPE.ALL, branches: [] });

		expect(
			buildNextStateForScopeChange({
				urlState: base,
				nextScope: SEARCH_SCOPE.MULTI,
			})
		).toMatchObject({ scope: SEARCH_SCOPE.MULTI, branches: ["NL01"] });
	});

	it("buildNextStateForToggleBranch toggles membership", () => {
		const s1 = buildNextStateForToggleBranch({
			urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01"] },
			branchId: "NL02",
		});
		expect(s1.branches.sort()).toEqual(["NL01", "NL02"]);

		const s2 = buildNextStateForToggleBranch({
			urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01", "NL02"] },
			branchId: "NL02",
		});
		expect(s2.branches).toEqual(["NL01"]);
	});

	it("buildNextStateForClearAllBranches clears branches", () => {
		const s = buildNextStateForClearAllBranches({
			urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01"] },
		});
		expect(s.branches).toEqual([]);
		expect(s.scope).toBe(SEARCH_SCOPE.MULTI);
	});

	it("buildHrefForSingleBranchSwitch returns null for invalid branch", () => {
		expect(
			buildHrefForSingleBranchSwitch({
				nextBranch: "bad",
				urlState: { q: "x", scope: SEARCH_SCOPE.SINGLE, limit: 100 },
			})
		).toBe(null);
	});
});
