"use strict";
/**
 * ls Command - List worktrees with status
 * Issue #518: [DR1-08] Factory pattern with createLsCommand()
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLsCommand = createLsCommand;
const commander_1 = require("commander");
const api_client_1 = require("../utils/api-client");
const command_helpers_1 = require("../utils/command-helpers");
/**
 * Derive display status from worktree flags.
 */
function deriveStatus(wt) {
    if (wt.isWaitingForResponse)
        return 'waiting';
    if (wt.isProcessing)
        return 'running';
    if (wt.isSessionRunning)
        return 'ready';
    return 'idle';
}
/**
 * Format worktrees as a table for terminal display.
 */
function formatTable(worktrees) {
    if (worktrees.length === 0)
        return 'No worktrees found.';
    const headers = ['ID', 'NAME', 'STATUS', 'DEFAULT'];
    const rows = worktrees.map(wt => [
        wt.id,
        wt.name,
        deriveStatus(wt),
        wt.cliToolId || '-',
    ]);
    // Calculate column widths
    const colWidths = headers.map((h, i) => Math.max(h.length, ...rows.map(r => r[i].length)));
    const headerLine = headers.map((h, i) => h.padEnd(colWidths[i])).join('  ');
    const separator = colWidths.map(w => '-'.repeat(w)).join('  ');
    const dataLines = rows.map(r => r.map((cell, i) => cell.padEnd(colWidths[i])).join('  '));
    return [headerLine, separator, ...dataLines].join('\n');
}
/**
 * Format output based on options [DR1-02]
 */
function formatOutput(worktrees, options) {
    if (options.json) {
        return JSON.stringify(worktrees, null, 2);
    }
    if (options.quiet) {
        return worktrees.map(wt => wt.id).join('\n');
    }
    return formatTable(worktrees);
}
/**
 * Create the ls command.
 * [DR1-08] Factory pattern for addCommand() registration.
 */
function createLsCommand() {
    const cmd = new commander_1.Command('ls');
    cmd
        .description('List worktrees with status')
        .option('--json', 'JSON output')
        .option('--quiet', 'IDs only (one per line)')
        .option('--branch <prefix>', 'Filter by branch name prefix')
        .option('--token <token>', command_helpers_1.TOKEN_WARNING)
        .action(async (options) => {
        try {
            const client = new api_client_1.ApiClient({ token: options.token });
            const data = await client.get('/api/worktrees');
            let worktrees = data.worktrees;
            // [DR2-08] Filter by name (not branch) prefix
            if (options.branch) {
                worktrees = worktrees.filter(wt => wt.name.startsWith(options.branch));
            }
            const output = formatOutput(worktrees, options);
            console.log(output);
        }
        catch (error) {
            (0, command_helpers_1.handleCommandError)(error);
        }
    });
    return cmd;
}
