/**
 * Hooks for consuming FormViewContext
 *
 * Provides convenient access to form view state and utilities.
 */
'use client';
import { useMemo, useCallback } from 'react';
import { useFormViewContext } from './context.js';
import { should_render_field, get_field_value, is_field_empty, has_visible_children, has_visible_groups } from '../shared/field_utils/index.js';
import { resolve_role_controls } from '../shared/role_utils/index.js';
/**
 * Hook to get current view mode
 */
export function useViewMode() {
    const { mode } = useFormViewContext();
    return mode;
}
/**
 * Hook to get form data
 */
export function useFormData() {
    const { form_data } = useFormViewContext();
    return form_data;
}
/**
 * Hook to get field metadata
 */
export function useFieldMetadata() {
    const { field_metadata } = useFormViewContext();
    return field_metadata;
}
/**
 * Hook to get settings
 */
export function useViewSettings() {
    const { settings } = useFormViewContext();
    return settings;
}
/**
 * Hook to get callbacks
 */
export function useViewCallbacks() {
    const { callbacks } = useFormViewContext();
    return callbacks;
}
/**
 * Compute whether hidden fields should be force-hidden based on active_role.
 * Returns true when the active_role cannot toggle visibility.
 */
function useForceHideHidden() {
    const { settings, icons_behaviour } = useFormViewContext();
    return useMemo(() => {
        // Base check already handles responders
        if (settings.user_role === 'responder')
            return false;
        if (!icons_behaviour || !settings.active_role)
            return false;
        const role_controls = resolve_role_controls(icons_behaviour, settings.active_role);
        return !role_controls?.control_visibility?.visible;
    }, [settings.user_role, settings.active_role, icons_behaviour]);
}
/**
 * Hook to check if a field should be rendered
 */
export function useShouldRenderField() {
    const { form_data, field_metadata, settings } = useFormViewContext();
    const force_hide_hidden = useForceHideHidden();
    return useCallback((field) => {
        const metadata = field_metadata[field.id];
        return should_render_field(field, form_data, metadata, {
            check_dependencies: settings.check_dependencies,
            check_visibility: true,
            check_deleted: true,
            user_role: settings.user_role ?? 'author',
            force_hide_hidden,
        });
    }, [form_data, field_metadata, settings.check_dependencies, settings.user_role, force_hide_hidden]);
}
/**
 * Hook to check if a group has any visible children.
 * Returns a callback (group: ViewFieldConfig) => boolean.
 * Authors always see groups; responders/non-visibility roles skip empty groups.
 */
export function useShouldRenderGroup() {
    const { form_data, field_metadata, settings } = useFormViewContext();
    const force_hide_hidden = useForceHideHidden();
    return useCallback((group) => {
        // Authors with visibility toggle always see everything
        if (settings.user_role !== 'responder' && !force_hide_hidden)
            return true;
        const render_options = {
            check_dependencies: settings.check_dependencies,
            check_visibility: true,
            check_deleted: true,
            user_role: settings.user_role ?? 'author',
            force_hide_hidden,
        };
        return has_visible_children(group, form_data, field_metadata, render_options);
    }, [form_data, field_metadata, settings.check_dependencies, settings.user_role, force_hide_hidden]);
}
/**
 * Hook to check if a section has any visible groups.
 * Returns a callback (section: ViewSection) => boolean.
 * Authors always see sections; responders/non-visibility roles skip empty sections.
 */
export function useShouldRenderSection() {
    const { form_data, field_metadata, settings } = useFormViewContext();
    const force_hide_hidden = useForceHideHidden();
    return useCallback((section) => {
        // Authors with visibility toggle always see everything
        if (settings.user_role !== 'responder' && !force_hide_hidden)
            return true;
        if (!section.groups?.length)
            return false;
        // Check for section-level files
        const section_key = section.section_name?.replace(/\s+/g, '_') || 'section';
        const section_files = form_data[`__files_${section_key}`];
        const section_private_files = form_data[`__private_files_${section_key}`];
        if (Array.isArray(section_files) && section_files.length > 0)
            return true;
        if (Array.isArray(section_private_files) && section_private_files.length > 0)
            return true;
        const render_options = {
            check_dependencies: settings.check_dependencies,
            check_visibility: true,
            check_deleted: true,
            user_role: settings.user_role ?? 'author',
            force_hide_hidden,
        };
        return has_visible_groups(section.groups, form_data, field_metadata, render_options);
    }, [form_data, field_metadata, settings.check_dependencies, settings.user_role, force_hide_hidden]);
}
/**
 * Hook to get a field's value
 */
export function useFieldValue(field_id) {
    const { form_data } = useFormViewContext();
    return get_field_value(field_id, form_data);
}
/**
 * Hook to get a field's metadata
 */
