import { loader } from './little-loader.mjs';

/**
 * 以 Promise 的方式加载 js 文件
 * @param {string}  url  js文件路径
 * @returns {Promise<number>} promise
 * @example
 * ```ts
 * await loadJS('https://example.com/lib.js');
 * console.log('loaded');
 * ```
 */
function loadJS(url) {
  return new Promise(function (resolve) {
    loader(url, function () {
      resolve(1);
    });
  });
}
/**
 * 动态加载CSS
 * @param {string} url CSS链接
 * @example
 *
 * loadCSS('xxx.css')
 */
function loadCSS(url) {
  var _a;
  var addSign = true;
  var links = document.getElementsByTagName('link');
  // eslint-disable-next-line @typescript-eslint/prefer-for-of
  for (var i = 0; i < links.length; i++) {
    if (((_a = links[i]) === null || _a === void 0 ? void 0 : _a.href) && links[i].href.indexOf(url) !== -1) {
      addSign = false;
    }
  }
  if (addSign) {
    var link = document.createElement('link');
    link.type = 'text/css';
    link.rel = 'stylesheet';
    link.href = url;
    document.getElementsByTagName('head')[0].appendChild(link);
  }
}

export { loadCSS, loadJS };
