import { beforeEach, describe, expect, test, vi } from "vitest";
import { TSurveyElementTypeEnum } from "@formbricks/types/surveys/constants";
import type { TJsWorkspaceStateSurvey } from "../../../types/js";
import { type TAllowedFileExtension, ZAllowedFileExtension, mimeTypes } from "../../../types/storage";
import type { TSurveyLanguage } from "../../../types/surveys/types";
import {
  cn,
  findBlockByElementId,
  getDefaultLanguageCode,
  getElementsFromSurveyBlocks,
  getMimeType,
  getShuffledChoicesIds,
  getShuffledRowIndices,
  getSurveyLanguageTag,
  isFileTypeAllowed,
  isRTL,
  isRTLLanguage,
  mirrorPlacementForDir,
  resolveSelectedLanguageCode,
  mimeTypes as uploadMimeTypes,
} from "./utils";

// Mock crypto.getRandomValues for deterministic shuffle tests
const mockGetRandomValues = vi.fn();
vi.stubGlobal("crypto", {
  ...(global.crypto || {}),
  getRandomValues: mockGetRandomValues,
});

describe("getMimeType", () => {
  Object.entries(mimeTypes).forEach(([extension, expectedMimeType]) => {
    test(`should return "${expectedMimeType}" for extension "${extension}"`, () => {
      expect(getMimeType(extension as TAllowedFileExtension)).toBe(expectedMimeType);
    });
  });
});

describe("isFileTypeAllowed", () => {
  test("takes a file whose extension the question allows, in any case", () => {
    expect(isFileTypeAllowed("photo.png", ["png", "pdf"])).toBe(true);
    expect(isFileTypeAllowed("Report.PDF", ["png", "pdf"])).toBe(true);
  });

  test("refuses a file whose extension the question does not allow", () => {
    expect(isFileTypeAllowed("report.pdf", ["png"])).toBe(false);
  });

  test("without a restriction, takes every extension the storage API allows, in any case", () => {
    for (const extension of ZAllowedFileExtension.options) {
      expect(isFileTypeAllowed(`file.${extension}`)).toBe(true);
      expect(isFileTypeAllowed(`file.${extension.toUpperCase()}`, [])).toBe(true);
    }
  });

  test.each(["report.bin", "setup.exe", "photo.png.bin"])(
    "without a restriction, refuses %s, which the storage API refuses",
    (fileName) => {
      expect(isFileTypeAllowed(fileName)).toBe(false);
    }
  );

  test.each(["README", "png", "file."])(
    "refuses %s, which has no extension, as the storage API does",
    (fileName) => {
      expect(isFileTypeAllowed(fileName)).toBe(false);
      expect(isFileTypeAllowed(fileName, ["png"])).toBe(false);
    }
  );

  test("checks against exactly the storage API's allowlist", () => {
    expect(Object.keys(uploadMimeTypes).sort()).toEqual([...ZAllowedFileExtension.options].sort());
  });
});

// Base mock for TJsWorkspaceStateSurvey to satisfy stricter type checks
const baseMockSurvey: TJsWorkspaceStateSurvey = {
  id: "survey1",
  name: "Test Survey",
  type: "link",
  status: "inProgress",
  questions: [],
  blocks: [],
  endings: [],
  welcomeCard: { enabled: false, timeToFinish: true, showResponseCount: false },
  variables: [],
  styling: { overwriteThemeStyling: false },
  recontactDays: null,
  displayLimit: null,
  displayPercentage: null,
  languages: [],
  segment: null,
  hiddenFields: { enabled: false, fieldIds: [] },
  workspaceOverwrites: null,
  triggers: [],
  displayOption: "displayOnce",
} as unknown as TJsWorkspaceStateSurvey;

describe("getDefaultLanguageCode", () => {
  const mockSurveyLanguageEn: TSurveyLanguage = {
    default: true,
    enabled: true,
    language: {
      id: "lang1",
      code: "en",
      alias: null,
      createdAt: new Date(),
      updatedAt: new Date(),
      workspaceId: "proj1",
    },
  };
  const mockSurveyLanguageEs: TSurveyLanguage = {
    default: false,
    enabled: true,
    language: {
      id: "lang2",
      code: "es",
      alias: null,
      createdAt: new Date(),
      updatedAt: new Date(),
      workspaceId: "proj1",
    },
  };

  test("should return the code of the default language", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      languages: [mockSurveyLanguageEs, mockSurveyLanguageEn],
    };
    expect(getDefaultLanguageCode(survey)).toBe("en");
  });

  test("should return undefined if no default language", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      languages: [{ ...mockSurveyLanguageEs, default: false }], // Ensure 'default' is explicitly false
    };
    expect(getDefaultLanguageCode(survey)).toBeUndefined();
  });

  test("should return undefined if languages array is empty", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      languages: [],
    };
    expect(getDefaultLanguageCode(survey)).toBeUndefined();
  });
});

