// import-entities.ts — engine EntityBlueprint[] → schema EntityPlacement[]
/** Tags injected by role during export — must be stripped on import to avoid accumulation. */
const ROLE_INJECTED_TAGS = new Set([
    'hostile', 'boss', 'elite', 'merchant', 'trader',
    'quest-giver', 'recruitable', 'companion',
]);
/**
 * Reverse-map engine type + tags → author EntityRole.
 * Priority: boss > companion > merchant > quest-giver > npc > enemy
 */
function reverseRole(type, tags) {
    const tagSet = new Set(tags);
    if (type === 'enemy' && tagSet.has('boss'))
        return 'boss';
    if (tagSet.has('companion') || tagSet.has('recruitable'))
        return 'companion';
    if (tagSet.has('merchant') || tagSet.has('trader'))
        return 'merchant';
    if (tagSet.has('quest-giver'))
        return 'quest-giver';
    if (type === 'enemy')
        return 'enemy';
    return 'npc';
}
export function importEntities(engineEntities, zoneIds) {
    const warnings = [];
    const fidelity = [];
    const placements = [];
    // EB-008: Informational fidelity entry when no entities to import
    if (engineEntities.length === 0) {
        fidelity.push({
            level: 'lossless', domain: 'entities', severity: 'info',
            message: 'No entities in content pack — imported world will have no NPCs or encounters.',
            reason: 'no-entities-in-source',
        });
    }
    for (let i = 0; i < engineEntities.length; i++) {
        const eb = engineEntities[i];
        const role = reverseRole(eb.type, eb.tags ?? []);
        // Strip role-injected tags and faction:* tags to recover author tags
        let factionId;
        const authorTags = [];
        for (const tag of eb.tags ?? []) {
            if (tag.startsWith('faction:')) {
                factionId = tag.slice(8);
            }
            else if (!ROLE_INJECTED_TAGS.has(tag)) {
                authorTags.push(tag);
            }
        }
        // Round-robin zone assignment — only warn when zoneIds is empty (a real problem),
        // not for every entity placement (EB-004: reduce warning noise)
        const zoneId = zoneIds.length > 0 ? zoneIds[i % zoneIds.length] : 'unplaced';
        if (zoneIds.length === 0) {
            warnings.push(`Entity '${eb.name}' has no zones available for placement — assigned to 'unplaced'. Import zones first or create at least one zone.`);
        }
        fidelity.push({
            level: 'approximated', domain: 'entities', severity: 'warning',
            entityId: eb.id, fieldPath: 'zoneId',
            message: `Entity '${eb.name}' zone round-robin assigned to '${zoneId}'`,
            reason: 'zone-placement-round-robin',
        });
        fidelity.push({
            level: 'approximated', domain: 'entities', severity: 'info',
            entityId: eb.id, fieldPath: 'role',
            message: `Entity '${eb.name}' role '${role}' reverse-mapped from type '${eb.type}'`,
            reason: 'role-reverse-mapped',
        });
        const placement = {
            entityId: eb.id,
            name: eb.name,
            zoneId,
            role,
            tags: authorTags.length > 0 ? authorTags : undefined,
            factionId,
        };
        if (eb.baseStats && typeof eb.baseStats === 'object' && Object.keys(eb.baseStats).length > 0) {
            placement.stats = { ...eb.baseStats };
        }
        if (eb.baseResources && typeof eb.baseResources === 'object' && Object.keys(eb.baseResources).length > 0) {
            placement.resources = { ...eb.baseResources };
        }
        const customField = eb.custom;
        if (customField && typeof customField === 'object' && !Array.isArray(customField)) {
            const validated = {};
            for (const [k, v] of Object.entries(customField)) {
                validated[k] = typeof v === 'string' ? v : String(v);
            }
            placement.custom = validated;
        }
        if (eb.aiProfile) {
            placement.ai = { profileId: eb.aiProfile };
        }
        placements.push(placement);
    }
    return { placements, warnings, fidelity };
}
//# sourceMappingURL=import-entities.js.map