/**
 * use_file_status - Hook for polling file statuses from hazo_files.
 *
 * Collects all file_ids from __files_ / __private_files_ keys in form_data,
 * then periodically calls file_manager.callbacks.get_status() to check
 * whether any files are missing, orphaned, or soft-deleted.
 */
'use client';
import { useState, useEffect, useRef, useCallback } from 'react';
/**
 * Extract all file_ids from form_data's __files_ and __private_files_ entries.
 */
function collect_file_ids(form_data) {
    const ids = new Set();
    for (const key of Object.keys(form_data)) {
        if (key.startsWith('__files_') || key.startsWith('__private_files_')) {
            const arr = form_data[key];
            if (Array.isArray(arr)) {
                for (const item of arr) {
                    if (item && typeof item === 'object' && typeof item.file_id === 'string') {
                        ids.add(item.file_id);
                    }
                }
            }
        }
    }
    return Array.from(ids);
}
export function use_file_status({ form_data, callbacks, enabled = false, poll_interval = 60000, }) {
    const [statuses, set_statuses] = useState({});
    const [loading, set_loading] = useState(false);
    const interval_ref = useRef(null);
    const fetch_statuses = useCallback(async () => {
        if (!callbacks?.get_status)
            return;
        const file_ids = collect_file_ids(form_data);
        if (file_ids.length === 0)
            return;
        set_loading(true);
        try {
            const result = await callbacks.get_status(file_ids);
            set_statuses(result);
        }
        catch {
            // Silently fail — status checking is non-critical
        }
        finally {
            set_loading(false);
        }
    }, [form_data, callbacks]);
    // Initial fetch + polling
    useEffect(() => {
        if (!enabled || !callbacks?.get_status)
            return;
        fetch_statuses();
        if (poll_interval > 0) {
            interval_ref.current = setInterval(fetch_statuses, poll_interval);
        }
        return () => {
            if (interval_ref.current) {
                clearInterval(interval_ref.current);
                interval_ref.current = null;
            }
        };
    }, [enabled, poll_interval, fetch_statuses]);
    return { statuses, loading, refresh: fetch_statuses };
}
//# sourceMappingURL=use_file_status.js.map