describe("resolveSelectedLanguageCode", () => {
  test("returns the sentinel when the pick is the default language", () => {
    // Selecting the default must record the same thing as never touching the switcher, because
    // survey.tsx resolves "default" to the default language's stored code for response.language.
    expect(resolveSelectedLanguageCode("en-US", "en-US")).toBe("default");
  });

  test("returns the picked code for a non-default language", () => {
    expect(resolveSelectedLanguageCode("de-DE", "en-US")).toBe("de-DE");
  });

  test("returns the sentinel when a canonical pick matches a legacy default code", () => {
    // The dedupe keeps the canonical row, so the visible default option reads "hi-IN" on a survey
    // whose default row stores "hi". Comparing raw strings would store "hi-IN" instead of the
    // sentinel, and response.language would then differ from the untouched-switcher path.
    expect(resolveSelectedLanguageCode("hi-IN", "hi")).toBe("default");
  });

  test("returns the sentinel when a legacy pick matches a canonical default code", () => {
    expect(resolveSelectedLanguageCode("hi", "hi-IN")).toBe("default");
  });

  test("does not collapse two different languages that share nothing canonical", () => {
    expect(resolveSelectedLanguageCode("hi-IN", "en-US")).toBe("hi-IN");
  });

  test("passes the code through untouched when the survey has no default language", () => {
    expect(resolveSelectedLanguageCode("de-DE", undefined)).toBe("de-DE");
  });
});

describe("getSurveyLanguageTag", () => {
  const languageWithCode = (code: string, isDefault: boolean): TSurveyLanguage => ({
    default: isDefault,
    enabled: true,
    language: {
      id: `lang-${code}`,
      code,
      alias: null,
      createdAt: new Date(),
      updatedAt: new Date(),
      workspaceId: "proj1",
    },
  });

  const surveyWithLanguages = (languages: TSurveyLanguage[]): TJsWorkspaceStateSurvey => ({
    ...baseMockSurvey,
    languages,
  });

  const multiLanguageSurvey = surveyWithLanguages([
    languageWithCode("en-US", true),
    languageWithCode("de-DE", false),
  ]);

  test("returns a configured language code unchanged", () => {
    expect(getSurveyLanguageTag(multiLanguageSurvey, "de-DE")).toBe("de-DE");
  });

  test('resolves the "default" sentinel to the default language code', () => {
    // "default" is the renderer's internal marker, not a language tag: putting it in a lang
    // attribute would declare a language that does not exist.
    expect(getSurveyLanguageTag(multiLanguageSurvey, "default")).toBe("en-US");
  });

  test("resolves an empty language code to the default language code", () => {
    expect(getSurveyLanguageTag(multiLanguageSurvey, "")).toBe("en-US");
  });

  test("returns null when the survey has no languages configured", () => {
    // A single-language survey declares nothing, so the host document's language stands.
    expect(getSurveyLanguageTag(surveyWithLanguages([]), "default")).toBeNull();
  });

  test("returns null when no language is marked default", () => {
    expect(
      getSurveyLanguageTag(surveyWithLanguages([languageWithCode("de-DE", false)]), "default")
    ).toBeNull();
  });

  test("falls back to the default for a code the survey does not have", () => {
    // The tag lands in a DOM lang attribute, so an unconfigured code would declare a language whose
    // content is not being rendered — getLocalizedValue falls back to the default text, and a screen
    // reader would read that text with the wrong pronunciation rules. The offline restore path can
    // replay a persisted language that has since been removed from the survey.
    expect(getSurveyLanguageTag(multiLanguageSurvey, "fr-FR")).toBe("en-US");
  });

  test("falls back to the default for a configured but disabled language", () => {
    // Same rule the server applies to ?lang=: a language that is not offered is not declared.
    const survey = surveyWithLanguages([
      languageWithCode("en-US", true),
      { ...languageWithCode("de-DE", false), enabled: false },
    ]);
    expect(getSurveyLanguageTag(survey, "de-DE")).toBe("en-US");
  });

  test("resolves a legacy alias to the stored canonical code", () => {
    // The stored code is what content is keyed under, so that is what the tag has to be.
    const survey = surveyWithLanguages([languageWithCode("en-US", true), languageWithCode("hi-IN", false)]);
    expect(getSurveyLanguageTag(survey, "hi")).toBe("hi-IN");
  });

  test("returns null when nothing matches and there is no default either", () => {
    const survey = surveyWithLanguages([languageWithCode("de-DE", false)]);
    expect(getSurveyLanguageTag(survey, "fr-FR")).toBeNull();
  });
});

