import { createContext, useContext, useEffect } from '@lynx-js/react';
export const OpenUIContext = createContext(null);
/**
 * Access the full OpenUI context. Throws if used outside an
 * `OpenUiRenderer` tree.
 */
export function useOpenUI() {
    const ctx = useContext(OpenUIContext);
    if (!ctx) {
        throw new Error('useOpenUI must be used within a <Renderer /> component.');
    }
    return ctx;
}
/**
 * Get the renderNode function for rendering nested component values.
 */
export function useRenderNode() {
    return useOpenUI().renderNode;
}
/**
 * Get the triggerAction function for firing structured action events.
 *
 * @example
 * ```tsx
 * const triggerAction = useTriggerAction();
 * <button onClick={() => triggerAction("Submit", "myForm")}>
 * ```
 */
export function useTriggerAction() {
    return useOpenUI().triggerAction;
}
/**
 * Whether the LLM is currently streaming content.
 */
export function useIsStreaming() {
    return useOpenUI().isStreaming;
}
/**
 * Whether any Query is currently fetching data.
 */
export function useIsQueryLoading() {
    return useOpenUI().isQueryLoading;
}
/**
 * Get a form field value from the form state context.
 *
 * @example
 * ```tsx
 * const getFieldValue = useGetFieldValue();
 * const name = getFieldValue("contactForm", "name");
 * ```
 */
export function useGetFieldValue() {
    return useOpenUI().getFieldValue;
}
/**
 * Get the setFieldValue function for updating form field values.
 *
 * @example
 * ```tsx
 * const setFieldValue = useSetFieldValue();
 * <input onChange={(e) => setFieldValue("contactForm", "Input", "name", e.target.value, false)} />
 * ```
 */
export function useSetFieldValue() {
    return useOpenUI().setFieldValue;
}
// ─── FormName context ───
export const FormNameContext = createContext(undefined);
/**
 * Get the current form name (set by the nearest parent Form component).
 * Returns undefined if not inside a Form.
 */
export function useFormName() {
    return useContext(FormNameContext);
}
// ─── Default value helper ───
/**
 * Persists a component's default/initial value into form state once streaming
 * finishes — but only if the user hasn't already set a value.
 *
 * Call this inside any form component that has a `defaultValue` or
 * `defaultChecked` prop. It is a no-op during streaming so that LLM
 * prop changes don't fight with partial state.
 *
 * @param shouldTriggerSaveCallback - Defaults to `false` (only local state, no message persistence).
 */
export function useSetDefaultValue({ formName, componentType, name, existingValue, defaultValue, shouldTriggerSaveCallback = false, }) {
    const setFieldValue = useSetFieldValue();
    const isStreaming = useIsStreaming();
    useEffect(() => {
        if (!isStreaming && existingValue === undefined && defaultValue !== undefined) {
            setFieldValue(formName, componentType, name, defaultValue, shouldTriggerSaveCallback);
        }
    }, [
        defaultValue,
        existingValue,
        formName,
        componentType,
        name,
        isStreaming,
        setFieldValue,
        shouldTriggerSaveCallback,
    ]);
}
//# sourceMappingURL=context.jsx.map