"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAction = void 0;
const operators_1 = require("rxjs/operators");
const react_1 = require("react");
const logs_1 = require("@ledgerhq/logs");
const observable_1 = require("../../observable");
const app_1 = require("./app");
const implementations_1 = require("./implementations");
const mapResult = ({ completed, imageFetched, imageAlreadyBackedUp }) => completed || imageFetched || imageAlreadyBackedUp || null;
const getInitialState = (device) => ({
    isLoading: !!device,
    requestQuitApp: false,
    unresponsive: false,
    fetchingImage: false,
    device,
    deviceInfo: null,
    error: null,
    imageAlreadyBackedUp: false,
});
const reducer = (state, e) => {
    switch (e.type) {
        case "unresponsiveDevice":
            return { ...state, unresponsive: true, isLoading: false };
        case "deviceChange":
            return getInitialState(e.device);
        case "error":
            return {
                ...getInitialState(state.device),
                error: e.error,
                isLoading: false,
            };
        case "appDetected":
            return {
                ...state,
                error: null,
                unresponsive: false,
                requestQuitApp: true,
                isLoading: false,
            };
        case "imageFetched":
            return {
                ...state,
                error: null,
                unresponsive: false,
                isLoading: false,
                fetchingImage: false,
                imageFetched: true,
                hexImage: e.hexImage,
            };
        case "currentImageHash":
            return {
                ...state,
                error: null,
                unresponsive: false,
                isLoading: false,
                fetchingImage: true,
                imgHash: e.imgHash,
            };
        case "imageAlreadyBackedUp":
            return {
                ...state,
                error: null,
                unresponsive: false,
                isLoading: false,
                fetchingImage: false,
                imageFetched: false,
                imageAlreadyBackedUp: true,
                completed: true,
            };
        case "progress":
            return {
                ...state,
                error: null,
                unresponsive: false,
                isLoading: false,
                fetchingImage: true,
                progress: e.progress,
            };
    }
    return state; // Nb Needed until e2e tests are better handled.
};
const createAction = (task) => {
    const useHook = (device, request) => {
        const [state, setState] = (0, react_1.useState)(() => getInitialState(device));
        const [resetIndex, setResetIndex] = (0, react_1.useState)(0);
        const deviceSubject = (0, observable_1.useReplaySubject)(device);
        (0, react_1.useEffect)(() => {
            if (state.imageFetched || state.imageAlreadyBackedUp || state.completed)
                return;
            const impl = (0, implementations_1.getImplementation)(app_1.currentMode)({
                deviceSubject,
                task,
                request,
            });
            const sub = impl
                .pipe((0, operators_1.tap)((e) => (0, logs_1.log)("actions-fetch-custom-lock-screen-event", e.type, e)), (0, operators_1.scan)(reducer, getInitialState()))
                .subscribe(setState);
            return () => {
                sub.unsubscribe();
            };
        }, [
            deviceSubject,
            request,
            state.completed,
            state.imageAlreadyBackedUp,
            state.imageFetched,
            resetIndex,
        ]);
        const onRetry = (0, react_1.useCallback)(() => {
            setResetIndex(currIndex => currIndex + 1);
            setState(s => getInitialState(s.device));
        }, []);
        return {
            ...state,
            onRetry,
        };
    };
    return {
        useHook,
        mapResult,
    };
};
exports.createAction = createAction;
//# sourceMappingURL=customLockScreenFetch.js.map