/**
 * Weapon profile merging and cross-unit deduplication.
 *
 * Source data splits multi-profile weapons into separate rows using an
 * en-dash delimiter: "Plasma pistol – Standard" / "Plasma pistol – Supercharge".
 * These become a single weapon entity with a profiles array.
 *
 * Cross-unit dedup: weapons with identical (name, type, all profile stats)
 * across different units are the same entity, referenced by a shared ID.
 */
import { nameToId } from "./id-generator.js";
import { parseRange, parseBSWS, parseStatValue, parseAP, parseWeaponKeywords, } from "./stat-parser.js";
// EN-DASH (U+2013) and regular hyphen used as profile separators
const PROFILE_SEPARATOR = /\s*[\u2013\u2014-]\s+/;
/** Split "Plasma pistol – Standard" into { baseName: "Plasma pistol", profileName: "Standard" } */
function splitProfileName(fullName) {
    const parts = fullName.split(PROFILE_SEPARATOR);
    if (parts.length >= 2) {
        return {
            baseName: parts[0].trim(),
            profileName: parts.slice(1).join(" – ").trim(),
        };
    }
    return { baseName: fullName.trim(), profileName: fullName.trim() };
}
/** Build a single weapon profile from a source wargear row. */
function buildProfile(row, profileName) {
    const range = parseRange(row.range);
    const isMelee = row.type === "Melee" || range === "Melee";
    const stats = {
        A: parseStatValue(row.A),
        S: parseStatValue(row.S),
        AP: parseAP(row.AP),
        D: parseStatValue(row.D),
    };
    if (isMelee) {
        stats.WS = parseBSWS(row.BS_WS);
    }
    else {
        stats.BS = parseBSWS(row.BS_WS);
    }
    return {
        name: profileName,
        range,
        stats,
        keywords: parseWeaponKeywords(row.description),
    };
}
/** Hash a weapon entity for cross-unit deduplication. */
function weaponHash(name, type, profiles) {
    const profileParts = profiles
        .map((p) => `${p.name}|${p.range}|${JSON.stringify(p.stats)}|${p.keywords.sort().join(",")}`)
        .sort();
    return `${name.toLowerCase()}|${type}|${profileParts.join(";")}`;
}
/**
 * Merge wargear rows for a single unit into weapon entities,
 * then deduplicate across all units.
 */
export function buildWeaponRegistry(allUnitWargear, gameVersion) {
    // Global dedup registry: hash → WeaponEntity
    const registry = new Map();
    const unitWeaponIds = new Map();
    for (const [datasheetId, rows] of allUnitWargear) {
        const weaponIds = new Set();
        unitWeaponIds.set(datasheetId, weaponIds);
        // Group rows by base weapon name for profile merging
        const groups = new Map();
        for (const row of rows) {
            const { baseName, profileName } = splitProfileName(row.name);
            const key = baseName.toLowerCase();
            if (!groups.has(key)) {
                groups.set(key, { baseName, rows: [], profileNames: [] });
            }
            const group = groups.get(key);
            group.rows.push(row);
            group.profileNames.push(profileName);
        }
        for (const [, group] of groups) {
            const { baseName, rows: groupRows, profileNames } = group;
            const type = groupRows[0].type === "Melee" ? "melee" : "ranged";
            // Skip rows with obviously corrupt source data (shifted columns)
            const validRows = groupRows.filter((row, i) => {
                const s = parseStatValue(row.S);
                if (typeof s === "number" && s < 0)
                    return false;
                const d = parseStatValue(row.D);
                if (type === "ranged" && d === 0 && row.D === "")
                    return false;
                return true;
            });
            if (validRows.length === 0)
                continue;
            const validProfileNames = groupRows.map((row, i) => ({ row, name: profileNames[i] }))
                .filter(({ row }) => validRows.includes(row))
                .map(({ name }) => name);
            // If there's only one profile and its name matches the base name, use base name as profile name
            const profiles = validRows.map((row, i) => {
                const pName = validRows.length === 1 ? baseName : validProfileNames[i];
                return buildProfile(row, pName);
            });
            const hash = weaponHash(baseName, type, profiles);
            if (!registry.has(hash)) {
                const id = nameToId(baseName);
                registry.set(hash, {
                    id,
                    name: baseName,
                    type: type,
                    profiles,
                    game_version: gameVersion,
                });
            }
            weaponIds.add(registry.get(hash).id);
        }
    }
    return {
        weapons: [...registry.values()],
        unitWeaponIds,
    };
}
//# sourceMappingURL=weapon-dedup.js.map