"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.documentReady = documentReady;
exports.createLinkElement = createLinkElement;
exports.createScriptElement = createScriptElement;
exports.existsElementById = existsElementById;
exports.validUniqueElementId = validUniqueElementId;
exports.genUniqueElementId = genUniqueElementId;
exports.appendCss = appendCss;
exports.appendJs = appendJs;
exports.appendCss2 = appendCss2;
exports.appendJs2 = appendJs2;
exports.removeElemById = removeElemById;
exports.getElementsByTagName = getElementsByTagName;
var shortid_1 = __importDefault(require("shortid"));
var lodash_1 = require("./lodash");
shortid_1.default.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_');
function documentReady(callback) {
    if (document.readyState !== 'loading') {
        return callback();
    }
    document.addEventListener('DOMContentLoaded', callback);
}
function createLinkElement(data) {
    var id = data.id, url = data.url;
    var linkEl = document.createElement('link');
    [
        { name: 'id', value: id },
        { name: 'rel', value: 'stylesheet' },
        { name: 'type', value: 'text/css' },
        { name: 'href', value: url },
    ].forEach(function (attrs) {
        var name = attrs.name, value = attrs.value;
        linkEl.setAttribute(name, value);
    });
    return linkEl;
}
function createScriptElement(data) {
    var id = data.id, url = data.url;
    var scriptEl = document.createElement('script');
    [
        { name: 'id', value: id },
        { name: 'src', value: url },
    ].forEach(function (attrs) {
        var name = attrs.name, value = attrs.value;
        scriptEl.setAttribute(name, value);
    });
    return scriptEl;
}
function existsElementById(id) {
    return !(0, lodash_1.isNil)(document.getElementById(id));
}
function validUniqueElementId(value) {
    if ((0, lodash_1.isNil)(value))
        return false;
    return !existsElementById(value);
}
function genUniqueElementId(prefix) {
    var sid;
    do {
        sid = (prefix + shortid_1.default.generate()).replace(/\$/g, '_');
    } while (!validUniqueElementId(sid));
    return sid;
}
function checkDupElements(data) {
    var id = data.id, url = data.url;
    if (existsElementById(id)) {
        console.warn('[DomValidate]', "exists ".concat(url));
        return true;
    }
    return false;
}
function appendCss(data) {
    if (checkDupElements(data))
        return false;
    var el = createLinkElement(data);
    var target = (0, lodash_1.get)(data, 'target', 'head');
    var container = target === 'body' ? document.body : document.head;
    if (!container)
        return false;
    container.appendChild(el);
    return true;
}
function appendJs(data) {
    if (checkDupElements(data))
        return false;
    var el = createScriptElement(data);
    var target = (0, lodash_1.get)(data, 'target', 'body');
    var container = target === 'body' ? document.body : document.head;
    if (!container)
        return false;
    container.appendChild(el);
    return true;
}
function appendCss2(data, resolve, uItem) {
    if (checkDupElements(data))
        return false;
    var el = createLinkElement(data);
    el.onload = function () {
        resolve(uItem);
    };
    var target = (0, lodash_1.get)(data, 'target', 'head');
    var container = target === 'body' ? document.body : document.head;
    if (!container)
        return false;
    container.appendChild(el);
    return true;
}
function appendJs2(data, resolve, uItem) {
    if (checkDupElements(data))
        return false;
    var el = createScriptElement(data);
    el.onload = function () {
        resolve(uItem);
    };
    var target = (0, lodash_1.get)(data, 'target', 'body');
    var container = target === 'body' ? document.body : document.head;
    if (!container)
        return false;
    container.appendChild(el);
    return true;
}
function removeElemById(id) {
    var elem = document.getElementById(id);
    if ((0, lodash_1.isNil)(elem))
        return;
    elem.parentElement.removeChild(elem);
    return elem;
}
function getElementsByTagName(tagName) {
    var elems = document.getElementsByTagName(tagName);
    return Array.prototype.slice.call(elems, 0);
}
//# sourceMappingURL=dom-utils.js.map