/**
 * commands/wfrun.mjs — C17 declarative-workflow CLI entry.
 *
 * Usage:
 *   apt-tools wfrun validate <workflow.yaml>
 *   apt-tools wfrun plan <workflow.yaml> [--input k=v] …
 *   apt-tools wfrun run  <workflow.yaml> [--input k=v] … [--project-dir <path>]
 *
 * Shape mirrors cmdWorkflow (C29 phase-enforcement): (subcommand, positional,
 * extraArgs). The second positional in the dispatch table (bin/apt-tools.mjs)
 * is the workflow YAML path — not a project dir. The project dir for
 * .aperant/wfrun/** writes defaults to process.cwd() and can be overridden
 * via the --project-dir flag.
 *
 * This command does NOT overload the existing `workflow` command — see
 * docs/workflows.md for the naming-collision note.
 */
import { resolve } from 'node:path';
import { parseFlags } from '../util/args.mjs';
import { err, exitWith, ok } from '../util/result.mjs';
import { parseInputFlags, runWfrun } from '../wfrun/run.mjs';
export async function cmdWfrun(subcommand, workflowPath, extraArgs = []) {
    if (!subcommand) {
        return err('Usage: apt-tools wfrun <validate|plan|run> <workflow.yaml> [--input k=v …]');
    }
    if (!['validate', 'plan', 'run'].includes(subcommand)) {
        return err(`Unknown wfrun subcommand: ${subcommand}. Expected: validate, plan, run`);
    }
    if (!workflowPath) {
        return err(`Usage: apt-tools wfrun ${subcommand} <workflow.yaml> [--input k=v …]`);
    }
    const inputRes = parseInputFlags(extraArgs);
    if (!inputRes.ok) {
        return exitWith({
            status: 'error',
            command: `wfrun-${subcommand}`,
            errors: inputRes.errors.map((m) => ({ path: '--input', message: m })),
        }, 1);
    }
    const flags = parseFlags(extraArgs.filter((a, i, arr) => a !== '--input' && arr[i - 1] !== '--input'));
    const projectDirFlag = flags.get('project-dir');
    const projectRoot = typeof projectDirFlag === 'string' ? resolve(projectDirFlag) : process.cwd();
    const res = await runWfrun({
        subcommand,
        workflowPath,
        projectDir: projectRoot,
        inputs: inputRes.inputs,
    });
    if (!res.ok) {
        return exitWith({
            status: 'error',
            command: `wfrun-${subcommand}`,
            errors: res.errors,
        }, 1);
    }
    return ok(res.result);
}
//# sourceMappingURL=wfrun.mjs.map