import { Terminal } from "../../api";
import { ComponentErrorSeverity } from "../../api/ComponentError";
import { getErrorCode, getErrorMessage } from "../../api/services/utils";
export const makeGetTerminals = ({ id, authToken, service }) => async ({ params, onSuccess, onError }) => {
    var _a, _b;
    try {
        const response = await service.fetchTerminals(id, authToken, params);
        if (!response.error) {
            const pagingInfo = Object.assign({}, response.page_info);
            const terminals = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.map((dataItem) => new Terminal(dataItem))) || [];
            onSuccess({ terminals, pagingInfo });
        }
        else {
            const responseError = getErrorMessage(response.error);
            const code = getErrorCode((_b = response.error) === null || _b === void 0 ? void 0 : _b.code);
            return onError({
                error: responseError,
                code,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        return onError({
            error: error.message || error,
            code,
            severity: ComponentErrorSeverity.ERROR,
        });
    }
};
