import { prettyJson, titleCaseId, totalArmyPoints } from "./helpers.js";
const PTS_TYPE_ID = "pts-type";
const NEWRECRUIT_XMLNS = "http://www.battlescribe.net/schema/rosterSchema";
const NEWRECRUIT_GENERATED_BY = "https://newrecruit.eu";
/** Build a "Faction: <name>" category from the unit's roster context. */
function factionCategory(roster) {
    const display = titleCaseId(roster.faction_id);
    if (display === null)
        return null;
    return { name: `Faction: ${display}`, primary: false };
}
function wargearSelection(idx, w) {
    return {
        id: `w-${idx}`,
        name: w.ref.raw_name,
        type: "upgrade",
        number: w.count,
        // The NewRecruit importer recognises a wargear selection by a category
        // ending in " Weapon" — emit a generic "Ranged Weapon" so we don't have
        // to track ranged-vs-melee separation the Roster doesn't model.
        categories: [{ name: "Ranged Weapon", primary: false }],
    };
}
function unitSelection(idx, u, faction) {
    const inner = [];
    if (u.is_warlord) {
        inner.push({ id: `u${idx}-warlord`, name: "Warlord", type: "upgrade", number: 1 });
    }
    if (u.enhancement) {
        const enhCost = u.enhancement_points === null
            ? undefined
            : [{ name: "pts", typeId: PTS_TYPE_ID, value: u.enhancement_points }];
        inner.push({
            id: `u${idx}-enh`,
            name: u.enhancement.raw_name,
            type: "upgrade",
            number: 1,
            group: "Enhancements",
            ...(enhCost ? { costs: enhCost } : {}),
        });
    }
    const wargearSelections = u.wargear.map((w, wi) => wargearSelection(wi, w));
    const ownCategories = faction ? [faction] : [];
    if (u.model_count <= 1) {
        return {
            id: `u-${idx}`,
            name: u.ref.raw_name,
            type: "model",
            number: 1,
            categories: ownCategories,
            ...(u.points === null ? {} : { costs: [{ name: "pts", typeId: PTS_TYPE_ID, value: u.points }] }),
            selections: [...inner, ...wargearSelections],
        };
    }
    // Multi-model: wrap in a `type: "unit"` with a nested `type: "model"` that
    // carries the model count and the (collapsed, per-unit) wargear.
    return {
        id: `u-${idx}`,
        name: u.ref.raw_name,
        type: "unit",
        number: 1,
        categories: ownCategories,
        ...(u.points === null ? {} : { costs: [{ name: "pts", typeId: PTS_TYPE_ID, value: u.points }] }),
        selections: [
            ...inner,
            {
                id: `u${idx}-model`,
                name: u.ref.raw_name,
                type: "model",
                number: u.model_count,
                selections: wargearSelections,
            },
        ],
    };
}
function configSelection(name, value, idx) {
    return {
        id: `cfg-${idx}`,
        name,
        type: "upgrade",
        number: 1,
        categories: [{ name: "Configuration", primary: true }],
        selections: [
            {
                id: `cfg-${idx}-val`,
                name: value,
                type: "upgrade",
                number: 1,
            },
        ],
    };
}
function battleSizeLabel(roster) {
    if (roster.battle_size === "strike-force") {
        const limit = roster.points.declared_limit ?? 2000;
        return `Strike Force (${limit} Point limit)`;
    }
    if (roster.battle_size === "incursion") {
        const limit = roster.points.declared_limit ?? 1000;
        return `Incursion (${limit} Point limit)`;
    }
    return null;
}
export const newRecruitJsonSerializer = {
    id: "newrecruit-json",
    serialize(roster) {
        const faction = factionCategory(roster);
        const factionDisplay = titleCaseId(roster.faction_id) ?? "Unknown";
        const detachmentDisplay = titleCaseId(roster.detachment_id);
        const battleSize = battleSizeLabel(roster);
        const config = [];
        if (battleSize)
            config.push(configSelection("Battle Size", battleSize, "battle-size"));
        if (detachmentDisplay)
            config.push(configSelection("Detachment", detachmentDisplay, "detachment"));
        const force = {
            id: "force-1",
            name: "Army Roster",
            catalogueName: factionDisplay,
            selections: [
                ...config,
                ...roster.units.map((u, i) => unitSelection(i, u, faction)),
            ],
        };
        const total = totalArmyPoints(roster);
        const payload = {
            name: roster.name,
            generatedBy: NEWRECRUIT_GENERATED_BY,
            roster: {
                name: roster.name,
                xmlns: NEWRECRUIT_XMLNS,
                generatedBy: NEWRECRUIT_GENERATED_BY,
                costs: [{ name: "pts", typeId: PTS_TYPE_ID, value: total }],
                forces: [force],
            },
        };
        return prettyJson(payload);
    },
};
//# sourceMappingURL=newrecruit-json.js.map