import { CliError } from '../../../errors/index.js';
import { sanitizeCodexRoleName, stringifyToml, withOatManagedCodexHeader, } from './shared.js';
function resolveSandboxMode(agent) {
    const codexExtension = agent.extensions.x_codex;
    if (codexExtension &&
        typeof codexExtension === 'object' &&
        codexExtension !== null &&
        'sandbox_mode' in codexExtension &&
        typeof codexExtension.sandbox_mode === 'string') {
        return codexExtension.sandbox_mode;
    }
    if (agent.readonly === true) {
        return 'read-only';
    }
    return 'workspace-write';
}
function codexExtensionMap(agent) {
    const extension = agent.extensions.x_codex;
    if (!extension || typeof extension !== 'object' || Array.isArray(extension)) {
        return {};
    }
    return { ...extension };
}
export function exportCanonicalAgentToCodexRole(agent) {
    const roleName = sanitizeCodexRoleName(agent.name);
    if (!roleName) {
        throw new CliError('Cannot export Codex role: missing canonical agent name.');
    }
    const extension = codexExtensionMap(agent);
    const roleObject = {
        ...extension,
        developer_instructions: agent.body,
        sandbox_mode: resolveSandboxMode(agent),
    };
    if (agent.model && typeof roleObject.model !== 'string') {
        roleObject.model = agent.model;
    }
    const tomlBody = stringifyToml(roleObject);
    return {
        roleName,
        description: agent.description,
        configFile: `agents/${roleName}.toml`,
        content: withOatManagedCodexHeader(roleName, tomlBody),
    };
}
