import { getCompanyFromEmail, getMostFrequentRole } from "@ax/modules/Sites/SitesList/WelcomeModal/utils";

describe("getCompanyFromEmail", () => {
	it("should extract company name from professional email", () => {
		expect(getCompanyFromEmail("user@secuoyas.com")).toBe("Secuoyas");
	});

	it("should capitalize the first letter of the company name", () => {
		expect(getCompanyFromEmail("user@acme.io")).toBe("Acme");
	});

	it("should return empty string for generic email providers", () => {
		expect(getCompanyFromEmail("user@gmail.com")).toBe("");
		expect(getCompanyFromEmail("user@hotmail.com")).toBe("");
		expect(getCompanyFromEmail("user@outlook.com")).toBe("");
		expect(getCompanyFromEmail("user@yahoo.com")).toBe("");
		expect(getCompanyFromEmail("user@icloud.com")).toBe("");
	});

	it("should return empty string when email has no domain", () => {
		expect(getCompanyFromEmail("usernodomain")).toBe("");
	});

	it("should return the full subdomain string before the TLD", () => {
		expect(getCompanyFromEmail("user@mail.company.com")).toBe("Mail.company");
	});
});

describe("getMostFrequentRole", () => {
	it("should return null for empty userRoles", () => {
		expect(getMostFrequentRole([])).toBeNull();
	});

	it("should return the ID of the most frequent role", () => {
		const userRoles = [
			{ siteId: 1, roles: [1, 2] },
			{ siteId: 2, roles: [1] },
		];
		expect(getMostFrequentRole(userRoles)).toBe(1);
	});

	it("should handle single role occurrence", () => {
		const userRoles = [{ siteId: 1, roles: [5] }];
		expect(getMostFrequentRole(userRoles)).toBe(5);
	});

	it("should return correct role when multiple roles have different frequencies", () => {
		const userRoles = [
			{ siteId: 1, roles: [1, 2, 3] },
			{ siteId: 2, roles: [1, 2] },
			{ siteId: 3, roles: [1] },
		];
		expect(getMostFrequentRole(userRoles)).toBe(1);
	});
});
