/**
 * Shared hook that extracts ALL common setup logic from form field components.
 *
 * Every form component (inputbox, textarea, checkbox, combo, radio, date)
 * duplicates ~200 lines of identical setup: file state, validation,
 * chat hook, visibility check, disabled state, additional entry logic,
 * error merging, data_ok handler, and imperative handle.
 *
 * This hook consolidates all of that into a single reusable hook.
 */
'use client';
import React from 'react';
import { use_collab_form_field, } from '../hazo_collab_form_base.js';
import { DEFAULTS } from '../../config/defaults.js';
import { useFormViewContextOptional } from '../hazo_collab_form_view/context.js';
/**
 * Hook that consolidates all common form field component setup.
 *
 * @param props - The full component props (extends CollabFormFieldBaseProps)
 * @param ref - The forwarded ref
 * @param element_type_name - Used for display name / typing context only
 */
export function use_base_form_field(props, ref) {
    // Create internal ref for component reference (for file_processor)
    const component_ref = React.useRef(null);
    const [internal_files, set_internal_files] = React.useState([]);
    const [validation_error, set_validation_error] = React.useState(null);
    // Use controlled files if provided, otherwise use internal state
    const current_files = props.files !== undefined ? props.files : internal_files;
    // Expose get_file_data method and forward ref
    React.useImperativeHandle(ref, () => {
        return {
            ...component_ref.current,
            get_file_data: () => current_files,
        };
    }, [current_files]);
    // Destructure commonly-used props with defaults
    const { label, tooltip, ihelp_context_key, error, field_id, field_data_id, field_name, on_chat_click, chat_status, is_chat_active, chat_background_color = DEFAULTS.chat_background_color, is_data_ok_default, container_class_name, confirm_state, label_class_name, label_actions, error_class_name, field_width_class_name, required, data_ok_checked, on_data_ok_change, data_ok_mode, data_ok_value, on_data_ok_value_change, data_ok_hidden_states, data_ok_editable, disable_data_ok, disable_chat, accept_files_public, accept_files_private, files_dir, default_file_accordion_open, max_size, min_files, max_files, file_accept, file_processor, file_validator, file_manager, files: controlled_files_prop, on_files_change, enable_notes, disable_notes, notes, on_notes_change, notes_panel_style, notes_background_color, notes_save_mode, current_user, className, id, disabled, 
    // HazoChat props
    hazo_chat_group_id, hazo_chat_reference_id, hazo_chat_reference_type, hazo_chat_api_base_url, hazo_chat_timezone, hazo_chat_title, hazo_chat_subtitle, hazo_chat_realtime_mode, hazo_chat_polling_interval, hazo_chat_messages_per_page, hazo_chat_class_name, hazo_chat_on_close, hazo_chat_is_open, hazo_chat_on_open_change, hazo_chat_show_sidebar_toggle, hazo_chat_show_delete_button, hazo_chat_bubble_radius, hazo_chat_read_only, 
    // Reference tag props
    reference_data: reference_data_prop, reference_value, reference_label, reference_tag_background_color, on_reference_delete, 
    // Visibility toggle props
    enable_visibility_toggle, disable_visibility_toggle, visibility = 'visible', on_visibility_change, user_role = 'responder', 
    // Additional entry props
    additional_entry, max_additional_entries, entry_index, entry_count, on_add_entry, on_delete_entry, is_additional_entry, 
    // Delete field props
    enable_delete, disable_delete, on_delete, 
    // Lock field props
    enable_lock, disable_lock, locked = false, on_lock_change, 
    // Skip/N/A field props
    enable_skip, disable_skip, skipped = false, on_skip_change, 
    // Complete checkbox props
    complete, on_complete_change, 
    // Action buttons styling
    action_buttons_borderless, 
    // Private file props
    private_files_permission, can_access_private_files, private_files, on_private_files_change, private_files_class_name, 
    // File validation state
    validating_file_ids, 
    // File error state
    error_file_ids, passed_file_ids, error_file_details, 
    // File clarification state
    clarification_items, clarification_callbacks, 
    // Icons behaviour
    icons_behaviour, active_role, 
    // Clarification props
    clarification_count, has_clarification, on_clarification_click, } = props;
    // Chat setup hook
    const { field_id_final, handle_chat_icon_click, handle_chat_close, chat_is_open, is_chat_disabled } = use_collab_form_field({
        label,
        field_id,
        id,
        field_data_id,
        field_name,
        on_chat_click,
        hazo_chat_group_id,
        hazo_chat_on_open_change,
        hazo_chat_is_open,
        hazo_chat_on_close,
    });
    // Visibility check
    const is_hidden = visibility === 'hidden';
    const should_hide = is_hidden && user_role === 'responder';
    // Disabled state
    const is_chat_field_disabled = is_chat_active !== undefined ? is_chat_active : chat_is_open;
    const is_field_disabled = !!disabled || is_chat_field_disabled || locked || skipped;
    const is_chat_effectively_disabled = is_chat_disabled || is_hidden;
    // Additional entry logic
    const show_add_entry = !!(additional_entry && on_add_entry && (entry_count === undefined || entry_index === entry_count - 1) && (entry_count === undefined || entry_count < (max_additional_entries ?? DEFAULTS.max_additional_entries)));
    // Combined error
    const display_error = error || validation_error || undefined;
    // Required-field highlight: reads the opt-in flag from FormViewContext.
    // Works standalone (outside any FormViewProvider) — flag defaults to false.
    const highlight_required = useFormViewContextOptional()?.settings?.highlight_empty_required ?? false;
    const is_value_empty = props.value === '' || props.value == null || props.checked === false;
    const show_required_highlight = highlight_required && ((!!required && is_value_empty) ||
        !!display_error);
    // Count files with validation errors for this field
    const file_error_count = React.useMemo(() => {
        if (!error_file_ids || error_file_ids.size === 0)
            return 0;
        let count = 0;
        for (const f of current_files) {
            if (error_file_ids.has(f.file_id))
                count++;
        }
        if (private_files) {
            for (const f of private_files) {
                if (error_file_ids.has(f.file_id))
                    count++;
            }
        }
        return count;
    }, [error_file_ids, current_files, private_files]);
    // Unified data_ok handler
    const handle_data_ok_unified_change = React.useCallback((value) => {
        if (typeof value === 'boolean') {
            on_data_ok_change?.(value);
        }
        else {
            on_data_ok_value_change?.(value);
        }
    }, [on_data_ok_change, on_data_ok_value_change]);
    // Stable files change handler
    const handle_files_change = React.useCallback((new_files) => {
        if (controlled_files_prop === undefined) {
            set_internal_files(new_files);
        }
        if (on_files_change) {
            on_files_change(new_files);
        }
    }, [controlled_files_prop, on_files_change]);
    // Effective field identifier
    const effective_field_id = field_data_id || field_id_final;
    // === Build sub-component prop groups ===
    const container_props = {
        chat_status,
        is_chat_active,
        chat_background_color,
        is_data_ok_default,
        container_class_name,
        confirm_state,
        hazo_chat_is_open: chat_is_open,
        hazo_chat_group_id,
        hazo_chat_reference_id,
        hazo_chat_reference_type,
        hazo_chat_api_base_url,
        hazo_chat_timezone,
        hazo_chat_title,
        hazo_chat_subtitle,
        hazo_chat_realtime_mode,
        hazo_chat_polling_interval,
        hazo_chat_messages_per_page,
        hazo_chat_class_name,
        hazo_chat_on_close: handle_chat_close,
        hazo_chat_show_sidebar_toggle,
        hazo_chat_show_delete_button,
        hazo_chat_bubble_radius,
        field_data_id,
        field_name,
        label,
        files: current_files,
    };
    const label_props = {
        field_id_final,
        label,
        tooltip,
        ihelp_context_key,
        label_class_name,
        required,
        visibility,
        locked,
        label_actions,
        clarification_count,
        has_clarification,
        file_error_count,
        on_file_error_click: file_error_count > 0 ? on_clarification_click : undefined,
    };
    const controls_props = {
        field_id: effective_field_id,
        icons_behaviour,
        active_role,
        metadata: {
            data_ok: data_ok_value ?? data_ok_checked,
            skipped,
            complete,
            locked,
            deleted: false,
            visibility,
            notes,
            chat_status,
        },
        callbacks: {
            on_data_ok_change: handle_data_ok_unified_change,
            on_skip_change,
            on_complete_change,
            on_notes_change: on_notes_change,
            on_chat_click: handle_chat_icon_click,
            on_visibility_change,
            on_lock_change,
            on_delete: on_delete ? () => on_delete(effective_field_id) : undefined,
        },
        config: {
            data_ok_mode: (data_ok_mode === 'multi_state' ? 'multi_state' : 'checkbox'),
            data_ok_hidden_states,
            chat_group_id: hazo_chat_group_id,
            current_user,
            field_data_id: effective_field_id,
            label,
            chat_api_base_url: hazo_chat_api_base_url,
            chat_timezone: hazo_chat_timezone,
            chat_read_only: hazo_chat_read_only,
            notes_panel_style,
            notes_background_color,
            notes_save_mode,
        },
        fallback_visibility: {
            data_ok: !disable_data_ok,
            skip: !!(enable_skip && !disable_skip),
            notes: !!(enable_notes && !disable_notes),
            chat: !disable_chat,
            visibility: !!(enable_visibility_toggle && !disable_visibility_toggle),
            lock: !!(enable_lock && !disable_lock),
            delete: !!(enable_delete && !disable_delete),
        },
        fallback_enabled: {
            data_ok: data_ok_editable !== false,
            skip: !disable_skip,
            notes: !disable_notes,
            chat: !is_chat_effectively_disabled,
            visibility: !disable_visibility_toggle,
            lock: !disable_lock,
            delete: !disable_delete,
        },
        // Clarification badge props
        clarification_count,
        on_clarification_click,
    };
    const file_upload_props = {
        field_id_final,
        accept_files_public: !!(accept_files_public && !locked),
        accept_files_private: !!(accept_files_private && !locked),
        files_dir,
        default_file_accordion_open,
        max_size,
        min_files,
        max_files,
        file_accept,
        file_processor,
        file_validator,
        file_manager,
        files: current_files,
        on_files_change: handle_files_change,
        component_ref,
        private_files_permission,
        can_access_private_files,
        private_files,
        on_private_files_change,
        validating_file_ids,
        error_file_ids,
        passed_file_ids,
        error_file_details,
        clarification_items,
        clarification_callbacks,
        private_files_class_name,
    };
    const reference_tag_props = {
        reference_data: reference_data_prop,
        reference_value,
        reference_label,
        reference_tag_background_color,
        on_reference_delete,
    };
    const error_props = {
        field_id_final,
        error: display_error,
        error_class_name,
    };
    const entry_props = {
        show_add_entry,
        label,
        field_id: effective_field_id,
        on_add_entry,
        is_additional_entry,
        on_delete_entry,
        action_buttons_borderless,
    };
    return {
        component_ref,
        current_files,
        set_internal_files,
        display_error,
        set_validation_error,
        is_field_disabled,
        is_hidden,
        should_hide,
        show_add_entry,
        handle_data_ok_unified_change,
        handle_files_change,
        field_id_final,
        handle_chat_icon_click,
        handle_chat_close,
        chat_is_open,
        is_chat_effectively_disabled,
        container_props,
        label_props,
        controls_props,
        file_upload_props,
        reference_tag_props,
        error_props,
        entry_props,
        user_role,
        field_width_class_name,
        locked,
        skipped,
        className,
        show_required_highlight,
    };
}
//# sourceMappingURL=use_base_form_field.js.map