"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAction = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const react_1 = require("react");
const logs_1 = require("@ledgerhq/logs");
const mapResult = ({ completeExchangeResult, completeExchangeError, }) => completeExchangeResult
    ? {
        completeExchangeResult,
    }
    : completeExchangeError
        ? {
            completeExchangeError,
        }
        : null;
const initialState = {
    completeExchangeResult: null,
    completeExchangeError: null,
    completeExchangeRequested: false,
    freezeReduxDevice: false,
    isLoading: true,
    estimatedFees: undefined,
};
const reducer = (state, e) => {
    switch (e.type) {
        case "complete-exchange":
            return {
                ...state,
                completeExchangeStarted: true,
                freezeReduxDevice: true,
            };
        case "complete-exchange-error":
            return {
                ...state,
                completeExchangeError: e.error,
                isLoading: false,
            };
        case "complete-exchange-requested":
            return {
                ...state,
                estimatedFees: e.estimatedFees,
                isLoading: false,
            };
        case "complete-exchange-result":
            return {
                ...state,
                completeExchangeResult: e.completeExchangeResult,
                isLoading: false,
            };
    }
    return state;
};
function useFrozenValue(value, frozen) {
    const [state, setState] = (0, react_1.useState)(value);
    (0, react_1.useEffect)(() => {
        if (!frozen) {
            setState(value);
        }
    }, [value, frozen]);
    return state;
}
const createAction = (completeExchangeExec) => {
    const useHook = (reduxDevice, completeExchangeRequest) => {
        const [state, setState] = (0, react_1.useState)(initialState);
        const reduxDeviceFrozen = useFrozenValue(reduxDevice, state?.freezeReduxDevice);
        const { provider, transaction, binaryPayload, signature, exchange, exchangeType, rateType } = completeExchangeRequest;
        (0, react_1.useEffect)(() => {
            const sub = (0, rxjs_1.concat)((0, rxjs_1.of)({
                type: "complete-exchange",
            }), completeExchangeExec({
                deviceId: reduxDeviceFrozen?.deviceId,
                deviceModelId: reduxDeviceFrozen?.modelId,
                provider,
                transaction,
                binaryPayload,
                signature,
                exchange,
                exchangeType,
                rateType,
            }))
                .pipe((0, operators_1.tap)(e => {
                (0, logs_1.log)("actions-completeExchange-event", JSON.stringify(e));
            }), (0, operators_1.scan)(reducer, initialState))
                .subscribe(setState);
            return () => {
                sub.unsubscribe();
            };
        }, [
            provider,
            transaction,
            binaryPayload,
            signature,
            exchange,
            exchangeType,
            rateType,
            reduxDeviceFrozen,
        ]);
        return { ...state, device: reduxDeviceFrozen };
    };
    return {
        useHook,
        mapResult,
    };
};
exports.createAction = createAction;
//# sourceMappingURL=completeExchange.js.map