/**
 * NXL (Nexus Language) Interpreter
 *
 * Parses declarative .nxl files to define agent archetypes,
 * swarm sizes, and execution modes.
 */
import * as yaml from 'js-yaml';
import { nexusEventBus } from './event-bus.js';
export class NXLInterpreter {
    archetypes = new Map();
    constructor() {
        this.registerDefaultArchetypes();
    }
    registerDefaultArchetypes() {
        this.archetypes.set('product-dev', {
            name: 'Product Developer',
            role: 'End-to-end product implementation (PDLC)',
            capabilities: ['planning', 'coding', 'testing', 'documentation'],
            temperature: 0.7,
            tools: ['grep_search', 'write_to_file', 'run_command']
        });
        this.archetypes.set('researcher', {
            name: 'Deep Researcher',
            role: 'Log analysis and codebase archaeology',
            capabilities: ['analysis', 'pattern-matching', 'context-retrieval'],
            temperature: 0.2,
            tools: ['nexus_recall_memory', 'githubSearchCode', 'grep_search']
        });
        this.archetypes.set('architect', {
            name: 'System Architect',
            role: 'High-level structural design and risk analysis',
            capabilities: ['modelling', 'risk-assessment', 'dependency-mapping'],
            temperature: 0.4,
            tools: ['nexus_ghost_pass', 'nexus_graph_query']
        });
        this.archetypes.set('ux-validator', {
            name: 'UX Validator',
            role: 'Runtime verification and UI/UX testing',
            capabilities: ['visual-check', 'interaction-testing', 'latency-analysis'],
            temperature: 0.5,
            tools: ['read_browser_page', 'command_status']
        });
    }
    /**
     * Parses a .nxl file (YAML format) and extracts swarm or archetype definitions.
     */
    parse(content) {
        try {
            const data = yaml.load(content) ?? {};
            nexusEventBus.emit('nexusnet.sync', { newItemsCount: 1 }); // Mocking event for now
            return data;
        }
        catch (error) {
            throw new Error(`NXL Parse Error: ${error.message}`);
        }
    }
    /**
     * Induces a specialized "army" of agents based on the use case.
     */
    induceArmy(useCase) {
        const army = [];
        const uc = useCase.toLowerCase();
        if (uc.includes('pdlc') || uc.includes('product')) {
            army.push(this.archetypes.get('architect'));
            army.push(this.archetypes.get('product-dev'));
            army.push(this.archetypes.get('ux-validator'));
        }
        else if (uc.includes('research') || uc.includes('bug') || uc.includes('investigate')) {
            army.push(this.archetypes.get('researcher'));
            army.push(this.archetypes.get('architect'));
        }
        else {
            // Default: Generalist Swarm
            army.push(this.archetypes.get('product-dev'));
            army.push(this.archetypes.get('researcher'));
        }
        return army;
    }
    getArchetype(name) {
        return this.archetypes.get(name);
    }
    compileExecution(goal, parsedInput = {}, useCase) {
        const induced = this.induceArmy(useCase || goal);
        const roles = normalizeStringArray(parsedInput.roles) ?? induced.map(agent => agent.name.toLowerCase().replace(/\s+/g, '-'));
        const strategies = normalizeStringArray(parsedInput.strategies) ?? ['minimal', 'standard', 'thorough'];
        const verify = normalizeStringArray(parsedInput.verify) ?? [];
        const skills = normalizeStringArray(parsedInput.skills) ?? [];
        const workflows = normalizeStringArray(parsedInput.workflows) ?? [];
        const crews = normalizeStringArray(parsedInput.crews) ?? [];
        const specialists = normalizeStringArray(parsedInput.specialists) ?? [];
        const files = normalizeStringArray(parsedInput.files) ?? [];
        const workers = normalizeNumber(parsedInput.workers) ?? Math.max(1, roles.filter(role => role.includes('coder')).length || roles.length || 1);
        const skillPolicy = normalizeSkillPolicy(parsedInput.skillPolicy);
        const workflowPolicy = normalizeSkillPolicy(parsedInput.workflowPolicy);
        const consensus = normalizeConsensus(parsedInput.consensus);
        const guardrails = typeof parsedInput.guardrails === 'boolean' ? parsedInput.guardrails : true;
        const derivationPolicy = normalizeDerivationPolicy(parsedInput.derivationPolicy);
        const backendMode = normalizeBackendMode(parsedInput.backendMode);
        return {
            goal: String(parsedInput.goal ?? goal),
            files,
            workers,
            roles,
            strategies,
            verify,
            skills,
            workflows,
            guardrails,
            consensus,
            memoryBackend: String(parsedInput.memoryBackend ?? 'sqlite-memory'),
            compressionBackend: String(parsedInput.compressionBackend ?? 'deterministic-token-supremacy'),
            dslCompiler: String(parsedInput.dslCompiler ?? 'deterministic-nxl-compiler'),
            backendMode,
            skillPolicy,
            workflowPolicy,
            derivationPolicy,
            crews,
            specialists,
            optimizationProfile: normalizeOptimizationProfile(parsedInput.optimizationProfile),
            actions: Array.isArray(parsedInput.actions)
                ? parsedInput.actions.filter((value) => !!value && typeof value === 'object')
                : [],
            metadata: {
                useCase: useCase ?? null,
                inducedArchetypes: induced.map(agent => agent.name),
                mode: String(parsedInput.mode ?? 'parallel'),
            },
        };
    }
}
export const nxl = new NXLInterpreter();
function normalizeStringArray(value) {
    if (!Array.isArray(value))
        return null;
    return value.map(String).filter(Boolean);
}
function normalizeNumber(value) {
    const numeric = Number(value);
    return Number.isFinite(numeric) && numeric > 0 ? numeric : null;
}
function normalizeSkillPolicy(value) {
    if (value === 'session-only' || value === 'manual')
        return value;
    return 'guarded-hot';
}
function normalizeConsensus(value) {
    if (value === 'run' || value === 'global')
        return value;
    return 'local';
}
function normalizeDerivationPolicy(value) {
    if (value === 'manual' || value === 'disabled')
        return value;
    return 'auto';
}
function normalizeBackendMode(value) {
    if (value === 'shadow' || value === 'experimental')
        return value;
    return 'default';
}
function normalizeOptimizationProfile(value) {
    return value === 'max' ? 'max' : 'standard';
}