const setNextRandomNormalizedValue = (val: number) => {
  mockGetRandomValues.mockImplementationOnce((typedArray: Uint32Array) => {
    typedArray[0] = Math.floor(val * 2 ** 32);
  });
};

describe("getShuffledRowIndices", () => {
  beforeEach(() => {
    mockGetRandomValues.mockReset();
  });

  test('should return unshuffled for "none"', () => {
    expect(getShuffledRowIndices(5, "none")).toEqual([0, 1, 2, 3, 4]);
  });

  test('should shuffle all for "all"', () => {
    setNextRandomNormalizedValue(0.1);
    setNextRandomNormalizedValue(0.1);
    expect(getShuffledRowIndices(3, "all")).toEqual([1, 2, 0]);
  });

  test('should shuffle except last for "exceptLast"', () => {
    setNextRandomNormalizedValue(0.1);
    setNextRandomNormalizedValue(0.1);
    expect(getShuffledRowIndices(4, "exceptLast")).toEqual([1, 2, 0, 3]);
  });

  test("should handle n=0 or n=1", () => {
    expect(getShuffledRowIndices(0, "all")).toEqual([]);
    expect(getShuffledRowIndices(1, "all")).toEqual([0]);
    expect(getShuffledRowIndices(1, "exceptLast")).toEqual([0]);
  });

  test('should reverse all for "reverseOrderOccasionally" when random < 0.5', () => {
    // getSecureRandom returns < 0.5, so the array is reversed
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledRowIndices(4, "reverseOrderOccasionally")).toEqual([3, 2, 1, 0]);
  });

  test('should keep original order for "reverseOrderOccasionally" when random >= 0.5', () => {
    // getSecureRandom returns >= 0.5, so the array is NOT reversed
    setNextRandomNormalizedValue(0.7);
    expect(getShuffledRowIndices(4, "reverseOrderOccasionally")).toEqual([0, 1, 2, 3]);
  });

  test('should preserve all elements with "reverseOrderOccasionally"', () => {
    setNextRandomNormalizedValue(0.3);
    const result = getShuffledRowIndices(5, "reverseOrderOccasionally");
    expect(result).toHaveLength(5);
    expect(result.sort((a, b) => a - b)).toEqual([0, 1, 2, 3, 4]);
  });

  test('should reverse all except last for "reverseOrderExceptLast" when random < 0.5', () => {
    // getSecureRandom returns < 0.5, so the array (minus last) is reversed
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledRowIndices(4, "reverseOrderExceptLast")).toEqual([2, 1, 0, 3]);
  });

  test('should keep original order for "reverseOrderExceptLast" when random >= 0.5', () => {
    // getSecureRandom returns >= 0.5, so the array is NOT reversed
    setNextRandomNormalizedValue(0.7);
    expect(getShuffledRowIndices(4, "reverseOrderExceptLast")).toEqual([0, 1, 2, 3]);
  });

  test('should always keep last element in place for "reverseOrderExceptLast"', () => {
    setNextRandomNormalizedValue(0.3);
    const result = getShuffledRowIndices(5, "reverseOrderExceptLast");
    expect(result[result.length - 1]).toBe(4);
    expect(result).toHaveLength(5);
    expect(result.sort((a, b) => a - b)).toEqual([0, 1, 2, 3, 4]);
  });

  test('should handle n=1 for "reverseOrderOccasionally" and "reverseOrderExceptLast"', () => {
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledRowIndices(1, "reverseOrderOccasionally")).toEqual([0]);
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledRowIndices(1, "reverseOrderExceptLast")).toEqual([0]);
  });
});

