/**
 * Build UnifiedFieldControls props from field config and context
 *
 * Provides a unified way to construct props for UnifiedFieldControls
 * from various sources (field config, form context, metadata).
 */
import { resolve_field_controls, controls_to_fallback_visibility } from './resolve_field_controls.js';
/**
 * Build UnifiedFieldControls props from field config and form context
 *
 * This function consolidates all the logic for building control props:
 * - Resolves field-level > group-level > form-level cascade
 * - Builds metadata from field_metadata
 * - Wraps callbacks to include field_id
 * - Configures auto-actions
 *
 * @param field - Field configuration
 * @param parent_group - Parent group configuration (if nested)
 * @param context - Form-level context
 * @param field_metadata - Current field metadata state
 * @param callbacks - Form-level callbacks
 * @returns Props ready for UnifiedFieldControls
 *
 * @example
 * ```tsx
 * const props = build_control_props(
 *   field,
 *   parent_group,
 *   formContext,
 *   field_metadata[field.id],
 *   formCallbacks
 * );
 *
 * <UnifiedFieldControls {...props} />
 * ```
 */
export function build_control_props(field, parent_group, context, field_metadata, callbacks) {
    // Resolve field-level controls with cascade
    const resolved_controls = resolve_field_controls(field, parent_group, context);
    // Build fallback visibility from resolved controls
    const fallback_visibility = controls_to_fallback_visibility(resolved_controls);
    // Build fallback enabled (based on _editable props)
    const fallback_enabled = {
        data_ok: context.data_ok_editable ?? true,
        skip: context.skip_editable ?? true,
        notes: true, // Notes are always editable when visible
        chat: true, // Chat is always interactive when visible
        visibility: context.visibility_editable ?? true,
        lock: context.lock_editable ?? true,
        delete: true, // Delete is always interactive when visible
        add_elements: true, // Add elements is always interactive when visible
    };
    // Build control metadata
    const metadata = {
        data_ok: field_metadata?.data_ok,
        skipped: field_metadata?.skipped,
        locked: field_metadata?.locked,
        deleted: field_metadata?.deleted,
        visibility: field_metadata?.visibility,
        notes: field_metadata?.notes,
        has_chat_messages: field_metadata?.has_chat_messages,
        chat_status: field_metadata?.chat_status,
    };
    // Build wrapped callbacks
    const control_callbacks = {
        on_data_ok_change: callbacks.on_data_ok_change
            ? (value) => callbacks.on_data_ok_change(field.id, value)
            : undefined,
        on_skip_change: callbacks.on_skip_change
            ? (skipped) => callbacks.on_skip_change(field.id, skipped)
            : undefined,
        on_notes_change: callbacks.on_notes_change
            ? (notes) => callbacks.on_notes_change(field.id, notes)
            : undefined,
        on_visibility_change: callbacks.on_visibility_change
            ? (visibility) => callbacks.on_visibility_change(field.id, visibility)
            : undefined,
        on_lock_change: callbacks.on_lock_change
            ? (locked) => callbacks.on_lock_change(field.id, locked)
            : undefined,
        on_delete: callbacks.on_delete
            ? () => callbacks.on_delete(field.id)
            : undefined,
        on_chat_click: callbacks.on_chat_click
            ? () => callbacks.on_chat_click(field.id)
            : undefined,
    };
    // Build control config
    const config = {
        data_ok_mode: context.data_ok_mode,
        data_ok_hidden_states: context.data_ok_hidden_states,
        chat_group_id: context.chat_group_id,
        chat_api_base_url: context.chat_api_base_url,
        chat_timezone: context.chat_timezone,
        chat_read_only: context.chat_read_only,
        notes_panel_style: context.notes_panel_style,
        notes_background_color: context.notes_background_color,
        notes_save_mode: context.notes_save_mode,
        notes_mode: context.notes_mode,
        current_user: context.current_user,
        field_data_id: field.id,
        label: field.label,
    };
    // Build auto-actions
    const auto_actions = {
        on_data_ok_hidden: context.on_data_ok_hidden,
        on_data_ok_protected: context.on_data_ok_protected,
    };
    return {
        field_id: field.id,
        icons_behaviour: context.icons_behaviour,
        active_role: context.active_role,
        metadata,
        callbacks: control_callbacks,
        config,
        fallback_visibility,
        fallback_enabled,
        auto_actions,
    };
}
//# sourceMappingURL=build_control_props.js.map