"use client";
import { useCallback, useMemo, useRef, useState } from "react";
import { useWorkflowValidation } from "./useWorkflowValidation.js";
import { useWorkflowTopology } from "./useWorkflowTopology.js";
import { useWorkflowSave } from "./useWorkflowSave.js";
import { useTaskKindRegistry } from "./useTaskKindRegistry.js";
/**
 * Behavior hook that orchestrates the workflow YAML editor experience.
 *
 * Composes the validation pipeline, topology computation, and save logic
 * into a single return value that the `WorkflowEditorView` renders.
 *
 * @param initialYaml - The YAML string to initialize the editor with.
 * @param options - Editor configuration (organization slug).
 *
 * @since T10 (YAML Editor with Graph Preview)
 */
export function useWorkflowEditor(initialYaml, options) {
    const [yaml, setYaml] = useState(initialYaml);
    const [versionMessage, setVersionMessage] = useState("");
    const initialRef = useRef(initialYaml);
    const registry = useTaskKindRegistry();
    const { diagnostics } = useWorkflowValidation(yaml, registry);
    const topology = useWorkflowTopology(yaml);
    const { save: applySave, isSaving, error: saveError } = useWorkflowSave(options.org);
    const isDirty = yaml !== initialRef.current;
    const save = useCallback(async () => {
        const saveOpts = versionMessage.trim()
            ? { versionMessage: versionMessage.trim() }
            : undefined;
        const success = await applySave(yaml, saveOpts);
        if (success) {
            initialRef.current = yaml;
            setVersionMessage("");
        }
        return success;
    }, [applySave, yaml, versionMessage]);
    const reset = useCallback(() => {
        setYaml(initialRef.current);
    }, []);
    const errorCount = useMemo(() => diagnostics.filter((d) => d.severity === "error").length, [diagnostics]);
    const warningCount = useMemo(() => diagnostics.filter((d) => d.severity === "warning").length, [diagnostics]);
    return useMemo(() => ({
        yaml,
        setYaml,
        diagnostics,
        isDirty,
        isSaving,
        saveError,
        save,
        reset,
        topology,
        errorCount,
        warningCount,
        versionMessage,
        setVersionMessage,
    }), [yaml, setYaml, diagnostics, isDirty, isSaving, saveError, save, reset, topology, errorCount, warningCount, versionMessage, setVersionMessage]);
}
//# sourceMappingURL=useWorkflowEditor.js.map