"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAllowedHttpErrorToReconnect = exports.isHttpTimeoutError = exports.AXIOS_HTTP_TIMEOUT_ERROR_CODE = void 0;
const common_1 = require("@refinitiv-data/common");
exports.AXIOS_HTTP_TIMEOUT_ERROR_CODE = 'ECONNABORTED';
// Errors are allowed to start a new authenticating process by AccessTokenProvider
const ACCESS_DENIED_MESSAGE = 'access_denied';
const TOKEN_ERROR_DESCRIPTION_COOKIE = 'iPlanet session has been expired';
const TOKEN_ERROR_DESCRIPTION_NOT_EXISTS = 'Refresh token does not exist';
function isHttpTimeoutError(error) {
    var _a;
    return ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === common_1.HttpCode.REQUEST_TIMEOUT || (error === null || error === void 0 ? void 0 : error.code) === exports.AXIOS_HTTP_TIMEOUT_ERROR_CODE;
}
exports.isHttpTimeoutError = isHttpTimeoutError;
function isAllowedHttpErrorToReconnect(error) {
    return isIPlanetExpiredError(error) || isRefreshTokenNotExistsError(error);
}
exports.isAllowedHttpErrorToReconnect = isAllowedHttpErrorToReconnect;
function isAccessDeniedError(error) {
    return error.response.status === common_1.HttpCode.BAD_REQUEST && error.response.data.error === ACCESS_DENIED_MESSAGE;
}
function isIPlanetExpiredError(error) {
    var _a, _b;
    const description = (_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error_description;
    if (!description) {
        return false;
    }
    return isAccessDeniedError(error) && description.indexOf(TOKEN_ERROR_DESCRIPTION_COOKIE) >= 0;
}
function isRefreshTokenNotExistsError(error) {
    var _a, _b;
    const description = (_b = (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error_description;
    if (!description) {
        return false;
    }
    return isAccessDeniedError(error) && description.indexOf(TOKEN_ERROR_DESCRIPTION_NOT_EXISTS) >= 0;
}
//# sourceMappingUR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