/* @vitest-environment node */

import { describe, it, expect } from "vitest";
import {
	ADMIN_USERS_SORT,
	normalizeAdminUsersSortMode,
	sortAdminUsers,
} from "./usersSorting.js";

function mapRows(rows) {
	return rows.map((x) => ({
		id: x.id,
		role: x.role,
		branchId: x.branchId,
		username: x.username,
	}));
}

describe("lib/frontend/admin/users/usersSorting", () => {
	it("normalizes known sort modes and rejects invalid values", () => {
		expect(normalizeAdminUsersSortMode(undefined)).toBe(
			ADMIN_USERS_SORT.DEFAULT,
		);
		expect(normalizeAdminUsersSortMode("")).toBe(ADMIN_USERS_SORT.DEFAULT);
		expect(normalizeAdminUsersSortMode(ADMIN_USERS_SORT.ROLE_RIGHTS)).toBe(
			ADMIN_USERS_SORT.ROLE_RIGHTS,
		);
		expect(normalizeAdminUsersSortMode("nope")).toBe(null);
	});

	it("sorts by role rights with deterministic tie-breakers", () => {
		const items = [
			{
				id: "6",
				role: "admin",
				branchId: null,
				username: "z-admin",
			},
			{
				id: "1",
				role: "branch",
				branchId: "NL10",
				username: "branch-z",
			},
			{
				id: "2",
				role: "branch",
				branchId: "NL2",
				username: "branch-a",
			},
			{
				id: "3",
				role: "dev",
				branchId: null,
				username: "dev",
			},
			{
				id: "4",
				role: "superadmin",
				branchId: null,
				username: "root",
			},
			{
				id: "5",
				role: "admin",
				branchId: null,
				username: "a-admin",
			},
		];

		const out = sortAdminUsers(items, ADMIN_USERS_SORT.ROLE_RIGHTS);

		expect(mapRows(out)).toEqual([
			{ id: "4", role: "superadmin", branchId: null, username: "root" },
			{ id: "3", role: "dev", branchId: null, username: "dev" },
			{ id: "5", role: "admin", branchId: null, username: "a-admin" },
			{ id: "6", role: "admin", branchId: null, username: "z-admin" },
			{ id: "2", role: "branch", branchId: "NL2", username: "branch-a" },
			{ id: "1", role: "branch", branchId: "NL10", username: "branch-z" },
		]);
	});

	it("sorts by branch asc (numeric) with null branch at the end", () => {
		const items = [
			{ id: "1", role: "branch", branchId: "NL10", username: "b1" },
			{ id: "2", role: "branch", branchId: "NL2", username: "b2" },
			{ id: "3", role: "admin", branchId: null, username: "admin" },
			{ id: "4", role: "superadmin", branchId: null, username: "super" },
			{ id: "5", role: "branch", branchId: "NL01", username: "b0" },
		];

		const out = sortAdminUsers(items, ADMIN_USERS_SORT.BRANCH_ASC);

		expect(mapRows(out)).toEqual([
			{ id: "5", role: "branch", branchId: "NL01", username: "b0" },
			{ id: "2", role: "branch", branchId: "NL2", username: "b2" },
			{ id: "1", role: "branch", branchId: "NL10", username: "b1" },
			{ id: "4", role: "superadmin", branchId: null, username: "super" },
			{ id: "3", role: "admin", branchId: null, username: "admin" },
		]);
	});

	it("returns a shallow copy for default mode", () => {
		const items = [{ id: "1" }, { id: "2" }];
		const out = sortAdminUsers(items, ADMIN_USERS_SORT.DEFAULT);
		expect(out).toEqual(items);
		expect(out).not.toBe(items);
	});
});

