function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import { PLUGIN_MAPPER_SOURCE } from './data';
import { transformPlugin } from './transformer';
import request from './request';
var Category;

(function (Category) {
  Category[Category["Limit traffic"] = 0] = "Limit traffic";
  Category[Category["Observability"] = 1] = "Observability";
  Category[Category["Security"] = 2] = "Security";
  Category[Category["Authentication"] = 3] = "Authentication";
  Category[Category["Log"] = 4] = "Log";
  Category[Category["Other"] = 5] = "Other";
})(Category || (Category = {}));

export var fetchPluginList = function fetchPluginList() {
  return request('/plugins');
};
var cachedPluginNameList = [];
export var getList = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var plugins,
        names,
        data,
        enabledPluginNames,
        _args = arguments;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            plugins = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};

            if (cachedPluginNameList.length) {
              _context.next = 5;
              break;
            }

            _context.next = 4;
            return fetchPluginList();

          case 4:
            cachedPluginNameList = _context.sent.data;

          case 5:
            names = cachedPluginNameList;
            data = {};
            enabledPluginNames = Object.keys(plugins);
            names.forEach(function (name) {
              var plugin = PLUGIN_MAPPER_SOURCE[name] || {};
              var _plugin$category = plugin.category,
                  category = _plugin$category === void 0 ? 'Other' : _plugin$category,
                  _plugin$hidden = plugin.hidden,
                  hidden = _plugin$hidden === void 0 ? false : _plugin$hidden;

              if (!data[category]) {
                data[category] = [];
              }

              if (!hidden) {
                data[category] = data[category].concat(_objectSpread(_objectSpread({}, plugin), {}, {
                  name: name,
                  enabled: enabledPluginNames.includes(name)
                }));
              }
            });
            return _context.abrupt("return", Object.keys(data).sort(function (a, b) {
              return Category[a] - Category[b];
            }).map(function (category) {
              return data[category].sort(function (a, b) {
                return (a.priority || 9999) - (b.priority || 9999);
              });
            }));

          case 10:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function getList() {
    return _ref.apply(this, arguments);
  };
}();
var cachedPluginSchema = {};
export var fetchPluginSchema = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(name) {
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            if (cachedPluginSchema[name]) {
              _context2.next = 4;
              break;
            }

            _context2.next = 3;
            return request("/schema/plugins/".concat(name));

          case 3:
            cachedPluginSchema[name] = _context2.sent.data;

          case 4:
            return _context2.abrupt("return", transformPlugin(name, cachedPluginSchema[name], 'schema'));

          case 5:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));

  return function fetchPluginSchema(_x) {
    return _ref2.apply(this, arguments);
  };
}();