import { useReactFlow } from "../../../xyflow/react.js";
import { useCallback, useMemo, useState } from "react";
import { nodeSchemas } from "../schemas.js";
function useNodeConfiguration(selectedNode, customSchemas = {}, enableValidation = false, onChange) {
    const { updateNodeData } = useReactFlow();
    const [errors, setErrors] = useState({});
    const schema = useMemo(()=>{
        if (!selectedNode) return null;
        if (selectedNode.data.configSchema) return selectedNode.data.configSchema;
        const allSchemas = {
            ...nodeSchemas,
            ...customSchemas
        };
        const nodeType = selectedNode.type || 'default';
        return allSchemas[nodeType] || allSchemas.default;
    }, [
        selectedNode,
        customSchemas
    ]);
    const handleFieldChange = useCallback((fieldKey, value)=>{
        if (!selectedNode?.id) return;
        updateNodeData(selectedNode.id, {
            [fieldKey]: value
        });
        if (onChange) onChange(selectedNode.id, fieldKey, value);
        if (enableValidation) setErrors((prev)=>({
                ...prev,
                [fieldKey]: ''
            }));
    }, [
        selectedNode?.id,
        updateNodeData,
        onChange,
        enableValidation
    ]);
    return {
        schema,
        errors,
        setErrors,
        handleFieldChange
    };
}
export { useNodeConfiguration };
