// engine/autopilot/config-loader.ts — SPEC-470: load planu/autopilot.yml or use defaults
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { parse as parseYaml } from 'yaml';
import { AutopilotYmlConfigSchema } from '../../types/index.js';
import { DEFAULT_AUTOPILOT_CONFIG } from './default-config.js';
/** Load planu/autopilot.yml or return defaults. */
export async function loadAutopilotConfig(projectPath) {
    try {
        const configPath = join(projectPath, 'planu', 'autopilot.yml');
        const content = await readFile(configPath, 'utf-8');
        const raw = parseYaml(content);
        const parsed = AutopilotYmlConfigSchema.safeParse(raw);
        if (!parsed.success) {
            console.error('[planu] autopilot.yml invalid:', parsed.error.message);
            return DEFAULT_AUTOPILOT_CONFIG;
        }
        return mergeWithDefaults(parsed.data);
    }
    catch {
        return DEFAULT_AUTOPILOT_CONFIG;
    }
}
function mergeWithDefaults(userConfig) {
    // userConfig.enabled is always boolean after Zod parse (schema has .default(true))
    return {
        version: 1,
        enabled: userConfig.enabled,
        on: { ...DEFAULT_AUTOPILOT_CONFIG.on, ...userConfig.on },
    };
}
export function parseTimeout(s) {
    if (!s) {
        return 10_000;
    }
    const match = /^(\d+)(ms|s|m)$/.exec(s);
    if (!match) {
        return 10_000;
    }
    // Groups 1 and 2 are always present when match succeeds (non-optional in regex)
    // match[1] and match[2] are always defined when regex succeeds (non-optional groups)
    /* v8 ignore next 2 */
    const n = parseInt(match[1] ?? '10', 10);
    /* v8 ignore next 1 */
    const unit = match[2] ?? 's';
    return unit === 'ms' ? n : unit === 's' ? n * 1000 : n * 60_000;
}
//# sourceMappingURL=config-loader.js.map