import path from 'node:path';
import { flag } from '../../cli/args.js';
import { printJson } from '../../cli/output.js';
import { projectRoot } from '../fsx.js';
import { clearFastModePreference, fastModePreferencePath, readFastModePreference, resolveFastModePolicy, writeFastModePreference } from '../agents/fast-mode-policy.js';
import { ensureGlobalCodexFastModeDuringInstall } from '../../cli/install-helpers.js';
export const FAST_MODE_COMMAND_SCHEMA = 'sks.fast-mode-command.v1';
export async function fastModeCommand(args = []) {
    const action = normalizeFastModeAction(args[0]);
    const root = path.resolve(String(readOption(args, '--root', '') || await projectRoot()));
    const statePath = fastModePreferencePath(root);
    let preference = await readFastModePreference(root);
    let removed = null;
    if (action === 'on' || action === 'off') {
        const mode = action === 'on' ? 'fast' : 'standard';
        preference = await writeFastModePreference(root, mode, `sks fast-mode ${action}`);
    }
    else if (action === 'clear') {
        const result = await clearFastModePreference(root);
        removed = result.removed;
        preference = null;
    }
    const policy = resolveFastModePolicy({ root });
    const codexFastModeRepair = action === 'on'
        ? await ensureGlobalCodexFastModeDuringInstall({ forceFastMode: true })
        : null;
    const result = {
        schema: FAST_MODE_COMMAND_SCHEMA,
        ok: true,
        action,
        root,
        state_path: statePath,
        preference,
        removed,
        fast_mode: policy.fast_mode,
        service_tier: policy.service_tier,
        disabled_by: policy.disabled_by,
        codex_fast_mode_repair: codexFastModeRepair,
        policy,
        dollar_commands: {
            on: '$Fast-On',
            off: '$Fast-Off',
            status: '$Fast-Mode'
        },
        cli_commands: {
            on: 'sks fast-mode on',
            off: 'sks fast-mode off',
            clear: 'sks fast-mode clear',
            status: 'sks fast-mode status'
        }
    };
    if (flag(args, '--json'))
        return printJson(result);
    console.log('SKS Fast Mode');
    console.log(`Root: ${root}`);
    console.log(`Status: ${result.fast_mode ? 'on' : 'off'} (service_tier=${result.service_tier})`);
    console.log(`State: ${path.relative(root, statePath)}`);
    if (action === 'on')
        console.log('Saved: fast mode on');
    else if (action === 'off')
        console.log('Saved: fast mode off');
    else if (action === 'clear')
        console.log(`Cleared: ${removed ? 'yes' : 'already default'}`);
    else if (!preference)
        console.log('Preference: implicit standard');
    console.log('Dollar: $Fast-On | $Fast-Off | $Fast-Mode');
    return result;
}
function normalizeFastModeAction(value) {
    const text = String(value || 'status').toLowerCase();
    if (['on', 'enable', 'enabled', 'fast', 'priority'].includes(text))
        return 'on';
    if (['off', 'disable', 'disabled', 'standard', 'slow', 'default'].includes(text))
        return 'off';
    if (['clear', 'reset'].includes(text))
        return 'clear';
    return 'status';
}
function readOption(args = [], name, fallback = null) {
    const index = args.indexOf(name);
    if (index >= 0 && args[index + 1] && !String(args[index + 1]).startsWith('--'))
        return args[index + 1];
    const prefixed = args.find((arg) => String(arg).startsWith(name + '='));
    return prefixed ? prefixed.slice(name.length + 1) : fallback;
}
//# sourceMappingURL=fast-mode-command.js.map