import path from 'node:path';
import os from 'node:os';
import fs from 'node:fs/promises';
import { nowIso, writeJsonAtomic } from '../fsx.js';
import { buildCodexPluginInventory } from '../codex-plugins/codex-plugin-json.js';
const RESERVED_EXTERNAL_ROUTE_SKILLS = ['ulw-loop', 'ulw-plan', 'start-work'];
export async function buildCodexNativeInteropPolicy(input) {
    const root = path.resolve(input.root);
    const inventory = normalizeInventory(input.inventory || await buildCodexPluginInventory().catch((err) => ({ plugins: [], blockers: [messageOf(err)] })));
    const codexHome = input.codexHome || process.env.CODEX_HOME || path.join(os.homedir(), '.codex');
    const skillNames = await discoverSkillNames([path.join(root, '.agents', 'skills'), path.join(codexHome, 'skills')]);
    const pluginIds = (inventory.plugins || []).map((plugin) => `${plugin.id || ''} ${plugin.name || ''}`.toLowerCase()).filter(Boolean);
    const preservedSkillNames = RESERVED_EXTERNAL_ROUTE_SKILLS.filter((name) => skillNames.includes(name));
    const report = {
        schema: 'sks.codex-native-interop-policy.v1',
        generated_at: nowIso(),
        ok: true,
        mode: input.mode || 'coexist',
        detection: {
            plugin_inventory_ids: pluginIds,
            skill_names: skillNames,
            preserved_skill_names: preservedSkillNames
        },
        policy: {
            clobber_user_skills: false,
            clobber_external_route_assets: false,
            explicit_handoff_required: true,
            artifact_names_brand_neutral: true
        },
        actions: preservedSkillNames.map((name) => `preserve_existing_skill:${name}`),
        blockers: []
    };
    await writeJsonAtomic(path.join(root, '.sneakoscope', 'reports', 'codex-native-interop-policy.json'), report).catch(() => undefined);
    return report;
}
function normalizeInventory(value) {
    if (!value || typeof value !== 'object' || Array.isArray(value))
        return { plugins: [], blockers: [] };
    const record = value;
    return {
        plugins: Array.isArray(record.plugins) ? record.plugins.map((plugin) => plugin && typeof plugin === 'object' ? plugin : {}) : [],
        blockers: Array.isArray(record.blockers) ? record.blockers.map(String) : []
    };
}
function messageOf(err) {
    return err instanceof Error ? err.message : String(err);
}
async function discoverSkillNames(roots) {
    const names = new Set();
    for (const root of roots) {
        const entries = await fs.readdir(root, { withFileTypes: true }).catch(() => []);
        for (const entry of entries)
            if (entry.isDirectory())
                names.add(entry.name);
    }
    return [...names].sort();
}
//# sourceMappingURL=codex-native-interop-policy.js.map