describe("getShuffledChoicesIds", () => {
  beforeEach(() => {
    mockGetRandomValues.mockReset();
  });

  const choicesBase = [
    { id: "c1", label: { en: "Choice 1" } },
    { id: "c2", label: { en: "Choice 2" } },
    { id: "c3", label: { en: "Choice 3" } },
  ];
  const choicesWithOther = [...choicesBase, { id: "other", label: { en: "Other" } }];

  test('should return unshuffled for "none"', () => {
    expect(getShuffledChoicesIds(choicesBase, "none")).toEqual(["c1", "c2", "c3"]);
    expect(getShuffledChoicesIds(choicesWithOther, "none")).toEqual(["c1", "c2", "c3", "other"]);
  });

  test('should shuffle all (no "other") for "all"', () => {
    setNextRandomNormalizedValue(0.1);
    setNextRandomNormalizedValue(0.1);
    expect(getShuffledChoicesIds(choicesBase, "all")).toEqual(["c2", "c3", "c1"]);
  });

  test('should shuffle all (with "other") for "all", keeping "other" last', () => {
    setNextRandomNormalizedValue(0.1);
    setNextRandomNormalizedValue(0.1);
    expect(getShuffledChoicesIds(choicesWithOther, "all")).toEqual(["c2", "c3", "c1", "other"]);
  });

  test('should shuffle except last (no "other") for "exceptLast"', () => {
    setNextRandomNormalizedValue(0.1);
    expect(getShuffledChoicesIds(choicesBase, "exceptLast")).toEqual(["c2", "c1", "c3"]);
  });

  test('should shuffle except last (with "other") for "exceptLast", keeping "other" truly last', () => {
    setNextRandomNormalizedValue(0.1);
    expect(getShuffledChoicesIds(choicesWithOther, "exceptLast")).toEqual(["c2", "c1", "c3", "other"]);
  });

  test("should handle empty or single choice arrays", () => {
    expect(getShuffledChoicesIds([], "all")).toEqual([]);
    const singleChoice = [{ id: "s1", label: { en: "Single" } }];
    expect(getShuffledChoicesIds(singleChoice, "all")).toEqual(["s1"]);
    expect(getShuffledChoicesIds(singleChoice, "exceptLast")).toEqual(["s1"]);
  });

  test('should reverse all for "reverseOrderOccasionally" when random < 0.5', () => {
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledChoicesIds(choicesBase, "reverseOrderOccasionally")).toEqual(["c3", "c2", "c1"]);
  });

  test('should keep original order for "reverseOrderOccasionally" when random >= 0.5', () => {
    setNextRandomNormalizedValue(0.7);
    expect(getShuffledChoicesIds(choicesBase, "reverseOrderOccasionally")).toEqual(["c1", "c2", "c3"]);
  });

  test('should preserve "other" at end with "reverseOrderOccasionally" when reversed', () => {
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledChoicesIds(choicesWithOther, "reverseOrderOccasionally")).toEqual([
      "c3",
      "c2",
      "c1",
      "other",
    ]);
  });

  test('should preserve all elements with "reverseOrderOccasionally"', () => {
    setNextRandomNormalizedValue(0.3);
    const result = getShuffledChoicesIds(choicesBase, "reverseOrderOccasionally");
    expect(result).toHaveLength(3);
    expect([...result].sort()).toEqual(["c1", "c2", "c3"]);
  });

  test('should reverse all except last for "reverseOrderExceptLast" when random < 0.5', () => {
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledChoicesIds(choicesBase, "reverseOrderExceptLast")).toEqual(["c2", "c1", "c3"]);
  });

  test('should keep original order for "reverseOrderExceptLast" when random >= 0.5', () => {
    setNextRandomNormalizedValue(0.7);
    expect(getShuffledChoicesIds(choicesBase, "reverseOrderExceptLast")).toEqual(["c1", "c2", "c3"]);
  });

  test('should keep last regular choice in place with "reverseOrderExceptLast", "other" appended after', () => {
    setNextRandomNormalizedValue(0.3);
    expect(getShuffledChoicesIds(choicesWithOther, "reverseOrderExceptLast")).toEqual([
      "c2",
      "c1",
      "c3",
      "other",
    ]);
  });

  test('should always keep last regular element in place for "reverseOrderExceptLast"', () => {
    setNextRandomNormalizedValue(0.3);
    const result = getShuffledChoicesIds(choicesBase, "reverseOrderExceptLast");
    expect(result[result.length - 1]).toBe("c3");
    expect(result).toHaveLength(3);
    expect([...result].sort()).toEqual(["c1", "c2", "c3"]);
  });
});
describe("getQuestionsFromSurvey", () => {
  test("should return elements from blocks", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      blocks: [
        {
          id: "block1",
          name: "Block 1",
          elements: [
            {
              id: "q1",
              type: TSurveyElementTypeEnum.OpenText,
              headline: { en: "Question 1" },
              required: false,
              inputType: "text",
              charLimit: { enabled: false },
            },
            {
              id: "q2",
              type: TSurveyElementTypeEnum.OpenText,
              headline: { en: "Question 2" },
              required: false,
              inputType: "text",
              charLimit: { enabled: false },
            },
          ],
        },
        {
          id: "block2",
          name: "Block 2",
          elements: [
            {
              id: "q3",
              type: TSurveyElementTypeEnum.OpenText,
              headline: { en: "Question 3" },
              required: false,
              inputType: "text",
              charLimit: { enabled: false },
            },
          ],
        },
      ],
    };

    const questions = getElementsFromSurveyBlocks(survey.blocks);
    expect(questions).toHaveLength(3);
    expect(questions[0].id).toBe("q1");
    expect(questions[1].id).toBe("q2");
    expect(questions[2].id).toBe("q3");
  });

  test("should return empty array when blocks is empty", () => {
    const survey = {
      ...baseMockSurvey,
      blocks: [],
    } as TJsWorkspaceStateSurvey;

    expect(getElementsFromSurveyBlocks(survey.blocks)).toEqual([]);
  });

  test("should handle blocks with no elements", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      blocks: [
        { id: "block1", name: "Block 1", elements: [] },
        {
          id: "block2",
          name: "Block 2",
          elements: [
            {
              id: "q1",
              type: TSurveyElementTypeEnum.OpenText,
              headline: { en: "Q1" },
              required: false,
              inputType: "text",
              charLimit: { enabled: false },
            },
          ],
        },
      ],
    };

    const questions = getElementsFromSurveyBlocks(survey.blocks);
    expect(questions).toHaveLength(1);
    expect(questions[0].id).toBe("q1");
  });
});

