/**
 * List Command
 *
 * Lists all flows (repositories) from configuration.
 * Works without the daemon running by reading config directly.
 */
import { Command, Options } from "@effect/cli";
import { ConfigManager, isConfigStorageError } from "@ghostflow/client";
import { Console, Effect, pipe } from "effect";
import { msg } from "../constants/ansi.js";
import { formatFlowConfigPaths, formatFlowConfigs, formatFlowConfigsCompact } from "../formatters/flows.js";
// ============================================================================
// Options
// ============================================================================
const jsonOption = Options.boolean("json").pipe(Options.withAlias("j"), Options.withDescription("Output flows as JSON"), Options.withDefault(false));
const compactOption = Options.boolean("compact").pipe(Options.withAlias("c"), Options.withDescription("Output flows in compact table format"), Options.withDefault(false));
const pathsOption = Options.boolean("paths").pipe(Options.withAlias("p"), Options.withDescription("Output only flow paths (for shell scripts)"), Options.withDefault(false));
// ============================================================================
// Command Implementation
// ============================================================================
const listHandler = ({ compact, json, paths }) => pipe(Effect.gen(function* () {
    const configManager = yield* ConfigManager;
    const flows = yield* configManager.listFlows();
    if (json) {
        yield* Console.log(JSON.stringify(flows, null, 2));
    }
    else if (paths) {
        // Only output if there are flows (avoid printing empty line)
        if (flows.length > 0) {
            yield* Console.log(formatFlowConfigPaths(flows));
        }
    }
    else if (compact) {
        yield* Console.log(formatFlowConfigsCompact(flows));
    }
    else {
        yield* Console.log(formatFlowConfigs(flows));
    }
}), Effect.catchAll((error) => Effect.gen(function* () {
    if (isConfigStorageError(error)) {
        yield* Console.error(msg.error(`Failed to read configuration: ${error.message}`));
    }
    else {
        yield* Console.error(msg.error(String(error)));
    }
    return yield* Effect.fail(error);
})));
// ============================================================================
// Command Definition
// ============================================================================
export const listCommand = Command.make("list", {
    json: jsonOption,
    compact: compactOption,
    paths: pathsOption
}).pipe(Command.withHandler(listHandler), Command.withDescription("List all flows (synced repositories)"));
//# sourceMappingURL=list.js.map