/**
 * Shared hook for building UnifiedFieldControls props.
 *
 * Summary view duplicates these 6 useMemo blocks identically in both
 * SummaryGroup and SummaryField. This hook consolidates them into one call.
 */
'use client';
import { useMemo } from 'react';
import { resolve_role_controls } from '../role_utils/index.js';
import { EDIT_CONTROL_NAMES, CONTROL_ICON_NAMES } from '../../../types/icons_behaviour.js';
/** The empty fallback settings (all false) */
const EMPTY_FALLBACK = {
    data_ok: false,
    skip: false,
    notes: false,
    chat: false,
    visibility: false,
    lock: false,
    delete: false,
};
/**
 * Build all props needed by UnifiedFieldControls for a field or group.
 */
export function use_field_control_props(input) {
    const { has_controls, show_edit_icons, show_control_icons, field_id, field_label, entity_type, icons_behaviour, active_role, metadata, settings, chat_config, current_user, callbacks, validation_rules, } = input;
    // Resolve category overrides with backward-compat fallback to has_controls
    const edit_override = show_edit_icons ?? (has_controls ? undefined : false);
    const control_override = show_control_icons ?? (has_controls ? undefined : false);
    const role_controls = useMemo(() => {
        return resolve_role_controls(icons_behaviour, active_role);
    }, [icons_behaviour, active_role]);
    // Helper: is a control visible given role config + category overrides?
    const is_visible = useMemo(() => {
        return (name) => {
            if (!role_controls?.[name]?.visible)
                return false;
            if (EDIT_CONTROL_NAMES.includes(name))
                return edit_override !== false;
            if (CONTROL_ICON_NAMES.includes(name))
                return control_override !== false;
            return true; // standalone (add_elements, edit_elements) - always role-driven
        };
    }, [role_controls, edit_override, control_override]);
    const fallback_visibility = useMemo(() => {
        return {
            data_ok: is_visible('control_data_ok'),
            skip: is_visible('control_skip'),
            notes: is_visible('control_notes'),
            chat: is_visible('control_chat'),
            visibility: is_visible('control_visibility'),
            lock: is_visible('control_lock'),
            delete: is_visible('control_delete'),
            edit_field: is_visible('control_edit_field'),
            edit_group: is_visible('control_edit_group'),
            delete_field: is_visible('control_delete_field'),
            edit_elements: is_visible('control_edit_elements'),
            revalidate: is_visible('control_revalidate') && !!validation_rules?.length,
        };
    }, [is_visible, validation_rules]);
    const fallback_enabled = useMemo(() => {
        return {
            data_ok: role_controls.control_data_ok.enabled,
            skip: role_controls.control_skip.enabled,
            notes: role_controls.control_notes.enabled,
            chat: role_controls.control_chat.enabled,
            visibility: role_controls.control_visibility.enabled,
            lock: role_controls.control_lock.enabled,
            delete: role_controls.control_delete.enabled,
            edit_field: role_controls.control_edit_field.enabled,
            edit_group: role_controls.control_edit_group.enabled,
            delete_field: role_controls.control_delete_field.enabled,
            edit_elements: role_controls.control_edit_elements.enabled,
            revalidate: role_controls.control_revalidate.enabled,
        };
    }, [role_controls]);
    const control_metadata = useMemo(() => ({
        data_ok: metadata?.data_ok,
        locked: metadata?.locked,
        deleted: metadata?.deleted,
        visibility: metadata?.visibility,
        skipped: metadata?.skipped,
        notes: metadata?.notes,
        has_chat_messages: metadata?.has_chat_messages,
        chat_status: metadata?.chat_status,
        has_validation_rules: !!validation_rules?.length,
    }), [metadata, validation_rules]);
    const control_callbacks = useMemo(() => ({
        on_data_ok_change: (value) => callbacks.on_data_ok_change?.(field_id, value),
        on_notes_change: (notes) => callbacks.on_notes_change?.(field_id, notes),
        on_chat_click: () => { },
        on_visibility_change: (visibility) => callbacks.on_visibility_change?.(field_id, visibility),
        on_lock_change: (locked) => callbacks.on_lock_change?.(field_id, locked),
        on_delete: () => callbacks.on_delete?.(field_id),
        on_skip_change: (skipped) => callbacks.on_skip_change?.(field_id, skipped),
        on_edit_elements: () => callbacks.on_edit_elements?.(field_id, entity_type),
        on_revalidate: validation_rules?.length ? () => callbacks.on_revalidate?.(field_id, validation_rules) : undefined,
    }), [field_id, entity_type, callbacks, validation_rules]);
    const control_config = useMemo(() => ({
        data_ok_mode: settings.data_ok_mode,
        data_ok_hidden_states: settings.data_ok_hidden_states,
        chat_group_id: chat_config?.group_id,
        chat_api_base_url: chat_config?.api_base_url,
        chat_timezone: chat_config?.timezone,
        chat_read_only: chat_config?.read_only,
        chat_log_polling: chat_config?.log_polling,
        current_user: current_user,
        field_data_id: field_id,
        label: field_label || field_id,
        notes_panel_style: settings.notes_panel_style,
        notes_background_color: settings.notes_background_color,
        notes_save_mode: settings.notes_save_mode,
        notes_mode: settings.notes_mode,
        auto_actions: {
            on_data_ok_hidden: settings.on_data_ok_hidden,
            on_data_ok_protected: settings.on_data_ok_protected,
        },
    }), [settings, chat_config, current_user, field_id, field_label]);
    return {
        role_controls,
        fallback_visibility,
        fallback_enabled,
        control_metadata,
        control_callbacks,
        control_config,
    };
}
//# sourceMappingURL=use_field_control_props.js.map