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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWNsaWVudC1lcnJvci5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2NsaWVudC9pbXBsLyIsInNvdXJjZXMiOlsibGliL3RhcC1jbGllbnQtZXJyb3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGlCQUFpQixFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFDbEUsT0FBTyxFQUFFLFNBQVMsRUFBRSxNQUFNLHNCQUFzQixDQUFDO0FBQ2pELE9BQU8sRUFBRSxZQUFZLEVBQXFDLE1BQU0sd0JBQXdCLENBQUM7QUFFekYsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0sOEJBQThCLENBQUM7QUFDaEUsT0FBTyxFQUFFLHFCQUFxQixFQUFFLE1BQU0sb0NBQ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