"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useBatteryStatuses = void 0;
const react_1 = require("react");
const logs_1 = require("@ledgerhq/logs");
const getBatteryStatuses_1 = require("../actions/getBatteryStatuses");
const env_react_1 = require("../../env.react");
/**
 * Hook used to query one or multiple battery statuses for Ledger Stax. The state will contain an array of with all the
 * requested statuses in corresponding order.
 *
 * @param deviceId
 * @param statuses A list of status types to query
 *
 * @returns An object containing:
 * - the current state of the request
 * - a boolean that informs if the request is complete
 * - a function to trigger an retrigger the device action
 */
const useBatteryStatuses = ({ deviceId, deviceName, statuses, enabled, }) => {
    const [batteryStatusesState, setBatteryStatusesState] = (0, react_1.useState)(getBatteryStatuses_1.initialState);
    const [requestCompleted, setRequestCompleted] = (0, react_1.useState)(false);
    const [nonce, setNonce] = (0, react_1.useState)(0);
    // when passing a function to useState, the function is used as an initializer,
    // i.e its return value will be the initial state value,
    // cf. https://react.dev/reference/react/useState#parameters
    const [cancelRequest, setCancelRequest] = (0, react_1.useState)(() => () => { });
    const [isBatteryLow, setIsBatteryLow] = (0, react_1.useState)(false);
    const lowBatteryPercentage = (0, env_react_1.useEnv)("LOW_BATTERY_PERCENTAGE");
    (0, react_1.useEffect)(() => {
        if (!enabled)
            return;
        if (nonce > 0 && deviceId) {
            const sub = (0, getBatteryStatuses_1.getBatteryStatusesAction)({
                deviceId,
                deviceName,
                statuses,
            }).subscribe({
                next: state => {
                    if (state.error?.type === "UnknownApdu") {
                        setBatteryStatusesState({ ...state, error: null });
                    }
                    else {
                        setBatteryStatusesState(state);
                    }
                    // no battery status flags available
                    if (state.batteryStatuses.length <= 1)
                        return;
                    const [percentage, statusFlags] = state.batteryStatuses;
                    if (percentage < lowBatteryPercentage && statusFlags.charging === 0) {
                        setIsBatteryLow(true);
                    }
                },
                complete: () => {
                    setRequestCompleted(true);
                },
                error: err => (0, logs_1.log)("error", "error while retrieving the battery statuses", err),
            });
            setCancelRequest(() => () => {
                sub.unsubscribe();
                setRequestCompleted(false);
                setBatteryStatusesState(getBatteryStatuses_1.initialState);
            });
            return () => {
                sub.unsubscribe();
            };
        }
    }, [deviceId, deviceName, lowBatteryPercentage, statuses, nonce, enabled]);
    const triggerRequest = (0, react_1.useCallback)(() => {
        setRequestCompleted(false);
        setIsBatteryLow(false);
        setBatteryStatusesState(getBatteryStatuses_1.initialState);
        setNonce(nonce => nonce + 1);
    }, []);
    return {
        batteryStatusesState,
        triggerRequest,
        requestCompleted,
        cancelRequest,
        isBatteryLow,
        lowBatteryPercentage,
    };
};
exports.useBatteryStatuses = useBatteryStatuses;
//# sourceMappingURL=useBatteryStatuses.js.map