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 { bufferToHexString } from '@iotize/common/byte-converter';
import { CodeError } from '@iotize/common/error';
import { ApduResponse } from '@iotize/tap/client/api';
import { TapRequestHelper } from './request/tap-request-helper';
import { ResultCodeTranslation } from './response/result-code-translation';
var TapClientError = /** @class */ (function (_super) {
    __extends(TapClientError, _super);
    function TapClientError(code, message, cause) {
        var _this = _super.call(this, message, code) || this;
        _this.cause = cause;
        return _this;
    }
    TapClientError.illegalArgument = function (msg) {
        return new TapClientError(TapClientError.Code.IllegalArgumentError, msg);
    };
    TapClientError.illegalStateError = function (msg) {
        return new TapClientError(TapClientError.Code.IllegalStateError, msg);
    };
    TapClientError.encodeRequestError = function (command, cause) {
        return new TapClientError(TapClientError.Code.EncodeRequestError, 'Cannot encode this request: ' + command + '. Cause: ' + cause, cause);
    };
    TapClientError.decodeResponseError = function (cause, frameOrCommand) {
        return new TapClientError(TapClientError.Code.DecodeResponseError, "Cannot decode response to command " + (frameOrCommand instanceof Uint8Array
            ? "0x" + bufferToHexString(frameOrCommand)
            : TapRequestHelper.toString(frameOrCommand)) + ". Cause: " + cause, cause);
    };
    TapClientError.notConnectedError = function () {
        return new TapClientError(TapClientError.Code.NotConnectedError, 'Trying to execute command but device is not connected');
    };
    TapClientError.cannotDecodeResponseError = function (err, bufferData) {
        return new TapClientError(TapClientError.Code.UnexpectedTapResponse, "Received from device an unexpected response that cannot be decoded (" + err.message + "). Frame: 0x" + bufferToHexString(bufferData));
    };
    TapClientError.unexpectedApduStatusCode = function (apduResponse) {
        return new TapClientError(TapClientError.Code.UnexpectedApduResponseStatus, "Received from device an unexpected response with an invalid APDU status code 0x" + apduResponse.status.toString(16) + "\n            but expecting 0x" + ApduResponse.Status.OK.toString(16));
    };
    TapClientError.cannotEncodeRequest = function (err, tapRequest) {
        return new TapClientError(TapClientError.Code.CannotEncodeRequest, "Cannot encode request " + tapRequest + ": " + err.message);
    };
    return TapClientError;
}(CodeError));
export { TapClientError };
(function (TapClientError) {
    var Code;
    (function (Code) {
        Code["NotConnectedError"] = "TapClientErrorNotConnected";
        Code["EncodeRequestError"] = "TapClientErrorEncodeRequest";
        Code["IllegalArgumentError"] = "TapClientErrorIllegalArgument";
        Code["IllegalStateError"] = "TapClientErrorIllegalState";
        Code["DecodeResponseError"] = "TapClientErrorDecodeResponse";
        Code["UnexpectedTapResponse"] = "TapClientErrorUnexpectedTapResponse";
        Code["UnexpectedApduResponseStatus"] = "TapClientErrorUnexpectedApduResponseStatus";
        Code["CannotEncodeRequest"] = "TapClientErrorCannotEncodeRequest";
        Code["TapClientResponseStatusError"] = "TapClientErrorResponseStatus";
    })(Code = TapClientError.Code || (TapClientError.Code = {}));
})(TapClientError || (TapClientError = {}));
// export const TapResponseErrorCode = 'TapResponseError';
var TapClientResponseStatusError = /** @class */ (function (_super) {
    __extends(TapClientResponseStatusError, _super);
    // public code: string;
    function TapClientResponseStatusError(response, request) {
        var _this = _super.call(this, TapClientError.Code.TapClientResponseStatusError, TapClientResponseStatusError.createErrorMessage(response, request)) || this;
        _this.response = response;
        _this.request = request;
        return _this;
    }
    TapClientResponseStatusError.createErrorMessage = function (response, request) {
        var codeRet = response.status;
        var msg = 'Tap request failed. ';
        if (request) {
            msg = "Tap request " + TapRequestHelper.toString(request) + " failed. ";
        }
        var errorExplained = codeRet in ResultCodeTranslation
            ? ResultCodeTranslation[codeRet]
            : 'an unknown error';
        msg += errorExplained + " (code=0x" + codeRet.toString(16) + ")";
        return msg;
    };
    return TapClientResponseStatusError;
}(TapClientError));
export { TapClientResponseStatusError };
//# sourceMappingURL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