"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("../../../core");
var request_1 = require("../../api/request");
var lwm2m_command_converter_1 = require("./lwm2m-command-converter");
/**
 * TODO replace with generated frame converter
 */
var DefaultCommandEncoder = /** @class */ (function () {
    function DefaultCommandEncoder() {
        this.apduCommandConverter = new core_1.APDU.Command.APDUCommandConverter();
        this.iotizeCommandConverter = new lwm2m_command_converter_1.IotizeLWM2MCommandConverter();
    }
    DefaultCommandEncoder.instance = function () {
        return new DefaultCommandEncoder();
    };
    DefaultCommandEncoder.prototype.encode = function (request) {
        var iotizeTram = this.encodeAsIoTizeTrame(request);
        return this.encodeAsAPDU(request, iotizeTram);
    };
    DefaultCommandEncoder.prototype.encodeAsAPDU = function (request, iotizeFrame) {
        var command = new core_1.APDU.Command.APDUCommand();
        command.setCLA(DefaultCommandEncoder.IOTIZE_CLA);
        switch (request.getMethod()) {
            case request_1.MethodType.GET:
                command.setInstructionCode(DefaultCommandEncoder.IOTIZE_INSTRUCTION_CODE_GET);
                break;
            case request_1.MethodType.POST:
            case request_1.MethodType.PUT:
            default:
                command.setInstructionCode(DefaultCommandEncoder.IOTIZE_INSTRUCTION_CODE_PUT);
        }
        command.setData(iotizeFrame);
        var apduResponse = this.apduCommandConverter.encode(command);
        return apduResponse;
    };
    DefaultCommandEncoder.prototype.encodeAsIoTizeTrame = function (command) {
        return this.iotizeCommandConverter.encode(command); // TODO fix
        // var header = command.getHeader();
        // var IdObj = header.getObjectId();
        // var IdInstObj = header.getObjectInstance();
        // var IdRes = header.getResourceId();
        // var method = command.getMethod();
        // // set the header
        // var lg = 0;
        // var lgDataIn = 0;
        // var data = command.getData();
        // if (data != null) {
        //     if (data.length >= DefaultCommandEncoder.IOTIZE_TRAME_MAX_SIZE) {
        //         throw new Error("Request max size"); // TODO
        //     }
        //     lgDataIn = data.length;
        // }
        // var dataTx = new Uint8Array(7 + lgDataIn);
        // dataTx[lg++] = method;
        // dataTx[lg++] = (IdObj / 256);
        // dataTx[lg++] = (IdObj % 256);
        // dataTx[lg++] = (IdInstObj / 256);
        // dataTx[lg++] = (IdInstObj % 256);
        // dataTx[lg++] = (IdRes / 256);
        // dataTx[lg++] = (IdRes % 256);
        // if (data != null) {
        //     for (var i = 0; i < lgDataIn; i++ , lg++) {
        //         dataTx[lg] = data[i];
        //     }
        // }
        // return dataTx;
    };
    // TODO max size is different according to the protocol
    // @see documentation
    DefaultCommandEncoder.IOTIZE_TRAME_MAX_SIZE = 256 - 6; // Lc = u8 legnth - 5 header bytes  - 1 byte "Le"
    DefaultCommandEncoder.TAG = "DefaultCommandEncoder";
    DefaultCommandEncoder.IOTIZE_CLA = 0xA2;
    DefaultCommandEncoder.IOTIZE_INSTRUCTION_CODE_GET = 0xCA;
    DefaultCommandEncoder.IOTIZE_INSTRUCTION_CODE_PUT = 0xDA;
    return DefaultCommandEncoder;
}());
exports.DefaultCommandEncoder = DefaultCommandEncoder;
