"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMainwrapRealtimeCounts = void 0;
const tslib_1 = require("tslib");
const react_1 = require("react");
const socket_io_client_1 = require("socket.io-client");
const required_action_api_1 = require("../components/require-action/required-action-api");
const getEnvPublic_1 = tslib_1.__importDefault(require("../../Func/getEnvPublic"));
const INAPP_NOTI_SOCKET_CONFIG = {
    namespace: '/core-socket',
    path: '/core-socket',
    joinRoomEvent: 'join_room',
    realtimeEvent: 'inapp-noti-count',
};
const INAPP_NOTI_ROOM_ID = {
    realtime: 'inapp-noti-count',
};
const isRecord = (value) => typeof value === 'object' && value !== null;
const getPublicHostname = async () => {
    const response = await (0, getEnvPublic_1.default)();
    return isRecord(response) && typeof response.HOSTNAME === 'string' ? response.HOSTNAME : undefined;
};
const getDashboardCountPayload = (payload) => {
    if (!isRecord(payload) || !isRecord(payload.data))
        return undefined;
    return isRecord(payload.data.dashboard) ? payload.data.dashboard : undefined;
};
const getUnreadCount = (payload) => {
    const dashboardPayload = getDashboardCountPayload(payload);
    return dashboardPayload && typeof dashboardPayload.unread_count === 'number'
        ? dashboardPayload.unread_count
        : undefined;
};
const parseDashboardRequiredActionPayload = (payload) => {
    const dashboardPayload = getDashboardCountPayload(payload);
    const counts = dashboardPayload && isRecord(dashboardPayload.required_action_count) ? dashboardPayload.required_action_count : {};
    return (0, required_action_api_1.parseRequiredActionPayload)({ counts });
};
const getPayloadUserId = (payload) => {
    if (!isRecord(payload))
        return undefined;
    const userId = Number(payload.user_id);
    return Number.isFinite(userId) ? userId : undefined;
};
const isPayloadForCurrentUser = (payload, currentUserId) => {
    const payloadUserId = getPayloadUserId(payload);
    if (!payloadUserId)
        return true;
    return payloadUserId === currentUserId;
};
const getSocketHeaders = (accessToken, hostname) => ({
    authorization: `Bearer ${accessToken}`,
    ...(hostname ? { hostname } : {}),
    module: 'inapp-noti',
});
const joinRealtimeRooms = (socket) => {
    socket.emit(INAPP_NOTI_SOCKET_CONFIG.joinRoomEvent, {
        roomId: INAPP_NOTI_ROOM_ID.realtime,
    });
};
const bindRealtimeSocketEvents = ({ currentUserId, onConnectionChange, onRequiredAction, onUnreadCount, socket, }) => {
    socket.on('connect', () => {
        onConnectionChange === null || onConnectionChange === void 0 ? void 0 : onConnectionChange(true);
        joinRealtimeRooms(socket);
    });
    socket.on('connect_error', () => {
        onConnectionChange === null || onConnectionChange === void 0 ? void 0 : onConnectionChange(false);
    });
    socket.on('disconnect', () => {
        onConnectionChange === null || onConnectionChange === void 0 ? void 0 : onConnectionChange(false);
    });
    socket.io.on('reconnect', () => {
        onConnectionChange === null || onConnectionChange === void 0 ? void 0 : onConnectionChange(true);
    });
    socket.on(INAPP_NOTI_SOCKET_CONFIG.realtimeEvent, (payload) => {
        if (!isPayloadForCurrentUser(payload, currentUserId)) {
            return;
        }
        onUnreadCount(getUnreadCount(payload));
        onRequiredAction(parseDashboardRequiredActionPayload(payload));
    });
};
const setupRealtimeSocket = async ({ accessToken, currentUserId, onConnectionChange, onRequiredAction, onUnreadCount, }) => {
    const socketUrl = `${process.env.NEXT_PUBLIC_API_GATEWAY}${INAPP_NOTI_SOCKET_CONFIG.namespace}`;
    const hostname = await getPublicHostname();
    const socket = (0, socket_io_client_1.io)(socketUrl, {
        path: INAPP_NOTI_SOCKET_CONFIG.path,
        // transports: ['websocket'],
        extraHeaders: getSocketHeaders(accessToken, hostname),
    });
    bindRealtimeSocketEvents({
        currentUserId,
        onConnectionChange,
        onRequiredAction,
        onUnreadCount,
        socket,
    });
    return socket;
};
const useMainwrapRealtimeCounts = ({ accessToken, currentUserId, enabled, onConnectionChange, onRequiredAction, onUnreadCount, }) => {
    const onConnectionChangeRef = (0, react_1.useRef)(onConnectionChange);
    const onRequiredActionRef = (0, react_1.useRef)(onRequiredAction);
    const onUnreadCountRef = (0, react_1.useRef)(onUnreadCount);
    // Sync ref mỗi render, không trigger effect
    (0, react_1.useEffect)(() => {
        onConnectionChangeRef.current = onConnectionChange;
        onRequiredActionRef.current = onRequiredAction;
        onUnreadCountRef.current = onUnreadCount;
    });
    (0, react_1.useEffect)(() => {
        var _a;
        if (!enabled || !accessToken || !currentUserId) {
            (_a = onConnectionChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onConnectionChangeRef, false);
            return;
        }
        let socket = null;
        let isDisposed = false;
        const connectSocket = async () => {
            var _a;
            try {
                if (isDisposed)
                    return;
                const nextSocket = await setupRealtimeSocket({
                    accessToken,
                    currentUserId,
                    onConnectionChange: (...args) => { var _a; return (_a = onConnectionChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onConnectionChangeRef, ...args); },
                    onRequiredAction: (...args) => { var _a; return (_a = onRequiredActionRef.current) === null || _a === void 0 ? void 0 : _a.call(onRequiredActionRef, ...args); },
                    onUnreadCount: (...args) => { var _a; return (_a = onUnreadCountRef.current) === null || _a === void 0 ? void 0 : _a.call(onUnreadCountRef, ...args); },
                });
                if (isDisposed) {
                    nextSocket.disconnect();
                    return;
                }
                socket = nextSocket;
            }
            catch (_b) {
                (_a = onConnectionChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onConnectionChangeRef, false);
            }
        };
        void connectSocket();
        return () => {
            var _a;
            isDisposed = true;
            (_a = onConnectionChangeRef.current) === null || _a === void 0 ? void 0 : _a.call(onConnectionChangeRef, false);
            socket === null || socket === void 0 ? void 0 : socket.disconnect();
        };
    }, [enabled, accessToken, currentUserId]);
};
exports.useMainwrapRealtimeCounts = useMainwrapRealtimeCounts;
//# sourceMappingURL=use-mainwrap-realtime-counts.js.map