///<reference path="../../../net/Uri.ts" />
var econda;
(function (econda) {
    var tagmanager;
    (function (tagmanager) {
        var tag;
        (function (tag) {
            var injector;
            (function (injector) {
                var Uri = econda.net.Uri;
                /**
                 * Class to insert script references (script tag with src attribute) into document.
                 * @class econda.tagmanager.injector.ExternalScriptInjector
                 */
                var ExternalScriptInjector = (function () {
                    function ExternalScriptInjector(cfg) {
                        if (typeof cfg != 'undefined') {
                            if (typeof cfg.outputNameEnabled != 'undefined') {
                                this.setOutputName(cfg.outputNameEnabled);
                            }
                        }
                    }
                    //noinspection JSUnusedGlobalSymbols
                    ExternalScriptInjector.prototype.getOutputName = function () {
                        return this._outputNameEnabled;
                    };
                    ExternalScriptInjector.prototype.setOutputName = function (enabled) {
                        this._outputNameEnabled = enabled;
                        return this;
                    };
                    //noinspection JSMethodCanBeStatic
                    /**
                     * Returns a script node for a defined javascript file
                     *
                     * @returns {Object} script
                     * @private
                     * @param url
                     * @param async
                     */
                    ExternalScriptInjector.prototype.generateExternalScript = function (url, async) {
                        if (async === void 0) { async = true; }
                        var codePath = new RegExp('^(?!https?\:\/\/)([-a-z0-9]+\.)', 'i').test(url.toString()) == true
                            ? "//" + url.toString()
                            : url.toString();
                        var element = document.createElement("script");
                        element.type = "text/javascript";
                        element.src = codePath + "?" + new Date().getTime();
                        element.async = async;
                        return element;
                    };
                    ExternalScriptInjector.prototype.inject = function (scriptToInject) {
                        var url = new Uri(scriptToInject.url);
                        var async = (typeof scriptToInject.async != 'undefined') ? scriptToInject.async : true;
                        var head = (typeof scriptToInject.head != 'undefined') ? scriptToInject.head : false;
                        var scriptElement = this.generateExternalScript(url, async);
                        if (head) {
                            document.getElementsByTagName('head')[0].appendChild(scriptElement);
                        }
                        else {
                            var divc = document.createElement("div");
                            if (this._outputNameEnabled) {
                                divc.innerHTML = "<!-- " + scriptToInject.name + "-->";
                            }
                            divc.appendChild(scriptElement);
                            document.getElementById(scriptToInject.location).appendChild(divc);
                        }
                    };
                    return ExternalScriptInjector;
                }());
                injector.ExternalScriptInjector = ExternalScriptInjector;
            })(injector = tag.injector || (tag.injector = {}));
        })(tag = tagmanager.tag || (tagmanager.tag = {}));
    })(tagmanager = econda.tagmanager || (econda.tagmanager = {}));
})(econda || (econda = {}));