describe("findBlockByElementId", () => {
  const survey: TJsWorkspaceStateSurvey = {
    ...baseMockSurvey,
    blocks: [
      {
        id: "block1",
        name: "Block 1",
        elements: [
          {
            id: "q1",
            type: TSurveyElementTypeEnum.OpenText,
            headline: { en: "Question 1" },
            required: false,
            inputType: "text",
            charLimit: { enabled: false },
          },
          {
            id: "q2",
            type: TSurveyElementTypeEnum.OpenText,
            headline: { en: "Question 2" },
            required: false,
            inputType: "text",
            charLimit: { enabled: false },
          },
        ],
      },
      {
        id: "block2",
        name: "Block 2",
        elements: [
          {
            id: "q3",
            type: TSurveyElementTypeEnum.OpenText,
            headline: { en: "Question 3" },
            required: false,
            inputType: "text",
            charLimit: { enabled: false },
          },
        ],
      },
    ],
  };

  test("should find block containing the element", () => {
    const block = findBlockByElementId(survey.blocks, "q1");
    expect(block).toBeDefined();
    expect(block?.id).toBe("block1");

    const block2 = findBlockByElementId(survey.blocks, "q3");
    expect(block2).toBeDefined();
    expect(block2?.id).toBe("block2");
  });

  test("should return undefined for non-existent element", () => {
    const block = findBlockByElementId(survey.blocks, "nonexistent");
    expect(block).toBeUndefined();
  });

  test("should find the block by question code when no element has that ID", () => {
    const coded = {
      ...survey,
      blocks: survey.blocks.map((b) =>
        b.id === "block2" ? { ...b, elements: b.elements.map((e) => ({ ...e, code: "age" })) } : b
      ),
    };
    expect(findBlockByElementId(coded.blocks, "age")?.id).toBe("block2");
  });

  test("the Question ID wins when it equals another question's code", () => {
    const clash = {
      ...survey,
      blocks: survey.blocks.map((b) =>
        b.id === "block2" ? { ...b, elements: b.elements.map((e) => ({ ...e, code: "q1" })) } : b
      ),
    };
    expect(findBlockByElementId(clash.blocks, "q1")?.id).toBe("block1");
  });
});

describe("isRTL", () => {
  test("returns true for RTL text", () => {
    expect(isRTL("مرحبا")).toBe(true);
    expect(isRTL("שלום")).toBe(true);
  });

  test("returns false for LTR text", () => {
    expect(isRTL("Hello")).toBe(false);
    expect(isRTL("")).toBe(false);
  });
});

