import { test, expect } from "vitest";
import { generateOutput } from "./message-modules.js";
test("should emit per locale message files", () => {
    const resources = [
        {
            bundle: {
                code: 'console.log("bundle code");',
                node: {
                    id: "happy_elephant",
                },
            },
            messages: {
                en: {
                    code: 'console.log("message in English");',
                    node: {},
                },
                de: {
                    code: 'console.log("message in German");',
                    node: {},
                },
            },
        },
    ];
    const settings = {
        locales: ["en", "de"],
        baseLocale: "en",
    };
    const fallbackMap = {
        en: "de",
        de: "en",
    };
    const output = generateOutput(resources, settings, fallbackMap);
    expect(output).not.toHaveProperty("messages/en.js");
    expect(output).not.toHaveProperty("messages/de.js");
    expect(output).toHaveProperty("messages/happy_elephant.js");
});
test("handles case senstivity by creating directories and files only in lowercase", () => {
    const resources = [
        {
            bundle: {
                code: "export const HappyElephant = () => en.HappyElephant",
                node: {
                    id: "HappyElephant",
                },
            },
            messages: {
                en: {
                    code: 'export const HappyElephant = () => "HappyElephant0"',
                    node: {},
                },
            },
        },
        {
            bundle: {
                code: "export const happyelephant = () => en.happyelephant",
                node: {
                    id: "happyelephant",
                },
            },
            messages: {
                en: {
                    code: 'export const happyelephant = () => "happyelephant1"',
                    node: {},
                },
            },
        },
    ];
    const settings = {
        locales: ["en"],
        baseLocale: "en",
    };
    const output = generateOutput(resources, settings, {});
    // expecting only lowercase directories and files
    expect(output).toHaveProperty("messages/happyelephant.js");
    expect(output).toHaveProperty("messages/happyelephant2.js");
    expect(output).not.toHaveProperty("messages/HappyElephant.js");
});
// Regression test for https://github.com/opral/inlang-paraglide-js/issues/507
test("emits fallback definitions after their dependencies", () => {
    const resources = [
        {
            bundle: {
                code: "export const admin_tasks = (inputs) => inputs;",
                node: {
                    id: "admin_tasks",
                    declarations: [],
                },
            },
            messages: {
                en: {
                    code: '/** @type {(inputs: {}) => string} */ () => "admin"',
                    node: {},
                },
            },
        },
    ];
    const settings = {
        locales: ["fr-ca", "fr", "en"],
        baseLocale: "en",
    };
    const fallbackMap = {
        "fr-ca": "fr",
        fr: "en",
        en: undefined,
    };
    const output = generateOutput(resources, settings, fallbackMap);
    const file = output["messages/admin_tasks.js"];
    expect(file).toBeDefined();
    if (!file) {
        throw new Error("messages/admin_tasks.js should have been generated");
    }
    expect(file).toContain("const fr_admin_tasks = en_admin_tasks;");
    expect(file).toContain("const fr_ca_admin_tasks = fr_admin_tasks;");
    const frIndex = file.indexOf("const fr_admin_tasks");
    const frCaIndex = file.indexOf("const fr_ca_admin_tasks");
    expect(frIndex).toBeGreaterThan(-1);
    expect(frCaIndex).toBeGreaterThan(-1);
    expect(frIndex).toBeLessThan(frCaIndex);
});
