"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 errors_1 = require("@ledgerhq/errors");
const observable_1 = require("../../observable");
const app_1 = require("./app");
const devices_1 = require("@ledgerhq/devices");
const implementations_1 = require("./implementations");
const mapResult = (status) => status.name;
const getInitialState = (device) => ({
    isLoading: !!device,
    allowRenamingRequested: false,
    unresponsive: false,
    name: "",
    device,
    deviceInfo: null,
    error: null,
    completed: false,
});
const reducer = (state, e) => {
    switch (e.type) {
        case "unresponsiveDevice":
            return { ...state, unresponsive: true, isLoading: false };
        case "deviceChange":
            return getInitialState(e.device);
        case "disconnected":
            return {
                ...getInitialState(state.device),
                isLoading: !!e.expected,
            };
        case "error":
            return {
                ...getInitialState(state.device),
                error: e.error,
                isLoading: false,
            };
        case "permission-requested":
            return {
                ...state,
                allowRenamingRequested: true,
                unresponsive: false,
                isLoading: false,
            };
        case "device-renamed":
            return {
                ...getInitialState(state.device),
                name: e.name,
                completed: true,
                isLoading: false,
            };
    }
    return state;
};
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);
        // Changing the nonce causing a refresh of the useEffect
        const onRetry = (0, react_1.useCallback)(() => {
            setResetIndex(i => i + 1);
            setState(getInitialState(device));
        }, [device]);
        const productName = (0, react_1.useMemo)(() => (device ? (0, devices_1.getDeviceModel)(device.modelId).productName : ""), [device]);
        (0, react_1.useEffect)(() => {
            if (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-rename-device-event", e.type, e)), (0, operators_1.map)((e) => {
                if (productName &&
                    e.type === "error" &&
                    e.error instanceof errors_1.UserRefusedDeviceNameChange) {
                    e.error = new errors_1.UserRefusedDeviceNameChange(undefined, {
                        productName,
                    });
                }
                return e;
            }), (0, operators_1.scan)(reducer, getInitialState()))
                .subscribe(setState);
            return () => {
                sub.unsubscribe();
            };
        }, [deviceSubject, state.completed, resetIndex, productName, request]);
        return {
            ...state,
            onRetry,
        };
    };
    return {
        useHook,
        mapResult,
    };
};
exports.createAction = createAction;
//# sourceMappingURL=renameDevice.js.map