import { InvalidArgumentError } from "commander";
import { ACCEPTED_HARNESS_INPUTS } from "../core/config.js";
import { GitService } from "../core/git-service.js";
import { DetectorRegistry } from "../detection/index.js";
import { MigrationRegistry } from "../migrations/index.js";
import { normalizeHarness } from "../core/project-config.js";
import { StrategyRegistry } from "../strategies/index.js";
import { BackupService, DryRunBackupService } from "../transaction/index.js";
import { createPrompter } from "./prompts.js";
/**
 * Resolve `--refresh-templates` into the selection the strategies consume.
 * @param value - Raw commander value: absent, true, or a comma-separated list
 * @returns The selection, or undefined when the flag was not passed
 */
export function parseRefreshTemplates(value) {
    if (value === undefined || value === false)
        return undefined;
    if (value === true)
        return { mode: "all" };
    const paths = value
        .split(",")
        .map(entry => entry.trim())
        .filter(entry => entry.length > 0);
    // `--refresh-templates=` with nothing after it reads as "refresh nothing",
    // which is a footgun either way. Treat it as the bare flag rather than
    // silently matching no path.
    return paths.length > 0 ? { mode: "paths", paths } : { mode: "all" };
}
/**
 * Validate the --harness CLI argument. Commander invokes this with the raw
 * user-supplied string and expects either the parsed value or a thrown
 * InvalidArgumentError.
 * @param value - Raw argument value
 * @returns The validated harness
 */
export function parseHarnessArg(value) {
    const normalized = normalizeHarness(value);
    if (normalized === undefined) {
        const allowed = ACCEPTED_HARNESS_INPUTS.join(" | ");
        throw new InvalidArgumentError(`expected ${allowed}, got ${JSON.stringify(value)}`);
    }
    return normalized;
}
/**
 * Register the apply-flow flags shared by every entry point that applies Lisa
 * to a project. Mutates and returns the command for chaining.
 * @param command - Command to decorate with shared options
 * @returns The same command instance
 */
export function addSharedOptions(command) {
    return command
        .option("-n, --dry-run", "Show what would be done without making changes")
        .option("-y, --yes", "Non-interactive mode (auto-accept defaults, overwrite on conflict)")
        .option("-v, --validate", "Validate project compatibility without applying changes")
        .option("--skip-git-check", "Skip dirty git working directory check (for postinstall use)")
        .option("--refresh-templates [paths]", "Let a non-interactive apply replace managed files it would otherwise report as out of date. " +
        "Optionally scope to a comma-separated list of repo-relative paths " +
        "(e.g. --refresh-templates=scripts/lisa-hooks). Backs up before replacing.")
        .option("--harness <harness>", `Target harness for emitted artifacts: ${ACCEPTED_HARNESS_INPUTS.join(" | ")} (default: claude, or value from .lisa.config.json)`, parseHarnessArg);
}
/**
 * Create Lisa dependencies based on options
 * @param dryRun - Whether in dry run mode
 * @param yesMode - Whether in non-interactive mode
 * @param logger - Logger instance
 * @returns Dependencies for Lisa
 */
export function createDependencies(dryRun, yesMode, logger) {
    return {
        logger,
        prompter: createPrompter(yesMode),
        backupService: dryRun
            ? new DryRunBackupService()
            : new BackupService(logger),
        detectorRegistry: new DetectorRegistry(),
        strategyRegistry: new StrategyRegistry(),
        gitService: new GitService(),
        migrationRegistry: new MigrationRegistry(),
    };
}
//# sourceMappingURL=shared-options.js.map