describe("isRTLLanguage", () => {
  test("returns true for RTL language codes when multi-language enabled", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      languages: [
        {
          language: {
            id: "l1",
            code: "ar",
            alias: null,
            createdAt: new Date(),
            updatedAt: new Date(),
            workspaceId: "p1",
          },
          default: true,
          enabled: true,
        },
      ],
    };
    expect(isRTLLanguage(survey, "ar")).toBe(true);
    expect(isRTLLanguage(survey, "he")).toBe(true);
  });

  test("returns false for LTR language codes", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      languages: [
        {
          language: {
            id: "l1",
            code: "en",
            alias: null,
            createdAt: new Date(),
            updatedAt: new Date(),
            workspaceId: "p1",
          },
          default: true,
          enabled: true,
        },
      ],
    };
    expect(isRTLLanguage(survey, "en")).toBe(false);
  });

  test("checks survey content when no languages configured", () => {
    const survey: TJsWorkspaceStateSurvey = {
      ...baseMockSurvey,
      blocks: [
        {
          id: "block1",
          name: "Block 1",
          elements: [
            {
              id: "q1",
              type: TSurveyElementTypeEnum.OpenText,
              headline: { default: "مرحبا" },
              required: false,
              inputType: "text",
              charLimit: { enabled: false },
            },
          ],
        },
      ],
    };
    expect(isRTLLanguage(survey, "default")).toBe(true);
  });
});

describe("cn", () => {
  test("joins multiple classes", () => {
    expect(cn("foo", "bar")).toBe("foo bar");
  });

  test("filters out undefined values", () => {
    expect(cn("foo", undefined, "bar")).toBe("foo bar");
  });

  test("filters out empty strings", () => {
    expect(cn("foo", "", "bar")).toBe("foo bar");
  });

  test("merges conflicting tailwind classes (last wins)", () => {
    expect(cn("mb-6", "mb-8")).toBe("mb-8");
  });

  test("merges conflicting min-h classes", () => {
    expect(cn("min-h-40", "min-h-0")).toBe("min-h-0");
  });

  test("merges conflicting padding classes", () => {
    expect(cn("p-4", "p-2")).toBe("p-2");
  });

  test("keeps non-conflicting classes", () => {
    expect(cn("mb-6 block rounded-md", "w-1/4")).toBe("mb-6 block rounded-md w-1/4");
  });

  test("handles single class", () => {
    expect(cn("foo")).toBe("foo");
  });

  test("handles no arguments", () => {
    expect(cn()).toBe("");
  });

  test("handles all undefined", () => {
    expect(cn(undefined, undefined)).toBe("");
  });

  test("handles nested arrays of classes", () => {
    expect(cn(["foo", ["foo", ["foo", "bar"]]])).toBe("foo foo foo bar");
  });

  test("handles nulls, booleans and undefined values", () => {
    expect(cn(null, true, false, undefined, [null, true, false, undefined])).toBe("");
  });
});

describe("mirrorPlacementForDir", () => {
  test("mirrors the horizontal side of every corner placement in RTL", () => {
    expect(mirrorPlacementForDir("bottomRight", "rtl")).toBe("bottomLeft");
    expect(mirrorPlacementForDir("bottomLeft", "rtl")).toBe("bottomRight");
    expect(mirrorPlacementForDir("topRight", "rtl")).toBe("topLeft");
    expect(mirrorPlacementForDir("topLeft", "rtl")).toBe("topRight");
  });

  test("leaves center untouched in RTL — it has no side to flip", () => {
    expect(mirrorPlacementForDir("center", "rtl")).toBe("center");
  });

  test("returns the authored placement unchanged for ltr and auto", () => {
    expect(mirrorPlacementForDir("bottomRight", "ltr")).toBe("bottomRight");
    expect(mirrorPlacementForDir("topLeft", "ltr")).toBe("topLeft");
    expect(mirrorPlacementForDir("bottomRight", "auto")).toBe("bottomRight");
  });

  test("is its own inverse, so a survey switched back to an LTR language returns to its corner", () => {
    const placements = ["bottomRight", "bottomLeft", "topRight", "topLeft", "center"] as const;
    for (const placement of placements) {
      expect(mirrorPlacementForDir(mirrorPlacementForDir(placement, "rtl"), "rtl")).toBe(placement);
    }
  });
});
