"use client";
import { useCallback, useMemo, useRef, useState } from "react";
import { useStigmer } from "../hooks";
import { parseWorkflowYaml } from "./serialize-workflow-yaml";
/**
 * Behavior hook that persists workflow YAML to the server via `apply()`.
 *
 * Parses the YAML into a `WorkflowInput`, calls `stigmer.workflow.apply()`,
 * and returns success/failure state. Handles UNIMPLEMENTED gracefully
 * (backend not yet deployed) with a descriptive error message.
 *
 * @param org - Organization slug for the target workflow.
 *
 * @since T10 (YAML Editor with Graph Preview)
 */
export function useWorkflowSave(org) {
    const stigmer = useStigmer();
    const [isSaving, setIsSaving] = useState(false);
    const [error, setError] = useState(null);
    const orgRef = useRef(org);
    orgRef.current = org;
    const save = useCallback(async (yaml, options) => {
        setIsSaving(true);
        setError(null);
        try {
            const input = parseWorkflowYaml(yaml, orgRef.current);
            if (options?.versionMessage) {
                input.versionMessage = options.versionMessage;
            }
            await stigmer.workflow.apply(input);
            return true;
        }
        catch (err) {
            const wrapped = err instanceof Error ? err : new Error(String(err));
            if (isUnimplementedError(err)) {
                setError(new Error("Workflow save is not yet available on this server. " +
                    "The workflow was validated locally but could not be persisted."));
            }
            else {
                setError(wrapped);
            }
            return false;
        }
        finally {
            setIsSaving(false);
        }
    }, [stigmer]);
    return useMemo(() => ({ save, isSaving, error }), [save, isSaving, error]);
}
/**
 * Detect gRPC UNIMPLEMENTED (code 12) from a Connect error or
 * a StigmerError wrapping one. The SDK's CODE_MAP doesn't include
 * Unimplemented so it surfaces as "unknown" — check connectCode.
 */
function isUnimplementedError(err) {
    if (!err || typeof err !== "object")
        return false;
    if ("connectCode" in err && err.connectCode === 12) {
        return true;
    }
    const msg = err.message ?? "";
    return msg.includes("[unimplemented]") || msg.includes("UNIMPLEMENTED");
}
//# sourceMappingURL=useWorkflowSave.js.map