"use strict";
// Copyright (c) Microsoft. All rights reserved.
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavigationErrorCode = void 0;
var tslib_1 = require("tslib");
var sp_core_library_1 = require("@microsoft/sp-core-library");
var Killswitches_1 = require("../common/Killswitches");
/**
 * Web part error codes.
 *
 * @internal
 */
var NavigationErrorCode;
(function (NavigationErrorCode) {
    /**
     * Failure to fetch navigation data.
     */
    NavigationErrorCode[NavigationErrorCode["fetchData"] = 0] = "fetchData";
    /**
     * Failure to parse navigation data.
     */
    NavigationErrorCode[NavigationErrorCode["parseData"] = 1] = "parseData";
    /**
     * Async Error
     */
    NavigationErrorCode[NavigationErrorCode["asyncError"] = 2] = "asyncError";
    /**
     * Sync Error
     */
    NavigationErrorCode[NavigationErrorCode["syncError"] = 3] = "syncError";
    /**
     * Build ID Mismatch
     */
    NavigationErrorCode[NavigationErrorCode["buildIdMismatch"] = 4] = "buildIdMismatch";
    /**
     * Unknown failure
     */
    NavigationErrorCode[NavigationErrorCode["unknown"] = 5] = "unknown";
})(NavigationErrorCode = exports.NavigationErrorCode || (exports.NavigationErrorCode = {}));
/**
 * Represents the contract of a Navigation Data Error.
 * sealed
 * @internal
 */
var NavigationDataError = /** @class */ (function (_super) {
    tslib_1.__extends(NavigationDataError, _super);
    function NavigationDataError(errorCode, errorMessage, innerError, isExpected, errorPayload) {
        if (isExpected === void 0) { isExpected = false; }
        var _this = _super.call(this, NavigationErrorCode[errorCode], errorMessage, errorPayload) || this;
        // Manually set the prototype, as we can no longer extend built-in classes like Error, Array, Map, etc
        // [https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work](https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work)
        //
        // Note: the prototype must also be set on any classes which extend this one
        _this.__proto__ = NavigationDataError.prototype; // tslint:disable-line:no-any
        _this._errorId = errorCode;
        _this.innerError = innerError;
        _this._isExpected = isExpected;
        return _this;
    }
    /**
     * Utility function to create an error response.
     * @param errorCode - Represents the navigation error code.
     * @param innerError - Represents the inner error.
     *
     * @returns An error object.
     */
    NavigationDataError.createNavigationError = function (errorCode, innerError) {
        sp_core_library_1.Validate.isNotNullOrUndefined(innerError, 'innerError');
        var isExpected = NavigationDataError._isExpectedError(errorCode);
        var errorTemplate = NavigationDataError._errorTemplate(errorCode);
        var errorMessage = sp_core_library_1.Text.format(errorTemplate, innerError.message);
        return new NavigationDataError(errorCode, errorMessage, innerError, isExpected);
    };
    /**
     * Utility function to create session ended reason
     * @param errorCode - Represents the navigation error code.
     * @param workload - Represents in which scenario/app the error originated
     *
     * @returns A string describing why the session is ending.
     */
    NavigationDataError.sessionEndedReason = function (errorCode, workload) {
        if (workload === void 0) { workload = 'XApp'; }
        var reason = 'RouteNav.';
        if (errorCode === NavigationErrorCode[NavigationErrorCode.parseData]) {
            reason += "Known." + workload + ".MalformedJSON";
        }
        else if (errorCode === NavigationErrorCode[NavigationErrorCode.fetchData]) {
            reason += "Known." + workload + ".NetworkError";
        }
        else if (errorCode === NavigationErrorCode[NavigationErrorCode.asyncError]) {
            reason += "Known." + workload + ".asyncError";
        }
        else if (errorCode === NavigationErrorCode[NavigationErrorCode.syncError]) {
            reason += "Known." + workload + ".syncError";
        }
        else if (errorCode === NavigationErrorCode[NavigationErrorCode.buildIdMismatch]) {
            reason += "Known." + workload + ".buildIdMismatch";
        }
        else {
            reason += "Unknown." + workload + ".NavigationDataError";
        }
        return reason;
    };
    NavigationDataError._isExpectedError = function (errorCode) {
        var isBuildIdMismatchExpected = Killswitches_1.Killswitches.monitorClosureKSActivated()
            ? false
            : errorCode === NavigationErrorCode.buildIdMismatch;
        return errorCode === NavigationErrorCode.parseData || isBuildIdMismatchExpected;
    };
    NavigationDataError._errorTemplate = function (errorCode) {
        return NavigationDataError._errorCodeToTemplateMap[errorCode] || 'Failed to navigate. Error: {0}';
    };
    Object.defineProperty(NavigationDataError.prototype, "id", {
        /**
         * Gets the error Id.
         */
        get: function () {
            return this._errorId;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(NavigationDataError.prototype, "category", {
        /**
         * Gets the error category.
         */
        get: function () {
            return 'NavigationDataError';
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(NavigationDataError.prototype, "isExpected", {
        /**
         * Gets a value indicating whether the error is expected.
         */
        get: function () {
            return this._isExpected;
        },
        enumerable: false,
        configurable: true
    });
    NavigationDataError._errorCodeToTemplateMap = new Map([
        [NavigationErrorCode.fetchData, 'Failed to retrieve navigation data. Error: {0}'],
        [NavigationErrorCode.parseData, 'Failed to parse navigation data. Error: {0}'],
        [NavigationErrorCode.asyncError, 'Async Error. Error: {0}'],
        [NavigationErrorCode.syncError, 'Sync Error. Error: {0}'],
        [NavigationErrorCode.buildIdMismatch, 'Build ID mismatch. Error: {0}']
    ]);
    return NavigationDataError;
}(sp_core_library_1.SPError));
exports.default = NavigationDataError;
//# sourceMappingURL=NavigationDataError.js.map