import { ApduResponse } from '@iotize/tap/client/api';
import { createApduFromTapRequest } from './apdu/tap-apdu-request-builder';
import { TapStreamReader, TapStreamWriter } from './frame/import-adapter';
import { TapClientError } from './tap-client-error';
var TapApduRequestConverter = /** @class */ (function () {
    function TapApduRequestConverter() {
    }
    TapApduRequestConverter.prototype.encode = function (tapRequest) {
        try {
            var stream = new TapStreamWriter(5 + 1 + tapRequest.payload.length + 1);
            stream.writeApduRequest(createApduFromTapRequest(tapRequest));
            return stream.toBytes;
        }
        catch (err) {
            if (!(err instanceof TapClientError)) {
                throw TapClientError.cannotEncodeRequest(err, tapRequest);
            }
            else {
                throw err;
            }
        }
    };
    TapApduRequestConverter.prototype.decode = function (bufferData) {
        var stream = bufferData instanceof TapStreamReader ? bufferData : TapStreamReader.create(bufferData);
        var apduHeader = stream.readApduRequestHeader();
        var tapRequestFrame = stream.readTapRequestFrame();
        return tapRequestFrame;
    };
    return TapApduRequestConverter;
}());
export { TapApduRequestConverter };
var TapApduResponseConverter = /** @class */ (function () {
    function TapApduResponseConverter() {
    }
    TapApduResponseConverter.prototype.encode = function (tapResponseFrame) {
        var stream = new TapStreamWriter();
        var tapRequestStream = new TapStreamWriter();
        tapRequestStream.writeTapResponseFrame(tapResponseFrame);
        stream.writeApduResponse({
            data: tapRequestStream.toBytes,
            status: ApduResponse.Status.OK,
        });
        return stream.toBytes;
    };
    TapApduResponseConverter.prototype.decode = function (bufferData) {
        try {
            var stream = bufferData instanceof TapStreamReader ? bufferData : TapStreamReader.create(bufferData);
            var apduResponse = stream.readApduResponse();
            if (apduResponse.status !== ApduResponse.Status.OK) {
                throw TapClientError.unexpectedApduStatusCode(apduResponse);
            }
            return TapStreamReader.fromArray(apduResponse.data).readTapResponseFrame();
        }
        catch (err) {
            if (!(err instanceof TapClientError)) {
                throw TapClientError.cannotDecodeResponseError(err, bufferData instanceof TapStreamReader ? bufferData.toBytes : bufferData);
            }
            else {
                throw err;
            }
        }
    };
    return TapApduResponseConverter;
}());
export { TapApduResponseConverter };
export var TAP_REQUEST_FRAME_CONVERTER = new TapApduRequestConverter();
export var TAP_RESPONSE_FRAME_CONVERTER = new TapApduResponseConverter();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udmVydGVycy5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2NsaWVudC9pbXBsLyIsInNvdXJjZXMiOlsibGliL2NvbnZlcnRlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLFlBQVksRUFBcUMsTUFBTSx3QkFBd0IsQ0FBQztBQUV6RixPQUFPLEVBQUUsd0JBQXdCLEVBQUUsTUFBTSxpQ0FBaUMsQ0FBQztBQUMzRSxPQUFPLEVBQUUsZUFBZSxFQUFFLGVBQWUsRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBQzFFLE9BQU8sRUFBRSxjQUFjLEVBQUUsTUFBTSxvQkFBb0IsQ0FBQztBQUVwRDtJQUFBO0lBNEJBLENBQUM7SUExQkcsd0NBQU0sR0FBTixVQUFPLFVBQTJCO1FBQzlCLElBQUk7WUFDQSxJQUFNLE1BQU0sR0FBRyxJQUFJLGVBQWUsQ0FDOUIsQ0FBQyxHQUFHLENBQUMsR0FBRyxVQUFVLENBQUMsT0FBTyxDQUFDLE1BQU0sR0FBRyxDQUFDLENBQ3hDLENBQUM7WUFDRixNQUFNLENBQUMsZ0JBQWdCLENBQ25CLHdCQUF3QixDQUFDLFVBQVUsQ0FBQyxDQUN2QyxDQUFDO1lBQ0YsT0FBTyxNQUFNLENBQUMsT0FBTyxDQUFDO1NBQ3pCO1FBQ0QsT0FBTyxHQUFHLEVBQUU7WUFDUixJQUFJLENBQUMsQ0FBQyxHQUFHLFlBQVksY0FBYyxDQUFDLEVBQUU7Z0JBQ2xDLE1BQU0sY0FBYyxDQUFDLG1CQUFtQixDQUFDLEdBQUcsRUFBRSxVQUFVLENBQUMsQ0FBQzthQ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