import { getErrorCode, getErrorMessage } from "../../api/services/utils";
import { ComponentErrorSeverity } from "../../api/ComponentError";
export const makeGetQuote = ({ authToken, service }) => async ({ payload, onSuccess, onError }) => {
    var _a;
    try {
        const response = await service.fetchQuote(authToken, payload);
        if (!response.error) {
            const quote = response.data;
            onSuccess({ quote });
        }
        else {
            const responseError = getErrorMessage(response.error);
            const code = getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.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,
        });
    }
};
export const makeToggleCoverage = ({ authToken, service }) => async ({ quoteId, payload, onSuccess, onError }) => {
    var _a;
    try {
        const response = await service.toggleCoverage(authToken, quoteId, payload);
        if (!response.error) {
            const quote = response.data;
            onSuccess({ quote });
        }
        else {
            const responseError = getErrorMessage(response.error);
            const code = getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.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,
        });
    }
};
