import { checkbox, confirm, input, password, select } from '@inquirer/prompts';
import ansis from 'ansis';
import { TOOLS } from '../core/tools.js';
import { readSettings, writeSettings } from '../core/settings.js';
import { findProvider } from '../core/providers.js';
import { applyProfileToInstalledTools, applyProfileToSettings, listProfiles, maskKey, readProfile, readState, writeProfile, } from '../core/profiles.js';
/**
 * `ccjk edit` —— 改当前 profile 的字段，比 `profile copy + 改` 快得多。
 *
 * 典型场景：
 *   - "我那个 key 过期了，给我换一下" → `ccjk edit --api-key sk-newkey`
 *   - "厂商换了 URL"                  → `ccjk edit --base-url https://new.com`
 *   - "想试新模型"                     → `ccjk edit --model glm-4.6-air`
 *
 * 修改后默认询问要不要同步写到 settings.json，避免必须 `ccjk use` 才生效。
 */
export async function editCommand(opts = {}) {
    const profile = await loadTarget(opts.name);
    if (!profile)
        return;
    console.log(ansis.bold(`\n编辑 profile: ${ansis.cyan(profile.name)}`));
    console.log(ansis.dim(`  provider:  ${profile.provider}`));
    console.log(ansis.dim(`  baseUrl:   ${profile.baseUrl}`));
    console.log(ansis.dim(`  authKey:   ${maskKey(profile.apiKey)}`));
    if (profile.model)
        console.log(ansis.dim(`  model:     ${profile.model}`));
    if (profile.fastModel)
        console.log(ansis.dim(`  haiku:     ${profile.fastModel}`));
    if (profile.sonnetModel)
        console.log(ansis.dim(`  sonnet:    ${profile.sonnetModel}`));
    if (profile.opusModel)
        console.log(ansis.dim(`  opus:      ${profile.opusModel}`));
    console.log();
    // 用 CLI 参数传入的字段直接写，不再交互
    const direct = await applyDirectOpts(profile, opts);
    if (!direct) {
        // 没有 CLI 参数 → 进入交互勾选
        const fields = await checkbox({
            message: '要修改哪些字段？',
            choices: [
                { name: `API Key / Auth Token  ${ansis.dim(maskKey(profile.apiKey))}`, value: 'apiKey' },
                { name: `Base URL              ${ansis.dim(profile.baseUrl)}`, value: 'baseUrl' },
                { name: `Main model            ${ansis.dim(profile.model ?? '(未设)')}`, value: 'model' },
                { name: `Haiku model           ${ansis.dim(profile.fastModel ?? '(未设)')}`, value: 'fastModel' },
                { name: `Sonnet model          ${ansis.dim(profile.sonnetModel ?? '(未设)')}`, value: 'sonnetModel' },
                { name: `Opus model            ${ansis.dim(profile.opusModel ?? '(未设)')}`, value: 'opusModel' },
            ],
        });
        if (fields.length === 0) {
            console.log(ansis.gray('未选任何字段，已取消。\n'));
            return;
        }
        await applyInteractive(profile, fields);
    }
    await writeProfile(profile);
    console.log(ansis.green(`\n✔ Profile ${profile.name} 已更新`));
    // 询问要不要把改动同步到 settings.json
    const state = await readState().catch(() => ({}));
    const isCurrent = state.current === profile.name;
    if (!isCurrent) {
        console.log();
        return;
    }
    const syncIt = opts.yes
        ? true
        : await confirm({
            message: '当前激活的就是这个 profile，立即同步到 settings.json？',
            default: true,
        });
    if (!syncIt) {
        console.log();
        return;
    }
    if (opts.tool === 'codex') {
        console.log(ansis.yellow('  Codex 暂不支持自动同步，请手动改 ~/.codex/config.toml'));
    }
    else if (opts.tool) {
        // 显式单工具
        const meta = TOOLS[opts.tool];
        const settings = await readSettings(meta.settingsFile);
        applyProfileToSettings(settings, profile);
        const backup = await writeSettings(meta.settingsFile, settings);
        console.log(ansis.green(`  ✓ ${meta.displayName} → ${meta.settingsFile}`));
        if (backup)
            console.log(ansis.dim(`    备份: ${backup}`));
    }
    else {
        // 默认：所有已装工具一起同步
        const results = await applyProfileToInstalledTools(profile);
        printEditSyncResults(results);
    }
    console.log(ansis.yellow('  提示: 重启对应工具才能生效'));
    console.log();
}
function printEditSyncResults(results) {
    for (const r of results) {
        const meta = TOOLS[r.tool];
        if (r.skipped) {
            console.log(ansis.dim(`  · ${meta.displayName}: ${r.skipped}（已跳过）`));
            continue;
        }
        console.log(ansis.green(`  ✓ ${meta.displayName} → ${r.settingsFile}`));
        if (r.backup)
            console.log(ansis.dim(`    备份: ${r.backup}`));
    }
}
async function loadTarget(explicitName) {
    const profiles = await listProfiles();
    if (profiles.length === 0) {
        console.log(ansis.yellow('\n还没有 profile。'));
        console.log(ansis.dim('  先运行 `ccjk init` 或 `ccjk quick` 配第一个。\n'));
        return null;
    }
    let target = explicitName;
    if (!target) {
        const state = await readState().catch(() => ({}));
        target = state.current;
        if (!target) {
            console.log(ansis.yellow('\n没有当前 profile。先用 `ccjk use` 选一个，或直接 `ccjk edit <name>`。\n'));
            return null;
        }
    }
    const p = await readProfile(target);
    if (!p) {
        console.log(ansis.red(`\nProfile "${target}" 不存在。`));
        console.log(ansis.dim(`  现有：${profiles.map(x => x.name).join(', ')}\n`));
        return null;
    }
    return p;
}
/**
 * 用 CLI 参数直接覆盖字段。返回 true 表示「至少改了一个字段」，
 * 让上层知道要跳过交互勾选。
 */