export function useFieldMeta(field_id) {
    const { field_metadata } = useFormViewContext();
    return field_metadata[field_id];
}
/**
 * Hook to check if a field is empty
 */
export function useIsFieldEmpty(field_id) {
    const value = useFieldValue(field_id);
    return is_field_empty(value);
}
/**
 * Hook for field data_ok operations
 */
export function useFieldDataOk(field_id) {
    const { field_metadata, callbacks, settings } = useFormViewContext();
    const metadata = field_metadata[field_id];
    const value = metadata?.data_ok;
    const is_ok = value === true || value === 'ok';
    const is_pending = value === 'pending';
    const is_not_ok = value === false || value === 'not_ok';
    const set_data_ok = useCallback((new_value) => {
        if (callbacks.on_data_ok_change && settings.data_ok_editable) {
            callbacks.on_data_ok_change(field_id, new_value);
        }
    }, [field_id, callbacks.on_data_ok_change, settings.data_ok_editable]);
    return {
        value,
        is_ok,
        is_pending,
        is_not_ok,
        set_data_ok,
        mode: settings.data_ok_mode,
        editable: settings.data_ok_editable,
    };
}
/**
 * Hook for field notes operations
 */
export function useFieldNotes(field_id) {
    const { field_metadata, callbacks, settings } = useFormViewContext();
    const metadata = field_metadata[field_id];
    const notes = metadata?.notes ?? [];
    const has_notes = notes.length > 0;
    const set_notes = useCallback((new_notes) => {
        if (callbacks.on_notes_change) {
            callbacks.on_notes_change(field_id, new_notes);
        }
    }, [field_id, callbacks.on_notes_change]);
    return {
        notes,
        has_notes,
        set_notes,
        enabled: settings.enable_notes,
    };
}
/**
 * Hook for field visibility operations
 */
export function useFieldVisibility(field_id) {
    const { field_metadata, callbacks, settings } = useFormViewContext();
    const metadata = field_metadata[field_id];
    const visibility = metadata?.visibility ?? 'visible';
    const is_hidden = visibility === 'hidden';
    const set_visibility = useCallback((new_visibility) => {
        if (callbacks.on_visibility_change && settings.visibility_editable) {
            callbacks.on_visibility_change(field_id, new_visibility);
        }
    }, [field_id, callbacks.on_visibility_change, settings.visibility_editable]);
    const toggle_visibility = useCallback(() => {
        set_visibility(is_hidden ? 'visible' : 'hidden');
    }, [is_hidden, set_visibility]);
    return {
        visibility,
        is_hidden,
        set_visibility,
        toggle_visibility,
        enabled: settings.enable_visibility_toggle,
        editable: settings.visibility_editable,
    };
}
/**
 * Hook for field lock operations
 */
export function useFieldLock(field_id) {
    const { field_metadata, callbacks, settings } = useFormViewContext();
    const metadata = field_metadata[field_id];
    const locked = metadata?.locked ?? false;
    const set_locked = useCallback((new_locked) => {
        if (callbacks.on_lock_change && settings.lock_editable) {
            callbacks.on_lock_change(field_id, new_locked);
        }
    }, [field_id, callbacks.on_lock_change, settings.lock_editable]);
    const toggle_lock = useCallback(() => {
        set_locked(!locked);
    }, [locked, set_locked]);
    return {
        locked,
        set_locked,
        toggle_lock,
        enabled: settings.enable_lock,
        editable: settings.lock_editable,
    };
}
/**
 * Hook for approval mode statistics
 * Calculates approval progress for sections
 */
export function useApprovalStats() {
    const { sections, field_metadata, form_data, settings } = useFormViewContext();
    const should_render = useShouldRenderField();
    return useMemo(() => {
        let total_fields = 0;
        let approved_fields = 0;
        let pending_fields = 0;
        let not_ok_fields = 0;
        const process_fields = (fields) => {
            for (const field of fields) {
                // Skip groups (they don't have data_ok)
                if (field.field_type === 'group') {
                    if (field.fields) {
                        process_fields(field.fields);
                    }
                    continue;
                }
                // Skip fields that shouldn't be rendered
                if (!should_render(field))
                    continue;
                total_fields++;
                const metadata = field_metadata[field.id];
                const data_ok = metadata?.data_ok;
                if (data_ok === true || data_ok === 'ok') {
                    approved_fields++;
                }
                else if (data_ok === 'pending') {
                    pending_fields++;
                }
                else {
                    not_ok_fields++;
                }
            }
        };
        for (const section of sections) {
            if (section.groups) {
                process_fields(section.groups);
            }
        }
        return {
            total_fields,
            approved_fields,
            pending_fields,
            not_ok_fields,
            progress_percent: total_fields > 0 ? Math.round((approved_fields / total_fields) * 100) : 0,
        };
    }, [sections, field_metadata, should_render]);
}
//# sourceMappingURL=use_form_view.js.map