import { existsSync } from 'node:fs';
import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';
export const BUILTIN_WORKFLOWS = {
    'starter': {
        id: 'starter',
        name: '新机首配',
        description: '装机后跑一遍：配 API → 设权限档位 → 装状态栏',
        steps: [
            { label: '配置 API（init）', action: { type: 'init' } },
            { label: '设权限档位 standard', action: { type: 'perms', tier: 'standard' } },
            { label: '安装状态栏', action: { type: 'statusline-install' } },
            { label: '装常用 MCP（可选）', action: { type: 'mcp' }, optional: true },
        ],
    },
    'team-import': {
        id: 'team-import',
        name: '团队配置导入',
        description: '从 profile 包导入 → 切到合适档位 → 体检自动修',
        steps: [
            { label: '导入 profile 包', action: { type: 'profile-import' } },
            { label: '切换到导入的 profile', action: { type: 'use' } },
            { label: '设权限档位 standard', action: { type: 'perms', tier: 'standard' } },
            { label: '体检 + 自动修', action: { type: 'doctor-fix' } },
        ],
    },
    'reset-soft': {
        id: 'reset-soft',
        name: '回到低打扰状态',
        description: '卸状态栏 + 切到 safe 档位 — 适合让步给同事用你的机器',
        steps: [
            { label: '卸载状态栏', action: { type: 'statusline-uninstall' } },
            { label: '切到 safe 权限档位', action: { type: 'perms', tier: 'safe' } },
        ],
    },
    'dev-ready': {
        id: 'dev-ready',
        name: '日常开发就绪',
        description: '切 code 模式 + perms standard + 装 git 模板 + 体检',
        steps: [
            { label: '切到 code 对话模式', action: { type: 'mode-use', mode: 'code' } },
            { label: '设权限档位 standard', action: { type: 'perms', tier: 'standard' } },
            { label: '装 git slash 命令', action: { type: 'git-install' } },
            { label: '体检 + 自动修', action: { type: 'doctor-fix' } },
        ],
    },
};
export const WORKFLOWS_DIR = join(homedir(), '.ccjk', 'workflows');
export async function listWorkflows(dir = WORKFLOWS_DIR) {
    const out = new Map();
    for (const w of Object.values(BUILTIN_WORKFLOWS))
        out.set(w.id, w);
    if (existsSync(dir)) {
        const files = await readdir(dir);
        for (const f of files) {
            if (!f.endsWith('.json'))
                continue;
            try {
                const raw = await readFile(join(dir, f), 'utf-8');
                const def = JSON.parse(raw);
                out.set(def.id, def);
            }
            catch { /* 跳过损坏 */ }
        }
    }
    return [...out.values()].sort((a, b) => a.id.localeCompare(b.id));
}
export async function readWorkflow(id, dir = WORKFLOWS_DIR) {
    if (existsSync(join(dir, `${id}.json`))) {
        const raw = await readFile(join(dir, `${id}.json`), 'utf-8');
        return JSON.parse(raw);
    }
    return BUILTIN_WORKFLOWS[id] ?? null;
}
export async function writeWorkflow(w, dir = WORKFLOWS_DIR) {
    await mkdir(dir, { recursive: true });
    await writeFile(join(dir, `${w.id}.json`), `${JSON.stringify(w, null, 2)}\n`, 'utf-8');
}
//# sourceMappingURL=workflows.js.map