import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useEffect } from "react";
import { useAppConfig } from "../AppConfig";
import { atom, useRecoilState, useRecoilValue } from "recoil";
import { useToasts } from "../Toasts";
var pluginsState = atom({
  key: "plugins",
  default: []
});
export var usePlugins = function usePlugins() {
  return useRecoilValue(pluginsState);
};
export default (function () {
  // eslint-disable-next-line
  var _useRecoilState = useRecoilState(pluginsState),
      _useRecoilState2 = _slicedToArray(_useRecoilState, 2),
      plugins = _useRecoilState2[0],
      setPlugins = _useRecoilState2[1];

  var _useAppConfig = useAppConfig(),
      fromConfig = _useAppConfig.fromConfig;

  var _useToasts = useToasts(),
      addToast = _useToasts.addToast;

  useEffect(function () {
    function loadPlugins() {
      return _loadPlugins.apply(this, arguments);
    }

    function _loadPlugins() {
      _loadPlugins = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var pluginUrls, plugins, _iterator, _step, pluginUrl, _yield$import$default, transformPlugins, importPlugins, interfacePlugins, authenticationPlugins;

        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                pluginUrls = (fromConfig("pluginUrls") || "").split("\n").filter(Boolean);
                plugins = [];
                _iterator = _createForOfIteratorHelper(pluginUrls);
                _context.prev = 3;

                _iterator.s();

              case 5:
                if ((_step = _iterator.n()).done) {
                  _context.next = 26;
                  break;
                }

                pluginUrl = _step.value;
                _context.prev = 7;
                _context.next = 10;
                return import(
                /* webpackIgnore: true */
                pluginUrl);

              case 10:
                _yield$import$default = _context.sent.default();
                transformPlugins = _yield$import$default.transformPlugins;
                importPlugins = _yield$import$default.importPlugins;
                interfacePlugins = _yield$import$default.interfacePlugins;
                authenticationPlugins = _yield$import$default.authenticationPlugins;
                plugins.push.apply(plugins, _toConsumableArray(transformPlugins.map(function (p) {
                  return _objectSpread(_objectSpread({}, p), {}, {
                    type: "transform"
                  });
                })));
                plugins.push.apply(plugins, _toConsumableArray(importPlugins.map(function (p) {
                  return _objectSpread(_objectSpread({}, p), {}, {
                    type: "import"
                  });
                })));
                plugins.push.apply(plugins, _toConsumableArray(interfacePlugins.map(function (p) {
                  return _objectSpread(_objectSpread({}, p), {}, {
                    type: "interface"
                  });
                })));
                plugins.push.apply(plugins, _toConsumableArray(authenticationPlugins.map(function (p) {
                  return _objectSpread(_objectSpread({}, p), {}, {
                    type: "authentication"
                  });
                })));
                _context.next = 24;
                break;

              case 21:
                _context.prev = 21;
                _context.t0 = _context["catch"](7);
                // TODO display broken plugin more nicely, using regex extraction of
                // package and version
                addToast("Couldn't load plugin: " + pluginUrl + "\n\n" + _context.t0.toString(), "error");

              case 24:
                _context.next = 5;
                break;

              case 26:
                _context.next = 31;
                break;

              case 28:
                _context.prev = 28;
                _context.t1 = _context["catch"](3);

                _iterator.e(_context.t1);

              case 31:
                _context.prev = 31;

                _iterator.f();

                return _context.finish(31);

              case 34:
                setPlugins(plugins);

              case 35:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, null, [[3, 28, 31, 34], [7, 21]]);
      }));
      return _loadPlugins.apply(this, arguments);
    }

    loadPlugins(); // eslint-disable-next-line
  }, [fromConfig("pluginUrls")]);
});