'use strict';

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _index;

function _load_index() {
  return _index = require('./api/consumer/index');
}

var _index2;

function _load_index2() {
  return _index2 = require('./api/scope/index');
}

var _extension;

function _load_extension() {
  return _extension = _interopRequireDefault(require('./extensions/extension'));
}

var _hooks;

function _load_hooks() {
  return _hooks = _interopRequireDefault(require('./hooks'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

(_hooks || _load_hooks()).default.init();

module.exports = {
  show: function show(scopePath, id, opts) {
    return (0, (_index || _load_index()).getScopeComponent)({ scopePath, id, allVersions: opts && opts.versions }).then(function (c) {
      if (Array.isArray(c)) {
        return c.map(function (v) {
          return v.toObject();
        });
      }
      return c.toObject();
    });
  },
  list: function list(scopePath) {
    return (0, (_index2 || _load_index2()).scopeList)(scopePath).then(function (components) {
      return components.map(function (c) {
        return c.id.toString();
      });
    });
  },
  /**
   * Load extension programmatically
   */
  loadExtension: function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(extensionName, scopePath, extensionConfig, extensionOptions) {
      var extension;
      return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return (_extension || _load_extension()).default.load({
                name: extensionName,
                rawConfig: extensionConfig,
                scopePath,
                options: extensionOptions
              });

            case 2:
              extension = _context.sent;
              return _context.abrupt('return', Promise.resolve(extension));

            case 4:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, undefined);
    }));

    function loadExtension(_x, _x2, _x3, _x4) {
      return _ref.apply(this, arguments);
    }

    return loadExtension;
  }()
};