/**
 * Hook for resolving control states based on icons_behaviour and active role
 *
 * Centralizes the logic for determining which controls are visible, enabled,
 * extracted (shown inline vs kebab), and have borders.
 */
import { useMemo } from 'react';
import { resolve_control_state, get_icon_order, control_has_value, } from '../role_utils/index.js';
import { DEFAULT_FALLBACK_VISIBILITY, DEFAULT_FALLBACK_ENABLED, CONTROL_TO_FALLBACK_KEY, } from './constants.js';
/**
 * Hook to resolve all control states based on icons_behaviour and active role
 *
 * @param icons_behaviour - The icons behaviour configuration
 * @param active_role - The active role ID
 * @param metadata - Current control metadata (values, states)
 * @param fallback_visibility - Fallback visibility when no icons_behaviour
 * @param fallback_enabled - Fallback enabled state when no icons_behaviour
 * @returns Resolved control states and rendering order
 */
export function use_control_resolution(icons_behaviour, active_role, metadata, fallback_visibility, fallback_enabled) {
    // Merge fallbacks with defaults
    const merged_visibility = {
        ...DEFAULT_FALLBACK_VISIBILITY,
        ...fallback_visibility,
    };
    const merged_enabled = {
        ...DEFAULT_FALLBACK_ENABLED,
        ...fallback_enabled,
    };
    // Convert metadata to the format expected by role_utils
    const resolved_metadata = useMemo(() => ({
        data_ok: metadata.data_ok,
        skipped: metadata.skipped,
        locked: metadata.locked,
        deleted: metadata.deleted,
        visibility: metadata.visibility,
        notes: metadata.notes,
        has_chat_messages: metadata.has_chat_messages,
        chat_status: metadata.chat_status,
    }), [metadata]);
    // Resolve all control states
    const controls = useMemo(() => {
        const all_controls = [
            'control_data_ok',
            'control_skip',
            'control_notes',
            'control_chat',
            'control_visibility',
            'control_lock',
            'control_delete',
            'control_add_elements',
            'control_edit_field',
            'control_edit_group',
            'control_delete_field',
            'control_edit_elements',
            'control_revalidate',
        ];
        const result = {};
        for (const control of all_controls) {
            const fallback_key = CONTROL_TO_FALLBACK_KEY[control];
            const fallback = {
                visible: merged_visibility[fallback_key] ?? false,
                enabled: merged_enabled[fallback_key] ?? true,
            };
            const state = resolve_control_state(control, icons_behaviour, active_role, resolved_metadata, fallback);
            result[control] = {
                ...state,
                has_value: control_has_value(control, resolved_metadata),
            };
        }
        return result;
    }, [icons_behaviour, active_role, resolved_metadata, merged_visibility, merged_enabled]);
    // Get icon order
    const icon_order = useMemo(() => {
        return get_icon_order(icons_behaviour);
    }, [icons_behaviour]);
    // Determine inline vs kebab placement
    const { inline_controls, kebab_controls, show_kebab, has_kebab_indicator, kebab_indicator_status } = useMemo(() => {
        const inline = [];
        const kebab = [];
        // Process controls in order
        for (const control of icon_order) {
            const state = controls[control];
            if (!state || !state.visible)
                continue;
            // Data OK, Skip, Edit Field, and Edit Group are ALWAYS inline (never in kebab)
            if (control === 'control_data_ok' || control === 'control_skip' || control === 'control_edit_field' || control === 'control_edit_group') {
                inline.push(control);
                continue;
            }
            // Extracted controls go inline
            if (state.extracted) {
                inline.push(control);
            }
            else {
                kebab.push(control);
            }
        }
        // Show kebab only if 2+ controls would go in it
        const should_show_kebab = kebab.length >= 2;
        // If not showing kebab, move kebab items to inline
        const final_inline = should_show_kebab ? inline : [...inline, ...kebab];
        const final_kebab = should_show_kebab ? kebab : [];
        // Determine kebab indicator
        let indicator_status = null;
        let has_indicator = false;
        if (should_show_kebab) {
            // Check for chat activity in kebab
            const chat_in_kebab = final_kebab.includes('control_chat');
            const notes_in_kebab = final_kebab.includes('control_notes');
            if (chat_in_kebab && controls.control_chat.has_value) {
                const chat_status = metadata.chat_status ?? (metadata.has_chat_messages ? 'has_messages' : 'none');
                if (chat_status === 'has_unread') {
                    indicator_status = 'unread';
                    has_indicator = true;
                }
                else if (chat_status === 'has_messages') {
                    indicator_status = 'messages';
                    has_indicator = true;
                }
            }
            if (notes_in_kebab && controls.control_notes.has_value && !indicator_status) {
                indicator_status = 'notes';
                has_indicator = true;
            }
        }
        return {
            inline_controls: final_inline,
            kebab_controls: final_kebab,
            show_kebab: should_show_kebab,
            has_kebab_indicator: has_indicator,
            kebab_indicator_status: indicator_status,
        };
    }, [icon_order, controls, metadata]);
    return {
        controls,
        icon_order,
        inline_controls,
        kebab_controls,
        show_kebab,
        has_kebab_indicator,
        kebab_indicator_status,
    };
}
//# sourceMappingURL=use_control_resolution.js.map