"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Created by IoTize on 09/03/
 *
 * Model representing an application protocol data unit
 */
var APDUCommand = /** @class */ (function () {
    function APDUCommand() {
        /**
         * Instruction class - indicates the type of command, e.g. interindustry or proprietary
         */
        this.CLA = 0;
        /**
         * Instruction code - indicates the specific command, e.g. "write data"
         */
        this.instructionCode = 0;
        /**
         * Instruction parameters for the command, e.g. offset into file at which to write the data
         */
        this.instructionParameter = Uint8Array.from([0, 0]);
        /**
         * Encodes the number (Nc) of bytes of command data to follow
         * 0 bytes denotes Nc=0
         * 1 byte with a value from 1 to 255 denotes Nc with the same value
         * 3 bytes, the first of which must be 0, denotes Nc in the range 1 to 65 535 (all three bytes may not be zero)
         */
        this.expectedResponseSize = new Uint8Array(0);
        /**
         * Nc bytes of data
         */
        this.data = new Uint8Array(0);
        /**
         * Encodes the maximum number (Ne) of response bytes expected
         * 0 bytes denotes Ne=0
         * 1 byte in the range 1 to 255 denotes that value of Ne, or 0 denotes Ne=256
         * 2 bytes (if extended Lc was present in the command) in the range 1 to 65 535 denotes Ne of that value, or two zero bytes denotes 65 536
         * 3 bytes (if Lc was not present in the command), the first of which must be 0, denote Ne in the same way as two-byte Le
         */
        this.sizeOfCommandInBytes = new Uint8Array(0);
    }
    APDUCommand.prototype.setCLA = function (CLA) {
        this.CLA = CLA;
        return this;
    };
    APDUCommand.prototype.setInstructionCode = function (instructionCode) {
        this.instructionCode = instructionCode;
        return this;
    };
    // public setInstructionParameter(data: number[]): this {
    //     this.instructionParameter = Uint8Array.from(data);
    //     return this;
    // }
    APDUCommand.prototype.setInstructionParameter = function (p1, p2) {
        this.instructionParameter = Uint8Array.from([p1, p2]);
        return this;
    };
    /**
     * Encodes the maximum number (Ne) of response bytes expected
     * - 0 bytes denotes Ne=0
     * - 1 byte in the range 1 to 255 denotes that value of Ne, or 0 denotes Ne=256
     * - 2 bytes (if extended Lc was present in the command) in the range 1 to 65 535 denotes Ne of that value, or two zero bytes denotes 65 536
     * - 3 bytes (if Lc was not present in the command), the first of which must be 0, denote Ne in the same way as two-byte Le
     *
     * @param expectedResponseSize
     * @return
     */
    APDUCommand.prototype.setExpectedResponseSize = function (expectedResponseSize) {
        if (expectedResponseSize <= 0) {
            this.expectedResponseSize = Uint8Array.from([]);
        }
        else if (expectedResponseSize >= 1 && expectedResponseSize < 255) {
            this.expectedResponseSize = Uint8Array.from([expectedResponseSize]);
        }
        else if (expectedResponseSize == 256) {
            this.expectedResponseSize = Uint8Array.from([0]);
        }
        else {
            throw new Error("Not implemented yet");
        }
        return this;
    };
    // public setExpectedResponseSize(expectedResponseSize: number[]) {
    //     if (expectedResponseSize.length > 3) {
    //         throw new Error("Invalid expected byte size");
    //     }
    //     this.expectedResponseSize = expectedResponseSize;
    //     return this;
    // }
    APDUCommand.prototype.setData = function (data) {
        this.data = data;
        var nbBytes = data != null ? data.length : 0;
        this.setSizeOfCommandInBytes(nbBytes);
        return this;
    };
    // public setSizeOfCommandInBytes(sizeOfCommandInBytes: number[]) {
    //     this.sizeOfCommandInBytes = sizeOfCommandInBytes;
    //     return this;
    // }
    /**
     * Encodes the number (Nc) of bytes of command data to follow
     * 0 bytes denotes Nc=0
     * 1 byte with a value from 1 to 255 denotes Nc with the same value
     * 3 bytes, the first of which must be 0, denotes Nc in the range 1 to 65 535 (all three bytes may not be zero)
     *
     * @param nbBytes
     */
    APDUCommand.prototype.setSizeOfCommandInBytes = function (nbBytes) {
        if (nbBytes == 0) {
            this.sizeOfCommandInBytes = new Uint8Array(0);
        }
        else if (nbBytes >= 1 && nbBytes <= 255) {
            this.sizeOfCommandInBytes = Uint8Array.from([nbBytes & 0xFF]);
        }
        else {
            var byteTwo = (nbBytes & 0xFF00 >> 8);
            var byteOne = (nbBytes & 0xFF);
            this.sizeOfCommandInBytes = Uint8Array.from([0, byteOne, byteTwo]);
        }
    };
    APDUCommand.prototype.getCLA = function () {
        return this.CLA;
    };
    APDUCommand.prototype.getInstructionCode = function () {
        return this.instructionCode;
    };
    APDUCommand.prototype.getParameters = function () {
        return this.instructionParameter;
    };
    APDUCommand.prototype.getSizeOfCommandInBytes = function () {
        return this.sizeOfCommandInBytes;
    };
    APDUCommand.prototype.getData = function () {
        return this.data;
    };
    APDUCommand.prototype.getExpectedResponseSize = function () {
        return this.expectedResponseSize;
    };
    APDUCommand.prototype.hasExpectedResponseSize = function () {
        return this.expectedResponseSize != null && this.expectedResponseSize.length > 0;
    };
    APDUCommand.CLA_LENGTH = 1;
    APDUCommand.INS_LENGTH = 1;
    APDUCommand.P1P2_LENGTH = 2;
    return APDUCommand;
}());
exports.APDUCommand = APDUCommand;
