"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

var appendJs = function appendJs(assets) {
  return new Promise(function (resolve, reject) {
    var script = document.createElement("script");
    var r = false;
    script.type = "text/javascript";
    script.src = assets.uri;
    script.async = true;
    script.id = "superset-chart-assets";
    script.setAttribute("crossorigin", "anonymous");

    script.onerror = function (err) {
      reject(err);
    };

    script.onload = function onload() {
      // @ts-ignore
      if (!r && (!this.readyState || this.readyState === "complete")) {
        r = true;
        resolve(r);
      }
    };

    document.head.appendChild(script);
  });
};

var appendCss = function appendCss(assets) {
  var el = document.createElement("link");
  el.href = assets.uri;
  return el;
};

var AppendAssetsList = /*#__PURE__*/function () {
  function AppendAssetsList() {
    var _this = this;

    _classCallCheck(this, AppendAssetsList);

    this.appendAssets = function (assets) {
      var type = assets.type;
      return _this.strategy[type](assets);
    };

    this.appendAssetsList = function () {
      if (!_this.assetsList.length) return undefined;

      if (!_this.init) {
        _this.init = true;
        var fragment = document.createDocumentFragment();

        _this.assetsList.filter(function (assets) {
          return assets.type !== "js";
        }).forEach(function (assets) {
          if (!assets.type || !_this.strategy[assets.type]) {
            // eslint-disable-next-line no-console
            console.error("assets type is not expected");
            return;
          }

          var el = _this.appendAssets(assets);

          fragment.appendChild(el);
        });

        return Promise.all(_this.assetsList.filter(function (assets) {
          return assets.type === "js";
        }).map(function (assets) {
          return appendJs(assets);
        }));
      }

      return Promise.resolve();
    };

    this.init = false;
    this.strategy = {
      js: appendJs,
      css: appendCss
    };
  }

  _createClass(AppendAssetsList, [{
    key: "setAssets",
    value: function setAssets(assets) {
      this.assetsList = assets;
    }
  }]);

  return AppendAssetsList;
}();

var _default = AppendAssetsList;
exports["default"] = _default;