/**
 * olam repos — manage the global repo registry (~/.olam/config.json).
 *
 *   olam repos list [--json]
 *   olam repos add --name <n> --path <p> [--description <d>] [--default-branch <b>]
 *   olam repos remove <name>
 *   olam repos update <name> [--path <p>] [--description <d>] [--default-branch <b>]
 */
import pc from 'picocolors';
import { listRepos, addRepo, removeRepo, updateRepo, } from '@olam/core/src/global-config/index.js';
import { printError, printSuccess, printHeader } from '../output.js';
import { renderReposListJson } from './repos-list-json.js';
function asMessage(err) {
    return err instanceof Error ? err.message : String(err);
}
export function registerRepos(program) {
    const repos = program
        .command('repos')
        .description('Manage the global repo registry');
    repos
        .command('list')
        .description('List all registered repos')
        .option('--json', 'Emit a machine-readable JSON object instead of the human table', false)
        .action((opts) => {
        const all = listRepos();
        // --json: stable, scriptable contract. Always an object with a `repos` array
        // (empty when no repos registered). Consumers gate on `count`; drivers
        // iterate `repos[].name` + `repos[].path` to pick a repo for `--repo` flags.
        // url credentials are redacted before emission (`:***@` substitution).
        if (opts.json) {
            process.stdout.write(renderReposListJson(all) + '\n');
            return;
        }
        if (all.length === 0) {
            console.log(pc.dim('0 repo(s) registered. Add one with: olam repos add --name <n> --path <p>'));
            return;
        }
        printHeader(`${all.length} repo(s)`);
        for (const r of all) {
            const when = new Date(r.addedAt).toISOString().slice(0, 10);
            console.log(`  ${pc.bold(r.name.padEnd(24))} ${r.path.padEnd(48)} ${pc.dim(when)}`);
        }
    });
    repos
        .command('add')
        .description('Register a local repo path')
        .requiredOption('--name <name>', 'Repo name (lowercase, digits, dash)')
        .requiredOption('--path <path>', 'Absolute or ~/... path to the local repo')
        .option('--description <desc>', 'Optional human-readable description')
        .option('--default-branch <branch>', 'Default branch name (e.g. main)')
        .action((opts) => {
        try {
            const entry = addRepo({
                name: opts.name,
                path: opts.path,
                description: opts.description,
                defaultBranch: opts.defaultBranch,
            });
            printSuccess(`registered repo "${entry.name}" at ${entry.path}`);
        }
        catch (err) {
            printError(asMessage(err));
            process.exitCode = 1;
        }
    });
    repos
        .command('remove')
        .description('Remove a repo from the registry')
        .argument('<name>', 'Repo name to remove')
        .action((name) => {
        try {
            removeRepo(name);
            printSuccess(`removed repo "${name}"`);
        }
        catch (err) {
            printError(asMessage(err));
            process.exitCode = 1;
        }
    });
    repos
        .command('update')
        .description('Update a registered repo')
        .argument('<name>', 'Repo name to update')
        .option('--path <path>', 'New path')
        .option('--description <desc>', 'New description')
        .option('--default-branch <branch>', 'New default branch')
        .action((name, opts) => {
        try {
            const entry = updateRepo(name, {
                path: opts.path,
                description: opts.description,
                defaultBranch: opts.defaultBranch,
            });
            printSuccess(`updated repo "${entry.name}"`);
        }
        catch (err) {
            printError(asMessage(err));
            process.exitCode = 1;
        }
    });
}
//# sourceMappingURL=repos.js.map