/**
 * commands/profile.mjs — `apt-tools profile <list|resolve|validate>` (C19).
 *
 * Mirrors commands/constitution.mjs: subcommand dispatch + JSON envelope via
 * util/io.mjs:output + util/args.mjs:parseFlags. Read-only for v1 — no write
 * subcommands.
 *
 *   apt-tools profile list <project-dir>
 *     → { status, command: 'profile-list', count, profiles: [...] }
 *
 *   apt-tools profile validate <project-dir> --name <n>
 *     → { status: 'ok'|'fail', command: 'profile-validate', name, chain?, reason?, errors? }
 *
 *   apt-tools profile resolve <project-dir> --name <n>
 *     → { status: 'ok'|'fail', command: 'profile-resolve', name, chain?, resolved?, reason?, errors? }
 */
import { listProfiles } from '../profile/parse.mjs';
import { resolveProfile } from '../profile/resolve.mjs';
import { parseFlags } from '../util/args.mjs';
import { err, ok, UsageError } from '../util/result.mjs';
/**
 * Sentinel error class for helper-level usage failures inside cmdProfile.
 * The dispatcher catches it and converts it into the standard err() envelope.
 */
class ProfileUsageError extends UsageError {
    constructor(message) {
        super(message);
        this.name = 'ProfileUsageError';
    }
}
export async function cmdProfile(subcommand, projectDir, extraArgs) {
    if (!subcommand) {
        return err('Usage: apt-tools profile <list|resolve|validate> [project-dir] [flags]');
    }
    try {
        switch (subcommand) {
            case 'list':
                return cmdProfileList(projectDir, extraArgs ?? []);
            case 'validate':
                return cmdProfileValidate(projectDir, extraArgs ?? []);
            case 'resolve':
                return cmdProfileResolve(projectDir, extraArgs ?? []);
            default:
                return err(`Unknown profile subcommand: ${subcommand}. Expected: list, validate, resolve`);
        }
    }
    catch (e) {
        if (e instanceof ProfileUsageError)
            return err(e.message);
        throw e;
    }
}
function cmdProfileList(projectDir, _extraArgs) {
    if (!projectDir)
        throw new ProfileUsageError('Usage: apt-tools profile list <project-dir>');
    const profiles = listProfiles(projectDir);
    return ok({
        status: 'ok',
        command: 'profile-list',
        count: profiles.length,
        profiles,
    });
}
function cmdProfileValidate(projectDir, extraArgs) {
    if (!projectDir)
        throw new ProfileUsageError('Usage: apt-tools profile validate <project-dir> --name <n>');
    const flags = parseFlags(extraArgs);
    const name = flags.get('name');
    if (!name || typeof name !== 'string')
        throw new ProfileUsageError('Required: --name <profile-name>');
    // validate = resolve + discard the merged output. Cycle / depth / dangling
    // extends / schema failures all surface through resolveProfile's failure
    // shape, so we share one code path for both `validate` and `resolve`.
    const result = resolveProfile(projectDir, name);
    if (!result.ok) {
        return ok({
            status: 'fail',
            command: 'profile-validate',
            name,
            reason: result.reason,
            errors: result.errors,
            chain: result.chain,
        });
    }
    return ok({
        status: 'ok',
        command: 'profile-validate',
        name,
        chain: result.chain,
    });
}
function cmdProfileResolve(projectDir, extraArgs) {
    if (!projectDir)
        throw new ProfileUsageError('Usage: apt-tools profile resolve <project-dir> --name <n>');
    const flags = parseFlags(extraArgs);
    const name = flags.get('name');
    if (!name || typeof name !== 'string')
        throw new ProfileUsageError('Required: --name <profile-name>');
    const result = resolveProfile(projectDir, name);
    if (!result.ok) {
        return ok({
            status: 'fail',
            command: 'profile-resolve',
            name,
            reason: result.reason,
            errors: result.errors,
            chain: result.chain,
        });
    }
    return ok({
        status: 'ok',
        command: 'profile-resolve',
        name,
        chain: result.chain,
        resolved: result.resolved,
    });
}
//# sourceMappingURL=profile.mjs.map