"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAction = exports.reducer = exports.getInitialState = 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");
// 2 minutes
const CONNECTION_TIMEOUT_MS = 120000;
const mapResult = ({ imageHash, imageSize }) => ({
    imageHash,
    imageSize,
});
const getInitialState = (device) => ({
    isLoading: !!device,
    requestQuitApp: false,
    unresponsive: false,
    device,
    deviceInfo: null,
    error: null,
    imageSize: 0,
    imageHash: "",
});
exports.getInitialState = getInitialState;
const reducer = (state, e) => {
    switch (e.type) {
        case "unresponsiveDevice":
            return { ...state, unresponsive: true, isLoading: false };
        case "deviceChange":
            return (0, exports.getInitialState)(e.device);
        case "error":
            return {
                ...(0, exports.getInitialState)(state.device),
                error: e.error,
                isLoading: false,
            };
        case "appDetected":
            return {
                ...state,
                unresponsive: false,
                requestQuitApp: true,
                isLoading: false,
            };
        case "loadImagePermissionRequested":
            return {
                ...state,
                unresponsive: false,
                imageLoadRequested: true,
                isLoading: false,
            };
        case "commitImagePermissionRequested":
            return {
                ...state,
                unresponsive: false,
                imageCommitRequested: true,
                loadingImage: false,
                isLoading: false,
            };
        case "imageLoaded":
            return {
                ...state,
                unresponsive: false,
                imageLoadRequested: false,
                imageCommitRequested: false,
                isLoading: false,
                loadingImage: false,
                imageLoaded: true,
                imageSize: e.imageSize,
                imageHash: e.imageHash,
            };
        case "progress":
            return {
                ...state,
                unresponsive: false,
                imageLoadRequested: false,
                imageCommitRequested: false,
                isLoading: false,
                loadingImage: true,
                progress: e.progress,
            };
    }
    return state; // Nb Needed until e2e tests are better handled.
};
exports.reducer = reducer;
const createAction = (task) => {
    const useHook = (device, request) => {
        const [state, setState] = (0, react_1.useState)(() => (0, exports.getInitialState)(device));
        const [resetIndex, setResetIndex] = (0, react_1.useState)(0);
        const deviceSubject = (0, observable_1.useReplaySubject)(device);
        (0, react_1.useEffect)(() => {
            if (!request)
                return;
            if (state.imageLoaded)
                return;
            const impl = (0, implementations_1.getImplementation)(app_1.currentMode)({
                config: {
                    ...implementations_1.defaultImplementationConfig,
                    connectionTimeout: CONNECTION_TIMEOUT_MS,
                },
                deviceSubject,
                task,
                request,
            });
            const sub = impl
                .pipe((0, operators_1.tap)((e) => (0, logs_1.log)("actions-load-custom-lock-screen-event", e.type, e)), (0, operators_1.scan)(exports.reducer, (0, exports.getInitialState)()))
                .subscribe(setState);
            return () => {
                sub.unsubscribe();
            };
        }, [deviceSubject, request, state.imageLoaded, resetIndex]);
        const onRetry = (0, react_1.useCallback)(() => {
            setResetIndex(currIndex => currIndex + 1);
            setState(s => (0, exports.getInitialState)(s.device));
        }, []);
        return {
            ...state,
            onRetry,
        };
    };
    return {
        useHook,
        mapResult,
    };
};
exports.createAction = createAction;
//# sourceMappingURL=customLockScreenLoad.js.map