"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useNotificationUnreadPolling = useNotificationUnreadPolling;
const react_1 = require("react");
const useUnreadPolling_1 = require("../hook/useUnreadPolling");
const apiClient_1 = require("../../../../../lib/fetch/apiClient");
const normalizeUnreadCount = (count) => {
    const normalized = Number(count || 0);
    return Number.isFinite(normalized) && normalized > 0 ? Math.floor(normalized) : 0;
};
function useNotificationUnreadPolling(intervalMs = 5000, enabled = true) {
    const [unreadCount, setUnreadCount] = (0, react_1.useState)(0);
    const fetchUnreadCount = (0, react_1.useCallback)(async () => {
        try {
            const data = (await (0, apiClient_1.apiFetch)('/inapp-noti/notification/unread-count', {
                method: 'GET',
            }));
            setUnreadCount(normalizeUnreadCount(data === null || data === void 0 ? void 0 : data.unread_count));
        }
        catch (error) {
            console.error('Error fetching unread count:', error);
        }
    }, []);
    const applyUnreadCount = (0, react_1.useCallback)((count) => {
        setUnreadCount(normalizeUnreadCount(count));
    }, []);
    (0, useUnreadPolling_1.useUnreadPolling)(fetchUnreadCount, intervalMs, enabled);
    return {
        unreadCount,
        refetchUnreadCount: fetchUnreadCount,
        applyUnreadCount,
    };
}
//# sourceMappingURL=useNotificationUnreadPolling.js.map