"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeProtocolFromURL = exports.captureException = exports.isApiError = exports.getApiErrorCode = exports.isUnknownServerError = exports.getApiErrorMessage = exports.handleApiError = exports.PAGE_RELOAD_ERROR = exports.SERVER_ERROR_CODE = exports.FETCH_CONNECTIVITY_ERROR_CODE = exports.FETCH_ABORTED_ERROR_CODE = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const Sentry = tslib_1.__importStar(require("@sentry/browser"));
const pick_js_1 = tslib_1.__importDefault(require("lodash-es/pick.js"));
const utils_1 = require("../utils");
const axiosExtensions_1 = require("./axiosExtensions");
const axiosClient_1 = require("./axiosClient");
exports.FETCH_ABORTED_ERROR_CODE = 'ERR_CANCELED';
exports.FETCH_CONNECTIVITY_ERROR_CODE = 'CONNECTIVITY_ERROR';
exports.SERVER_ERROR_CODE = 'SERVER_ERROR';
exports.PAGE_RELOAD_ERROR = 'ECONNABORTED';
const handleApiError = (error, onError) => {
    if (axios_1.default.isCancel(error)) {
        (0, exports.captureException)(error, 'Request canceled');
    }
    else if ((0, exports.isApiError)(error)) {
        const config = error.config || { url: '', method: '' };
        let message = `URL: ${JSON.stringify(config.url, null, 1)}\n`;
        message += `Method: ${JSON.stringify(config.method, null, 1)}\n`;
        if (error.response) {
            // The request was made and the server responded with a status code
            // that falls out of the range of 2xx
            message += `Status: ${error.response.status}\n`;
            message += `Response: ${JSON.stringify((0, pick_js_1.default)(error.response.data, ['code', 'message', 'reason']), null, 1)}\n`;
        }
        else if (error.request) {
            // The request was made but no response was received
            // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js
            message += `Status Code: ${JSON.stringify(
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
            error.request.__sentry_xhr__.status_code, null, 1)}`;
        }
        (0, exports.captureException)(error, message);
    }
    else {
        (0, exports.captureException)(error);
    }
    if (onError)
        return onError(error);
};
exports.handleApiError = handleApiError;
const getApiErrorMessage = (error) => {
    var _a, _b, _c;
    if ((0, exports.isApiError)(error)) {
        return ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || ((_c = error.response) === null || _c === void 0 ? void 0 : _c.data.reason) || error.message;
    }
    return (0, utils_1.getErrorMessage)(error);
};
exports.getApiErrorMessage = getApiErrorMessage;
const isUnknownServerError = (error) => {
    return (0, exports.getApiErrorCode)(error) === exports.SERVER_ERROR_CODE;
};
exports.isUnknownServerError = isUnknownServerError;
const getApiErrorCode = (error) => {
    var _a;
    if (!(0, axiosExtensions_1.isAxiosError)(error)) {
        return exports.FETCH_CONNECTIVITY_ERROR_CODE;
    }
    // Aborted request
    if (error.code === exports.FETCH_ABORTED_ERROR_CODE) {
        return exports.FETCH_ABORTED_ERROR_CODE;
    }
    // Page is reloading and some request has been interrupted
    if (error.code === exports.PAGE_RELOAD_ERROR) {
        return '';
    }
    const responseStatus = ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) || 0;
    // Error status
    if (responseStatus >= 500 && responseStatus < 600) {
        return exports.SERVER_ERROR_CODE;
    }
    if (responseStatus >= 400 && responseStatus < 500) {
        return responseStatus;
    }
    // A generic connectivity issue
    return exports.FETCH_CONNECTIVITY_ERROR_CODE;
};
exports.getApiErrorCode = getApiErrorCode;
const isApiError = (error) => {
    var _a;
    if (!(0, axiosExtensions_1.isAxiosError)(error)) {
        return false;
    }
    return typeof ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === 'object';
};
exports.isApiError = isApiError;
const captureException = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error, message) => {
    if (axiosClient_1.isInOcm) {
        message && Sentry.captureMessage(message);
        Sentry.captureException(error);
    }
    else {
        // severity === Sentry.Severity.Error
        //   ? console.error(message, error)
        //   : console.warn(message, error);
    }
};
exports.captureException = captureException;
const removeProtocolFromURL = (url = '') => url.replace(/^(http|https):\/\//, '');
exports.removeProtocolFromURL = removeProtocolFromURL;
//# sourceMappingURL=utils.js.map