/**
 * Sync Command
 *
 * Triggers an immediate sync of a flow or all flows.
 */
import { Args, Command } from "@effect/cli";
import { GhostflowClient, isDaemonError, isDaemonNotRunning } from "@ghostflow/client";
import { Console, Effect, Option, pipe } from "effect";
import { msg } from "../constants/ansi.js";
// ============================================================================
// Arguments
// ============================================================================
const flowIdArg = Args.text({ name: "flowId" }).pipe(Args.withDescription("Flow ID to sync (syncs all flows if not specified)"), Args.optional);
// ============================================================================
// Command Implementation
// ============================================================================
const syncHandler = ({ flowId }) => pipe(Effect.gen(function* () {
    const client = yield* GhostflowClient;
    const targetFlowId = Option.getOrUndefined(flowId);
    const targetDesc = targetFlowId ?? "all flows";
    yield* Console.log(`Syncing ${targetDesc}...`);
    yield* client.sync(targetFlowId);
    yield* Console.log(msg.success(`Sync triggered for ${targetDesc}`));
}), Effect.catchAll((error) => Effect.gen(function* () {
    // Use type guards for proper error handling
    if (isDaemonError(error) && isDaemonNotRunning(error)) {
        yield* Console.error(msg.stopped("Ghostflow daemon is not running"));
        yield* Console.error("");
        yield* Console.error("Start the daemon with: ghostflow start");
    }
    else {
        yield* Console.error(msg.error(String(error)));
    }
    return yield* Effect.fail(error);
})));
// ============================================================================
// Command Definition
// ============================================================================
export const syncCommand = Command.make("sync", { flowId: flowIdArg }).pipe(Command.withHandler(syncHandler), Command.withDescription("Trigger an immediate sync"));
//# sourceMappingURL=sync.js.map