'use client';
import { jsx as _jsx } from "react/jsx-runtime";
/**
 * Context and Provider for the validation rule editor.
 * Thin wrapper around UI state + data hook.
 */
import { createContext, useContext, useState, useMemo, useEffect } from 'react';
import { use_validation_rules } from './hooks/use_validation_rules.js';
const ValidationRuleEditorContext = createContext(null);
export function ValidationRuleEditorProvider({ children, api_base_url, table_name, scope_id, prompt_config: custom_prompt_config, form_field_variables, read_only = false, available_document_types: custom_document_types, available_response_sets: custom_response_sets, include_globals, on_clone_to_override, }) {
    const rules_data = use_validation_rules({
        api_base_url,
        table_name,
        scope_id,
        include_globals,
        on_clone_to_override,
    });
    const [selected_id, set_selected_id] = useState(null);
    const [type_filter, set_type_filter] = useState(null);
    const [search_query, set_search_query] = useState('');
    const [active_tab, set_active_tab] = useState('all');
    // Merge form-field variables into the prompt config so they appear in the @-popup
    // alongside session-level variables. Form-field variables are typically supplied by
    // the consuming app from its template's date/text fields.
    const prompt_config = useMemo(() => {
        const base = custom_prompt_config ?? { commands: [], variables: [] };
        if (!form_field_variables?.length)
            return base;
        // De-dupe by trigger; consumer-provided form fields win on collision.
        const merged_variables = [
            ...base.variables.filter(v => !form_field_variables.some(f => f.trigger === v.trigger)),
            ...form_field_variables,
        ];
        return { commands: base.commands, variables: merged_variables };
    }, [custom_prompt_config, form_field_variables]);
    // Available document types for dropdown
    const available_document_types = useMemo(() => custom_document_types ?? [], [custom_document_types]);
    // Available response sets for clarification dropdown
    const available_response_sets = useMemo(() => custom_response_sets ?? [], [custom_response_sets]);
    // Filter rules based on active filters
    const filtered_rules = useMemo(() => {
        let result = rules_data.rules;
        // Tab filter
        if (active_tab === 'enabled') {
            result = result.filter(r => r.enabled);
        }
        else if (active_tab === 'disabled') {
            result = result.filter(r => !r.enabled);
        }
        // Document type filter
        if (type_filter) {
            result = result.filter(r => r.document_type === type_filter);
        }
        // Search filter
        if (search_query.trim()) {
            const q = search_query.toLowerCase();
            result = result.filter(r => r.name.toLowerCase().includes(q) ||
                r.document_type.toLowerCase().includes(q) ||
                (r.description?.toLowerCase().includes(q) ?? false));
        }
        // Sort by document_type then priority then name
        return result.sort((a, b) => {
            const type_cmp = a.document_type.localeCompare(b.document_type);
            if (type_cmp !== 0)
                return type_cmp;
            const pri_cmp = (a.priority ?? 100) - (b.priority ?? 100);
            if (pri_cmp !== 0)
                return pri_cmp;
            return a.name.localeCompare(b.name);
        });
    }, [rules_data.rules, active_tab, type_filter, search_query]);
    // Fetch rules on mount
    useEffect(() => {
        rules_data.fetch_rules();
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    const value = useMemo(() => ({
        rules_data,
        selected_id,
        set_selected_id,
        type_filter,
        set_type_filter,
        search_query,
        set_search_query,
        active_tab,
        set_active_tab,
        filtered_rules,
        prompt_config,
        read_only,
        available_document_types,
        available_response_sets,
    }), [
        rules_data, selected_id, type_filter, search_query,
        active_tab, filtered_rules, prompt_config, read_only,
        available_document_types, available_response_sets,
    ]);
    return (_jsx(ValidationRuleEditorContext.Provider, { value: value, children: children }));
}
export function useValidationRuleEditor() {
    const ctx = useContext(ValidationRuleEditorContext);
    if (!ctx) {
        throw new Error('useValidationRuleEditor must be used within a ValidationRuleEditorProvider');
    }
    return ctx;
}
//# sourceMappingURL=context.js.map