/**
 * use_response_files - Hook for managing file upload state in ClarificationResponseForm.
 *
 * Handles:
 * - File state with prop-sync (merges external initial_files changes without overwriting local additions)
 * - Upload via file_manager.upload
 * - Drag-and-drop + file input selection
 * - File removal via file_manager.remove
 * - Change notification (skips when syncing from props to avoid loops)
 */
'use client';
import { useState, useCallback, useRef, useEffect } from 'react';
export function use_response_files({ initial_files, file_manager, on_change, }) {
    const [files, set_files] = useState(initial_files ?? []);
    const [uploading, set_uploading] = useState(false);
    const file_input_ref = useRef(null);
    // Sync from props: merge external initial_files changes without overwriting local additions
    const syncing_from_props = useRef(false);
    const prev_files_len = useRef(initial_files?.length);
    useEffect(() => {
        const new_len = initial_files?.length ?? 0;
        if (new_len !== prev_files_len.current && new_len > 0) {
            set_files(current => {
                const current_ids = new Set(current.map(f => f.file_id));
                const has_new = (initial_files ?? []).some(f => !current_ids.has(f.file_id));
                if (has_new) {
                    syncing_from_props.current = true;
                    const merged = [...current];
                    for (const f of initial_files ?? []) {
                        if (!current_ids.has(f.file_id))
                            merged.push(f);
                    }
                    return merged;
                }
                return current;
            });
        }
        prev_files_len.current = new_len;
    }, [initial_files]);
    // Notify parent when files change (skip when syncing from props)
    const files_ref = useRef(files);
    useEffect(() => {
        if (files_ref.current !== files) {
            files_ref.current = files;
            if (syncing_from_props.current) {
                syncing_from_props.current = false;
            }
            else {
                on_change?.(files);
            }
        }
    }, [files]); // eslint-disable-line react-hooks/exhaustive-deps
    const upload_files = useCallback(async (file_list) => {
        if (!file_manager?.upload)
            return;
        set_uploading(true);
        try {
            for (const file of Array.from(file_list)) {
                const attachment = await file_manager.upload(file, { entity_id: 'clarification_response', visibility: 'public' });
                set_files((prev) => [...prev, attachment]);
            }
        }
        catch (err) {
            console.error('[use_response_files] Upload failed:', err);
        }
        finally {
            set_uploading(false);
        }
    }, [file_manager]);
    const handle_file_drop = useCallback((e) => {
        e.preventDefault();
        if (e.dataTransfer.files.length > 0) {
            upload_files(e.dataTransfer.files);
        }
    }, [upload_files]);
    const handle_file_select = useCallback((e) => {
        if (e.target.files && e.target.files.length > 0) {
            upload_files(e.target.files);
            e.target.value = '';
        }
    }, [upload_files]);
    const handle_remove_file = useCallback(async (file_id) => {
        try {
            await file_manager?.remove?.(file_id, '');
        }
        catch { /* ignore removal errors */ }
        set_files((prev) => prev.filter((f) => f.file_id !== file_id));
    }, [file_manager]);
    return {
        files,
        uploading,
        file_input_ref,
        handle_file_drop,
        handle_file_select,
        handle_remove_file,
    };
}
//# sourceMappingURL=use_response_files.js.map