var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import { CodeError } from '@iotize/common/error';
import { TapRequestFrame } from '@iotize/tap/client/api';
import { tapResponseStatusToString } from '@iotize/tap/client/impl';
import { serviceCallToString } from './service-call-runner';
var TapError = /** @class */ (function (_super) {
    __extends(TapError, _super);
    function TapError(code, message, cause) {
        var _this = _super.call(this, message, code) || this;
        _this.cause = cause;
        return _this;
    }
    TapError.cannotStartScram = function (err) {
        return new TapError(TapError.Code.CannotStartScram, "Cannot start encrypted session, initialization failed. Cause: " + err.message, err);
    };
    TapError.notImplementedYet = function (msg) {
        throw new TapError(TapError.Code.NotImplemented, msg);
    };
    TapError.initializationVectorNotSupported = function (err) {
        return new TapError(TapError.Code.InitializationVectorNotSupported, "Initialization vector for SCRAM is not implemented with your current firmware version. You cannot use this feature.", err);
    };
    TapError.executeRequestError = function (request, err) {
        return new TapError(TapError.Code.ExecuteRequestError, "Tap request " + TapRequestFrame.MethodType[request.method] + " " + (request.pathAlias || request.path) + " failed. " + err.message, err);
    };
    TapError.reponseStatusError = function (response, call) {
        return new TapResponseStatusError(response, call);
    };
    TapError.invalidServiceCallConfiguration = function (msg) {
        return new TapError(TapError.Code.InvalidServiceCallConfiguration, msg);
    };
    TapError.appendChunkError = function (cause, chunk, progress) {
        return new AppendChunkError(cause, chunk, progress);
    };
    TapError.nfcPairingError = function (msg, cause) {
        return new TapError(TapError.Code.NfcPairingError, "NFC pairing failed. " + msg, cause);
    };
    return TapError;
}(CodeError));
export { TapError };
(function (TapError) {
    var Code;
    (function (Code) {
        Code["CannotStartScram"] = "TapErrorCannotStartScram";
        Code["NotImplemented"] = "TapErrorNotImplemented";
        Code["InitializationVectorNotSupported"] = "TapErrorInitializationVectorNotSupported";
        Code["AppendChunkError"] = "AppendChunkError";
        Code["ResponseStatusError"] = "TapErrorTapResponseStatusError";
        Code["InvalidServiceCallConfiguration"] = "TapErrorInvalidServiceCallConfiguration";
        Code["ExecuteRequestError"] = "TapErrorTapExecuteRequestError";
        Code["NfcPairingError"] = "TapErrorNfcPairingError";
    })(Code = TapError.Code || (TapError.Code = {}));
})(TapError || (TapError = {}));
var TapResponseStatusError = /** @class */ (function (_super) {
    __extends(TapResponseStatusError, _super);
    function TapResponseStatusError(response, call) {
        var _this = _super.call(this, TapError.Code.ResponseStatusError, "Tap request " + (call ? serviceCallToString(call) + " " : (response.request ? response.request.toString() + ' ' : '')) + "failed. " + tapResponseStatusToString(response.status) + " (status code 0x" + response.status.toString(16) + ")") || this;
        _this.response = response;
        return _this;
    }
    return TapResponseStatusError;
}(TapError));
export { TapResponseStatusError };
var AppendChunkError = /** @class */ (function (_super) {
    __extends(AppendChunkError, _super);
    function AppendChunkError(cause, chunk, progress) {
        var _this = _super.call(this, TapError.Code.AppendChunkError, "Appending data chunk n\u00B0" + progress.loaded + "/" + progress.total + " failed with error: " + cause.message, cause) || this;
        _this.chunk = chunk;
        _this.progress = progress;
        return _this;
    }
    return AppendChunkError;
}(TapError));
export { AppendChunkError };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWVycm9ycy5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwLyIsInNvdXJjZXMiOlsibGliL3RhcC1lcnJvcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7OztBQUFBLE9BQU8sRUFBRSxTQUFTLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQUNqRCxPQUFPLEVBQUUsZUFBZSxFQUFFLE1BQU0sd0JBQXdCLENBQ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