import url from 'url';
var addedElements = new Map();
function createElement(tag, attrs) {
  var el = document.createElement(tag);
  Object.keys(attrs).forEach(function (key) {
    if (key === 'className') {
      el[key] = attrs[key];
    } else {
      el.setAttribute(key, attrs[key]);
    }
  });
  return el;
}
export default function injectStyleSheet(urlStr) {
  var parsedURL = new URL(urlStr);
  var protocol = parsedURL.protocol;
  var protocolPattern = /^(http:|https:)/;
  if (!protocolPattern.test(protocol || '')) {
    if (protocolPattern.test(window.location.protocol)) {
      parsedURL.protocol = window.location.protocol;
    } else {
      parsedURL.protocol = 'http:';
    }
  }
  var href = url.format(parsedURL);
  if (addedElements.has(href)) {
    return;
  }
  var el = createElement('link', {
    crossorigin: 'anonymous',
    href: href,
    rel: 'stylesheet'
  });
  addedElements.set(href, el);
  var root = document.head || document.documentElement || document.body;
  root === null || root === void 0 || root.appendChild(el);
}