async function applyDirectOpts(profile, opts) {
    let changed = false;
    if (opts.baseUrl) {
        profile.baseUrl = opts.baseUrl.trim();
        changed = true;
    }
    if (opts.apiKey) {
        profile.apiKey = opts.apiKey.trim();
        changed = true;
    }
    if (opts.model !== undefined) {
        if (opts.model.trim())
            profile.model = opts.model.trim();
        else
            delete profile.model;
        changed = true;
    }
    if (opts.fastModel !== undefined) {
        if (opts.fastModel.trim())
            profile.fastModel = opts.fastModel.trim();
        else
            delete profile.fastModel;
        changed = true;
    }
    if (opts.sonnetModel !== undefined) {
        if (opts.sonnetModel.trim())
            profile.sonnetModel = opts.sonnetModel.trim();
        else
            delete profile.sonnetModel;
        changed = true;
    }
    if (opts.opusModel !== undefined) {
        if (opts.opusModel.trim())
            profile.opusModel = opts.opusModel.trim();
        else
            delete profile.opusModel;
        changed = true;
    }
    return changed;
}
async function applyInteractive(profile, fields) {
    // 如果 profile 的 provider 有 modelCatalog，model / fastModel 编辑用 select
    const provider = findProvider(profile.provider);
    const catalog = provider?.modelCatalog;
    for (const f of fields) {
        if (f === 'apiKey') {
            const v = await password({
                message: '新 API Key / Auth Token',
                mask: true,
                validate: (s) => s.trim().length > 0 || '不能为空',
            });
            profile.apiKey = v.trim();
        }
        else if (f === 'baseUrl') {
            const v = await input({
                message: '新 Base URL',
                default: profile.baseUrl,
                validate: (s) => {
                    const t = s.trim();
                    if (!t)
                        return '不能为空';
                    if (!/^https?:\/\//i.test(t))
                        return '必须以 http:// 或 https:// 开头';
                    return true;
                },
            });
            profile.baseUrl = v.trim();
        }
        else if (f === 'model') {
            const v = await pickModel('新 Main model', profile.model ?? '', catalog);
            if (v)
                profile.model = v;
            else
                delete profile.model;
        }
        else if (f === 'fastModel') {
            const v = await pickModel('新 Haiku model', profile.fastModel ?? '', catalog);
            if (v)
                profile.fastModel = v;
            else
                delete profile.fastModel;
        }
        else if (f === 'sonnetModel') {
            const v = await pickModel('新 Sonnet model', profile.sonnetModel ?? '', catalog);
            if (v)
                profile.sonnetModel = v;
            else
                delete profile.sonnetModel;
        }
        else if (f === 'opusModel') {
            const v = await pickModel('新 Opus model', profile.opusModel ?? '', catalog);
            if (v)
                profile.opusModel = v;
            else
                delete profile.opusModel;
        }
    }
}
/**
 * 有 catalog 走 select，没有就走 input。空字符串 = 删除该字段。
 */
async function pickModel(message, current, catalog) {
    if (!catalog || catalog.length === 0) {
        const v = await input({ message: `${message}（留空=删除）`, default: current });
        return v.trim();
    }
    const list = current && !catalog.includes(current) ? [current, ...catalog] : [...catalog];
    const CUSTOM = '__custom__';
    const CLEAR = '__clear__';
    const picked = await select({
        message,
        default: current || list[0],
        pageSize: Math.min(10, list.length + 2),
        choices: [
            ...list.map(m => ({ name: m, value: m })),
            { name: ansis.dim('其他… (手动输入)'), value: CUSTOM },
            { name: ansis.gray('删除（不写这个槽位）'), value: CLEAR },
        ],
    });
    if (picked === CLEAR)
        return '';
    if (picked === CUSTOM) {
        const v = await input({ message: '输入模型 ID', default: current });
        return v.trim();
    }
    return picked;
}
//# sourceMappingURL=edit.js.map