import { prettyJson, titleCaseId, totalArmyPoints } from "./helpers.js";
// Mirror the importer's constants (kept inline rather than imported so the
// exporter stays decoupled — the seams are the `item` keys themselves).
const CLS_ROSTER = "Roster";
const CLS_FACTION = "Faction";
const CLS_DETACHMENT = "Detachment";
const CLS_UNIT = "Unit";
const CLS_WEAPON = "Weapon";
const CLS_ENHANCEMENT = "Enhancement";
const CLS_BATTLE_SIZE = "Battle Size";
const CLS_TRAIT = "Trait";
const DSG_WARLORD = "Warlord";
const RULEBOOK_NAME = "40kdc";
const RULEBOOK_GAME = "Warhammer 40,000";
const RULEBOOK_PUBLISHER = "Alpaca Software";
const RULEBOOK_URL = "https://40kdc.dev";
const RULEBOOK_GENRE = "wargame";
function key(classification, designation) {
    return `${classification}§${designation}`; // §
}
function pointsStat(value) {
    if (value === null || value === undefined)
        return undefined;
    return { Points: { value } };
}
function wargearAsset(w) {
    return {
        item: key(CLS_WEAPON, w.ref.raw_name),
        name: w.ref.raw_name,
        quantity: w.count,
    };
}
function enhancementAsset(u) {
    if (!u.enhancement)
        return null;
    return {
        item: key(CLS_ENHANCEMENT, u.enhancement.raw_name),
        name: u.enhancement.raw_name,
        quantity: 1,
        ...(u.enhancement_points !== null
            ? { stats: pointsStat(u.enhancement_points) }
            : {}),
    };
}
function warlordTraitAsset() {
    return {
        item: key(CLS_TRAIT, DSG_WARLORD),
        name: DSG_WARLORD,
        quantity: 1,
    };
}
function unitAsset(u) {
    const included = [];
    const enh = enhancementAsset(u);
    if (enh !== null)
        included.push(enh);
    for (const w of u.wargear)
        included.push(wargearAsset(w));
    const traits = [];
    if (u.is_warlord)
        traits.push(warlordTraitAsset());
    const asset = {
        item: key(CLS_UNIT, u.ref.raw_name),
        name: u.ref.raw_name,
        quantity: u.model_count,
    };
    const stats = pointsStat(u.points);
    if (stats !== undefined)
        asset.stats = stats;
    if (included.length > 0 || traits.length > 0) {
        asset.assets = {};
        if (included.length > 0)
            asset.assets.included = included;
        if (traits.length > 0)
            asset.assets.traits = traits;
    }
    return asset;
}
function factionAsset(roster) {
    const display = titleCaseId(roster.faction_id);
    if (display === null)
        return null;
    return { item: key(CLS_FACTION, display), name: display, quantity: 1 };
}
function detachmentAsset(roster) {
    const display = titleCaseId(roster.detachment_id);
    if (display === null)
        return null;
    return { item: key(CLS_DETACHMENT, display), name: display, quantity: 1 };
}
function battleSizeAsset(roster) {
    if (roster.battle_size === "strike-force") {
        const limit = roster.points.declared_limit ?? 2000;
        const label = `Strike Force (${limit} Point limit)`;
        return { item: key(CLS_BATTLE_SIZE, label), name: label, quantity: 1 };
    }
    if (roster.battle_size === "incursion") {
        const limit = roster.points.declared_limit ?? 1000;
        const label = `Incursion (${limit} Point limit)`;
        return { item: key(CLS_BATTLE_SIZE, label), name: label, quantity: 1 };
    }
    return null;
}
export const rosterizerSerializer = {
    id: "rosterizer",
    serialize(roster) {
        const included = [];
        const faction = factionAsset(roster);
        if (faction)
            included.push(faction);
        const detachment = detachmentAsset(roster);
        if (detachment)
            included.push(detachment);
        const battleSize = battleSizeAsset(roster);
        if (battleSize)
            included.push(battleSize);
        for (const u of roster.units)
            included.push(unitAsset(u));
        const total = totalArmyPoints(roster);
        const snapshot = {
            item: key(CLS_ROSTER, CLS_ROSTER),
            name: roster.name,
            quantity: 1,
            ...(total > 0 ? { stats: pointsStat(total) } : {}),
            assets: { included },
        };
        const envelope = {
            slug: "",
            key: "",
            visible: "hidden",
            locked: false,
            rulebook: {
                name: RULEBOOK_NAME,
                game: RULEBOOK_GAME,
                publisher: RULEBOOK_PUBLISHER,
                url: RULEBOOK_URL,
                genre: RULEBOOK_GENRE,
            },
            snapshot,
        };
        return prettyJson(envelope);
    },
};
//# sourceMappingURL=rosterizer.js.map