import { of, concat } from "rxjs";
import { scan, tap } from "rxjs/operators";
import { useEffect, useState } from "react";
import { log } from "@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] = useState(value);
    useEffect(() => {
        if (!frozen) {
            setState(value);
        }
    }, [value, frozen]);
    return state;
}
export const createAction = (completeExchangeExec) => {
    const useHook = (reduxDevice, completeExchangeRequest) => {
        const [state, setState] = useState(initialState);
        const reduxDeviceFrozen = useFrozenValue(reduxDevice, state?.freezeReduxDevice);
        const { provider, transaction, binaryPayload, signature, exchange, exchangeType, rateType } = completeExchangeRequest;
        useEffect(() => {
            const sub = concat(of({
                type: "complete-exchange",
            }), completeExchangeExec({
                deviceId: reduxDeviceFrozen?.deviceId,
                deviceModelId: reduxDeviceFrozen?.modelId,
                provider,
                transaction,
                binaryPayload,
                signature,
                exchange,
                exchangeType,
                rateType,
            }))
                .pipe(tap(e => {
                log("actions-completeExchange-event", JSON.stringify(e));
            }), scan(reducer, initialState))
                .subscribe(setState);
            return () => {
                sub.unsubscribe();
            };
        }, [
            provider,
            transaction,
            binaryPayload,
            signature,
            exchange,
            exchangeType,
            rateType,
            reduxDeviceFrozen,
        ]);
        return { ...state, device: reduxDeviceFrozen };
    };
    return {
        useHook,
        mapResult,
    };
};
//# sourceMappingURL=completeExchange.js.map