import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';

/**
 * 移除CSS
 *
 * @param {string} href - CSS链接
 *
 * @example
 *
 * removeCss('https://xxx.css')
 */
function removeCss(href) {
  var _a;
  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(href) !== -1) {
      links[i].parentNode.removeChild(links[i]);
    }
  }
}
function loadStyle(url, urlClass) {
  var link = document.createElement('link');
  link.type = 'text/css';
  link.rel = 'stylesheet';
  link.href = url;
  link.className = urlClass; // 'load-style';
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(link);
}
/**
 * 加载多个样式文件，并在加载前移除具有相同类名的文件
 * @param {array} urls 外链地址列表
 * @param {string} urlClass 外链类名
 *
 * @example
 *
 * ```ts
 * loadStyles(['https://a.com/b.css'], 'load-style');
 * ```
 */
function loadStyles(urls, urlClass) {
  // 先删除其他的
  Array.prototype.slice.call(document.getElementsByClassName(urlClass)).forEach(function (item) {
    return item.remove();
  });
  // loadJs('https://image-1251917893.file.myqcloud.com/igame/common/js/adapter.js');
  _toConsumableArray(urls).forEach(function (url) {
    loadStyle(url, urlClass);
  });
}
/**
 * 加载样式代码块，会将样式代码包裹在 style 标签内，并加载到当前页面中
 * @param {string} code 样式代码
 * @param {string} className 类名
 *
 * @example
 *
 * ```ts
 * loadCssCode(
 *   '.press__cover { color: red; }',
 *   'load-css-code'
 * );
 * ```
 */
function loadCssCode(code, className) {
  Array.prototype.slice.call(document.getElementsByClassName(className)).forEach(function (item) {
    return item.remove();
  });
  var style = document.createElement('style');
  style.className = className; // 'load-css-code';
  style.type = 'text/css';
  // @ts-ignore
  style.rel = 'stylesheet';
  try {
    // for Chrome Firefox Opera Safari
    style.appendChild(document.createTextNode(code));
  } catch (ex) {
    // for IE
    // @ts-ignore
    style.styleSheet.cssText = code;
  }
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(style);
}

export { loadCssCode, loadStyles, removeCss };
