"use strict";
var XmlRpcHelper = (function () {
    function XmlRpcHelper() {
        try {
            new ActiveXObject('MSXML2.DOMDocument');
            this._hasActiveX = true;
        }
        catch (e) {
            this._hasActiveX = false;
        }
    }
    Object.defineProperty(XmlRpcHelper.prototype, "hasActiveX", {
        /**
         *
         */
        get: function () {
            return this._hasActiveX;
        },
        enumerable: true,
        configurable: true
    });
    /**
     *
     */
    XmlRpcHelper.prototype.cloneArray = function (object) {
        var length = object.length;
        if (length > 0) {
            var result = new Array(length);
            for (var i = 0; i < length; i++) {
                result[i] = object[i];
            }
            return result;
        }
        return [];
    };
    /**
     *
     */
    XmlRpcHelper.prototype.createMsXmlDocument = function () {
        var doc = new ActiveXObject('MSXML2.DOMDocument');
        if (doc) {
            doc.resolveExternals = false;
            doc.validateOnParse = false;
            try {
                doc.setProperty('ProhibitDTD', true);
                doc.setProperty('MaxXMLSize', 2 * 1024);
                doc.setProperty('MaxElementDepth', 256);
            }
            catch (e) {
                console.log(e);
            }
        }
        return doc;
    };
    /**
     *
     */
    XmlRpcHelper.prototype.createDocument = function (rootTag, uriNamespace) {
        if (!rootTag && uriNamespace) {
            throw Error("Can't create document with namespace and no root tag");
        }
        if (this.hasActiveX) {
            var doc = this.createMsXmlDocument();
            if (doc) {
                if (rootTag) {
                    doc.appendChild(doc.createNode(1, rootTag, uriNamespace || ''));
                }
                return doc;
            }
        }
        else if (document.implementation && document.implementation.createDocument) {
            return document.implementation.createDocument(uriNamespace || '', rootTag || '', null);
        }
        throw Error('Your browser does not support creating new documents');
    };
    /**
     *
     */
    XmlRpcHelper.prototype.type = function (object) {
        return Object.prototype.toString.call(object).slice(8, -1).toLowerCase();
    };
    /**
     *
     */
    XmlRpcHelper.prototype.createNode = function (doc, nodeName) {
        var children = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            children[_i - 2] = arguments[_i];
        }
        var result = doc.createElement(nodeName);
    };
    /**
     *
     */
    XmlRpcHelper.prototype.appendChild = function (child) {
        if (this.type(child) == 'object' && child.nodeType != 1) {
            for (var item in child) {
            }
        }
    };
    /**
     *
     */
    XmlRpcHelper.prototype.generateId = function () {
        return 'xmlrpc-' + (new Date().getTime()) + ' - ' + Math.floor(Math.random() * 1000);
    };
    /**
     *
     */
    XmlRpcHelper.prototype.loadXml = function (xml) {
        if (this.hasActiveX) {
            var doc = this.createMsXmlDocument();
            doc.loadXML(xml);
            return doc;
        }
        else if (typeof DOMParser != 'undefined') {
            return new DOMParser().parseFromString(xml, 'application/xml');
        }
        throw Error('Your browser does not support loading xml documents');
    };
    /**
     *
     */
    XmlRpcHelper.prototype.getOwnerDocument = function (node) {
        return (node.nodeType == 9 ? node : node.ownerDocument || node.document);
    };
    /**
     *
     */
    XmlRpcHelper.prototype.selectSingleNode = function (node, path) {
        var doc = this.getOwnerDocument(node);
        if (typeof node.selectSingleNode != 'undefined') {
            if (typeof doc.setProperty != 'undefined') {
                doc.setProperty('SelectionLanguage', 'XPath');
            }
            return node.selectSingleNode(path);
        }
        else if (document.implementation.hasFeature('XPath', '3.0')) {
            var resolver = doc.createNSResolver(doc.documentElement);
            var result = doc.evaluate(path, node, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
            return result.singleNodeValue;
        }
        return null;
    };
    /**
     *
     */
    XmlRpcHelper.prototype.getTextContent = function (node, buffer, normalizeWhitespace) {
        var PREDEFINED_TAG_VALUES = { 'IMG': '', 'BR': '\n' };
        if (node.nodeName in ['SCRIPT', 'STYLE', 'HEAD', 'IFRAME', 'OBJECT']) {
        }
        else if (node.nodeType == 3) {
            if (normalizeWhitespace) {
                buffer.push(String(node.nodeValue).replace(/(\r\n|\r|\n)/g, ''));
            }
            else {
                buffer.push(node.nodeValue);
            }
        }
        else if (node.nodeName in PREDEFINED_TAG_VALUES) {
            buffer.push(PREDEFINED_TAG_VALUES[node.nodeName]);
        }
        else {
            var child = node.firstChild;
            while (child) {
                this.getTextContent(child, buffer, normalizeWhitespace);
                child = child.nextSibling;
            }
        }
        return buffer.join('');
    };
    /**
     *
     */
    XmlRpcHelper.prototype.selectNodes = function (node, path) {
        var doc = this.getOwnerDocument(node);
        if (typeof node.selectNodes != 'undefined') {
            if (typeof doc.setProperty != 'undefined') {
                doc.setProperty('SelectionLanguage', 'XPath');
            }
            return node.selectNodes(path);
        }
        else if (document.implementation.hasFeature('XPath', '3.0')) {
            var resolver = doc.createNSResolver(doc.documentElement);
            var nodes = doc.evaluate(path, node, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            var results = [];
            var count = nodes.snapshotLength;
            for (var i = 0; i < count; i++) {
                results.push(nodes.snapshotItem(i));
            }
            return results;
        }
        else {
            return [];
        }
    };
    return XmlRpcHelper;
}());
exports.XmlRpcHelper = XmlRpcHelper;
