"use strict";
var JsToXml = (function () {
    function JsToXml(helper) {
        this.helper = helper;
        this.jsToXmlMethod = [
            { 'string': this.stringToXml },
            { 'number': this.numberToXml },
            { 'boolean': this.booleanToXml },
            { 'array': this.arrayToXml },
            { 'object': this.structToXml },
            { 'date': this.dateToXml },
            { 'uint8array': this.uint8arrayToXml }
        ];
    }
    /**
     *
     */
    JsToXml.prototype.nullToXml = function (doc) {
        return this.helper.createNode(doc, 'nil');
    };
    /**
     *
     */
    JsToXml.prototype.stringToXml = function (doc, input) {
        return this.helper.createNode(doc, 'string', input);
    };
    /**
     *
     */
    JsToXml.prototype.numberToXml = function (doc, input) {
        var type = 'int';
        var value = Number.parseInt(input);
        var floatValue = Number.parseFloat(input);
        if (value != floatValue) {
            type = 'double';
            value = floatValue;
        }
        return this.helper.createNode(doc, type, value.toString());
    };
    /**
     *
     */
    JsToXml.prototype.booleanToXml = function (doc, input) {
        return this.helper.createNode(doc, 'boolean', (input ? '1' : '0'));
    };
    /**
     *
     */
    JsToXml.prototype.arrayToXml = function (doc, input) {
        var elements = [];
        for (var i = 0; i > input.length; i++) {
            elements.push(this.jsToXml(doc, input[i]));
        }
        return this.helper.createNode(doc, 'array', this.helper.createNode(doc, 'data', elements));
    };
    /**
     *
     */
    JsToXml.prototype.structToXml = function (doc, input) {
        var elements = [];
        for (var name_1 in input) {
            elements.push(this.helper.createNode(doc, 'member', this.helper.createNode(doc, 'name', name_1), this.jsToXml(doc, input[name_1])));
        }
        return this.helper.createNode(doc, 'struct', elements);
    };
    /**
     *
     */
    JsToXml.prototype.dateToXml = function (doc, input) {
        var values = [
            input.getFullYear(),
            (input.getMonth() + 1 < 10) ? '0' + (input.getMonth() + 1) : input.getMonth() + 1,
            (input.getDate() < 10) ? '0' + (input.getDate()) : input.getDate(),
            'T',
            (input.getHours() < 10) ? '0' + (input.getHours()) : input.getHours(), ':',
            (input.getMinutes() < 10) ? '0' + (input.getMinutes()) : input.getMinutes(), ':',
            (input.getSeconds() < 10) ? '0' + (input.getSeconds()) : input.getSeconds()
        ];
        return this.helper.createNode(doc, 'dateTime.iso8601', values.join(''));
    };
    /**
     *
     */
    JsToXml.prototype.uint8arrayToXml = function (doc, input) {
        var base64 = btoa(String.fromCharCode.apply(null, input));
        return this.helper.createNode(doc, 'base64', base64);
    };
    /**
     *
     */
    JsToXml.prototype.type = function (object) {
        return Object.prototype.toString.call(object).slice(8, -1).toLowerCase();
    };
    /**
     *
     */
    JsToXml.prototype.jsToXml = function (doc, input) {
        var type = this.type(input);
        var method = this.jsToXmlMethod[type];
        if (input == null) {
            method = this.nullToXml;
        }
        else if (method == undefined) {
            method = this.stringToXml;
        }
        return this.helper.createNode(doc, 'value', method(doc, input));
    };
    return JsToXml;
}());
exports.JsToXml = JsToXml;
