'use strict';

require('whatwg-fetch');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _pluginCore = require('@annotation-studio/plugin-core');

var _pluginCore2 = _interopRequireDefault(_pluginCore);

var _pluginViewer = require('@annotation-studio/plugin-viewer');

var _pluginViewer2 = _interopRequireDefault(_pluginViewer);

var _pluginResourceEditor = require('@annotation-studio/plugin-resource-editor');

var _pluginResourceEditor2 = _interopRequireDefault(_pluginResourceEditor);

var _pluginTagging = require('@annotation-studio/plugin-tagging');

var _pluginTranscriber = require('@annotation-studio/plugin-transcriber');

var _pluginTranscriber2 = _interopRequireDefault(_pluginTranscriber);

var _pluginDrafts = require('@annotation-studio/plugin-drafts');

var _pluginDrafts2 = _interopRequireDefault(_pluginDrafts);

var _pluginJsonPreview = require('@annotation-studio/plugin-json-preview');

var _pluginJsonPreview2 = _interopRequireDefault(_pluginJsonPreview);

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

var behaviours = function behaviours(resolveSharedStore) {
  return {
    'annotation-studio-core': function annotationStudioCore(image, attributes) {
      return (0, _pluginCore2.default)(image, attributes).then(function (store) {
        return resolveSharedStore(store);
      });
    },
    'annotation-studio-viewer': _pluginViewer2.default,
    'annotation-studio-resource-editor': _pluginResourceEditor2.default,
    'annotation-studio-tagging-annotations': _pluginTagging.TaggingAnnotationsPlugin,
    'annotation-studio-tagging': _pluginTagging.TaggingPlugin,
    'annotation-studio-drafts': _pluginDrafts2.default,
    'annotation-studio-transcriber': _pluginTranscriber2.default,
    'annotation-studio-json-preview': _pluginJsonPreview2.default
  };
};

var _utils$deferred = _pluginCore.utils.deferred(),
    resolve = _utils$deferred.resolve,
    whenToRender = _utils$deferred.promise;

function loadPlugin($el, name, config) {
  try {
    var register = behaviours(resolve);
    var behaviour = register[name];
    if (behaviour.length === 3) {
      whenToRender.then(function (store) {
        return behaviour($el, config, store);
      });
    } else {
      behaviour($el, config);
    }
  } catch (e) {
    console.error('Behaviour does not exist: ' + name, e);
  }
}

document.addEventListener('DOMContentLoaded', function () {
  // Small error-prone example of data behaviour factory.
  for (var _iterator = document.querySelectorAll('[data-behaviour]'), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
    var _ref;

    if (_isArray) {
      if (_i >= _iterator.length) break;
      _ref = _iterator[_i++];
    } else {
      _i = _iterator.next();
      if (_i.done) break;
      _ref = _i.value;
    }

    var $el = _ref;

    loadPlugin($el, $el.getAttribute('data-behaviour'), _pluginCore.utils.getAttributes($el));
  }
});

// Set up a global.
(function (g) {
  g.loadAnnotationStudioPlugin = g.loadAnnotationStudioPlugin ? g.loadAnnotationStudioPlugin : loadPlugin;
})(window || global);