"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.echoPreferences = echoPreferences;
const chalk_1 = __importDefault(require("chalk"));
const preferences_schema_1 = require("./preferences-schema");
const SOURCE_LABELS = {
    explicit: 'flag',
    env: 'env',
    project: 'project',
    user: 'user',
    default: 'default',
    missing: 'missing',
};
/**
 * Print resolved preferences (key, value, source) before a command performs work,
 * so users always see what's about to be applied.
 *
 * Pass only the keys actually consumed by the command — not every preference in the schema —
 * to keep the output focused.
 */
function echoPreferences(resolved, resolver) {
    if (resolved.length === 0)
        return;
    const keyWidth = Math.max(...resolved.map((r) => String(r.key).length));
    const valueWidth = Math.max(...resolved.map((r) => formatValue(r.value).length));
    console.log(chalk_1.default.cyan('\nConfiguration in use:'));
    for (const r of resolved) {
        const def = preferences_schema_1.PREFERENCES[r.key];
        const valStr = formatValue(r.value);
        const sourceStr = formatSource(r);
        console.log(`  ${chalk_1.default.bold(r.key.padEnd(keyWidth))}  ${valStr.padEnd(valueWidth)}  ${sourceStr}  ${chalk_1.default.dim(def.description)}`);
    }
    if (resolved.some((r) => r.source === 'project')) {
        console.log(chalk_1.default.dim(`  ↳ project file: ${resolver.projectFilePath()}`));
    }
    console.log();
}
function formatValue(value) {
    if (value === null || value === undefined)
        return chalk_1.default.yellow('<unset>');
    return String(value);
}
function formatSource(r) {
    const label = SOURCE_LABELS[r.source];
    switch (r.source) {
        case 'explicit':
        case 'env':
            return chalk_1.default.green(`(${label})`);
        case 'project':
            return chalk_1.default.cyan(`(${label})`);
        case 'user':
            return chalk_1.default.blue(`(${label})`);
        case 'default':
            return chalk_1.default.dim(`(${label})`);
        case 'missing':
            return chalk_1.default.red(`(${label})`);
    }
}
