import { log } from "@ledgerhq/logs";
import { useEffect, useState, useMemo } from "react";
import { concat, of } from "rxjs";
import { catchError, scan, tap } from "rxjs/operators";
import { getMainAccount } from "../../account";
import { createAction as createAppAction } from "./app";
const mapResult = ({ initSwapResult, initSwapError, swapId, }) => initSwapResult
    ? {
        initSwapResult,
    }
    : initSwapError
        ? {
            initSwapError,
            swapId,
        }
        : null;
const initialState = {
    initSwapResult: null,
    initSwapError: null,
    swapId: undefined,
    initSwapRequested: false,
    isLoading: true,
    freezeReduxDevice: false,
};
const reducer = (state, e) => {
    switch (e.type) {
        case "init-swap":
            return { ...state, freezeReduxDevice: true };
        case "init-swap-error":
            return {
                ...state,
                initSwapError: e.error,
                swapId: e.swapId,
                isLoading: false,
            };
        case "init-swap-requested":
            return {
                ...state,
                initSwapRequested: true,
                isLoading: false,
                amountExpectedTo: e.amountExpectedTo,
                estimatedFees: e.estimatedFees,
            };
        case "init-swap-result":
            return {
                ...state,
                initSwapResult: e.initSwapResult,
                isLoading: false,
            };
    }
};
function useFrozenValue(value, frozen) {
    const [state, setState] = useState(value);
    useEffect(() => {
        if (!frozen) {
            setState(value);
        }
    }, [value, frozen]);
    return state;
}
export const createAction = (connectAppExec, initSwapExec) => {
    const useHook = (reduxDevice, initSwapRequest) => {
        const [state, setState] = useState(initialState);
        const reduxDeviceFrozen = useFrozenValue(reduxDevice, state.freezeReduxDevice);
        const { exchange, exchangeRate, transaction, requireLatestFirmware } = initSwapRequest;
        const { fromAccount, fromParentAccount, toAccount, toParentAccount } = exchange;
        const mainFromAccount = getMainAccount(fromAccount, fromParentAccount);
        const maintoAccount = getMainAccount(toAccount, toParentAccount);
        const request = useMemo(() => {
            return {
                appName: "Exchange",
                dependencies: [
                    {
                        account: mainFromAccount,
                    },
                    {
                        account: maintoAccount,
                    },
                ],
                requireLatestFirmware,
            };
        }, [mainFromAccount, maintoAccount, requireLatestFirmware]);
        const appState = createAppAction(connectAppExec).useHook(reduxDeviceFrozen, request);
        const { device, opened, error } = appState;
        const hasError = error || state.error;
        useEffect(() => {
            if (!opened || !device) {
                setState({ ...initialState, isLoading: !!device });
                return;
            }
            const sub = concat(of({
                type: "init-swap",
            }), initSwapExec({
                exchange,
                exchangeRate,
                transaction,
                deviceId: device.deviceId,
            }))
                .pipe(tap(e => {
                log("actions-initSwap-event", e.type, e);
            }), catchError((error) => of({
                type: "init-swap-error",
                error,
            })), scan(reducer, { ...initialState, isLoading: !hasError }))
                .subscribe(setState);
            return () => {
                sub.unsubscribe();
            };
        }, [exchange, exchangeRate, transaction, device, opened, hasError]);
        return {
            ...appState,
            ...state,
        };
    };
    return {
        useHook,
        mapResult,
    };
};
//# sourceMappingURL=initSwap.js.map