import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * Confirm Dialog Screen
 *
 * Modal dialog for confirming destructive actions.
 * Performs the action via SDK when confirmed.
 */
import { useRemoveFlow } from "@ghostflow/ui-core";
import { Array as Arr, Match, pipe } from "effect";
import { Box, Text, useInput } from "ink";
import { useCallback, useState } from "react";
import { CONFIRM_HINTS, KeyHints } from "../components/KeyHints.js";
import { useSDK } from "../contexts/SDKContext.js";
// ============================================================================
// Constants
// ============================================================================
/** Confirm dialog title */
const DIALOG_TITLE = "Confirm Action";
/** Remove action confirmation question */
const REMOVE_QUESTION = "Are you sure you want to remove this flow?";
/** Remove action explanation */
const REMOVE_EXPLANATION = "This will stop syncing but won't delete any files.";
/** Confirm key bindings */
const KEYS_CONFIRM = ["y", "Y"];
const KEYS_CANCEL = ["n", "N"];
// ============================================================================
// Component
// ============================================================================
/** Status text shown during removal */
const REMOVING_TEXT = "Removing...";
/** Error text prefix */
const ERROR_TEXT = "Error: ";
/**
 * Confirmation dialog for destructive actions.
 * Performs the action via SDK when confirmed.
 */
export const ConfirmDialog = ({ action, send }) => {
    const sdk = useSDK();
    const [error, setError] = useState(undefined);
    // Create remove mutation with SDK callback
    const removeFlow = useRemoveFlow({
        onRemove: useCallback(async (flowId) => {
            const result = await sdk.config.removeFlow(flowId);
            // Handle partial failure case
            if (result._tag === "ConfigWrittenReloadFailed") {
                // Config was removed but daemon reload failed
                // This is still a "success" from user perspective - flow is removed
                console.warn("[ghostflow] Reload failed after remove:", result.reloadError);
            }
        }, [sdk])
    });
    const isConfirmKey = (input) => Arr.contains(KEYS_CONFIRM, input);
    const isCancelKey = (input) => Arr.contains(KEYS_CANCEL, input);
    const handleConfirm = useCallback(() => {
        if (action?.type === "remove") {
            setError(undefined);
            removeFlow.mutate(action.flowId, {
                onSuccess: () => send({ type: "CONFIRM" }),
                onError: (err) => setError(err.message)
            });
        }
    }, [action, removeFlow, send]);
    const handleCancel = useCallback(() => {
        send({ type: "CANCEL" });
    }, [send]);
    useInput((input, key) => pipe(Match.value({ input, escape: key.escape }), Match.when(({ input }) => isConfirmKey(input), handleConfirm), Match.when(({ input }) => isCancelKey(input), handleCancel), Match.when({ escape: true }, handleCancel), Match.orElse(() => { })));
    if (action === undefined) {
        return null;
    }
    return (_jsxs(Box, { flexDirection: "column", padding: 1, children: [_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsx(Text, { bold: true, color: "yellow", children: DIALOG_TITLE }) }), _jsx(Box, { flexDirection: "column", marginBottom: 1, children: action.type === "remove" && (_jsxs(_Fragment, { children: [_jsx(Text, { children: REMOVE_QUESTION }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { bold: true, children: action.flowName }) }), _jsx(Text, { dimColor: true, children: REMOVE_EXPLANATION })] })) }), removeFlow.isPending && (_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: "cyan", children: REMOVING_TEXT }) })), error !== undefined && (_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { color: "red", children: [ERROR_TEXT, error] }) })), _jsx(KeyHints, { hints: CONFIRM_HINTS })] }));
};
//# sourceMappingURL=ConfirmDialog.js.map