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';
export class TapClientError extends CodeError {
    constructor(code, message, cause) {
        super(message, code);
        this.cause = cause;
    }
    static illegalArgument(msg) {
        return new TapClientError(TapClientError.Code.IllegalArgumentError, msg);
    }
    static illegalStateError(msg) {
        return new TapClientError(TapClientError.Code.IllegalStateError, msg);
    }
    static encodeRequestError(command, cause) {
        return new TapClientError(TapClientError.Code.EncodeRequestError, 'Cannot encode this request: ' + command + '. Cause: ' + cause, cause);
    }
    static decodeResponseError(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);
    }
    static notConnectedError() {
        return new TapClientError(TapClientError.Code.NotConnectedError, 'Trying to execute command but device is not connected');
    }
    static cannotDecodeResponseError(err, bufferData) {
        return new TapClientError(TapClientError.Code.UnexpectedTapResponse, `Received from device an unexpected response that cannot be decoded (${err.message}). Frame: 0x${bufferToHexString(bufferData)}`);
    }
    static unexpectedApduStatusCode(apduResponse) {
        return new TapClientError(TapClientError.Code.UnexpectedApduResponseStatus, `Received from device an unexpected response with an invalid APDU status code 0x${apduResponse.status.toString(16)}
            but expecting 0x${ApduResponse.Status.OK.toString(16)}`);
    }
    static cannotEncodeRequest(err, tapRequest) {
        return new TapClientError(TapClientError.Code.CannotEncodeRequest, `Cannot encode request ${tapRequest}: ${err.message}`);
    }
}
(function (TapClientError) {
    let 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';
export class TapClientResponseStatusError extends TapClientError {
    // public code: string;
    constructor(response, request) {
        super(TapClientError.Code.TapClientResponseStatusError, TapClientResponseStatusError.createErrorMessage(response, request));
        this.response = response;
        this.request = request;
    }
    static createErrorMessage(response, request) {
        const codeRet = response.status;
        let msg = 'Tap request failed. ';
        if (request) {
            msg = `Tap request ${TapRequestHelper.toString(request)} failed. `;
        }
        const errorExplained = codeRet in ResultCodeTranslation
            ? ResultCodeTranslation[codeRet]
            : 'an unknown error';
        msg += `${errorExplained} (code=0x${codeRet.toString(16)})`;
        return msg;
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWNsaWVudC1lcnJvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL2NsaWVudC9pbXBsL3NyYy9saWIvdGFwLWNsaWVudC1lcnJvci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUNsRSxPQUFPLEVBQUUsU0FBUyxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDakQsT0FBTyxFQUNMLFlBQVksR0FHYixNQUFNLHdCQUF3QixDQUFDO0FBRWhDLE9BQU8sRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLDhCQUE4QixDQUFDO0FBQ2hFLE9BQU8sRUFBRSxxQkFBcUI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