"use strict";

exports.__esModule = true;
exports.createAlignmentView = createAlignmentView;
exports.createVersionHistoryView = createVersionHistoryView;
exports["default"] = createVectorEditor;
var _react = _interopRequireDefault(require("react"));
var _reactRedux = require("react-redux");
var _makeStore = _interopRequireDefault(require("./makeStore"));
var _reactDom = require("react-dom");
var _Editor = _interopRequireDefault(require("../Editor"));
var _updateEditor = _interopRequireDefault(require("../updateEditor"));
var _addAlignment = _interopRequireDefault(require("../addAlignment"));
var _AlignmentView = _interopRequireDefault(require("../AlignmentView"));
var _reactSizeme = _interopRequireDefault(require("react-sizeme"));
var _VersionHistoryView = _interopRequireDefault(require("../VersionHistoryView"));
var _excluded = ["editorName"],
  _excluded2 = ["editorName"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var store;
function StandaloneEditor(props) {
  if (!store) {
    store = (0, _makeStore["default"])();
  }
  return /*#__PURE__*/_react["default"].createElement(_reactRedux.Provider, {
    store: store
  }, /*#__PURE__*/_react["default"].createElement(_Editor["default"], props));
}
function StandaloneAlignment(props) {
  if (!store) {
    store = (0, _makeStore["default"])();
  }
  return /*#__PURE__*/_react["default"].createElement(_reactRedux.Provider, {
    store: store
  }, /*#__PURE__*/_react["default"].createElement(_AlignmentView["default"], _extends({}, props, {
    dimensions: {
      width: props.size.width
    }
  })));
}
function StandaloneVersionHistoryView(props) {
  if (!store) {
    store = (0, _makeStore["default"])();
  }
  return /*#__PURE__*/_react["default"].createElement(_reactRedux.Provider, {
    store: store
  }, /*#__PURE__*/_react["default"].createElement(_VersionHistoryView["default"], _extends({}, props)));
}
function createVectorEditor(_node, _temp) {
  var _ref = _temp === void 0 ? {} : _temp,
    _ref$editorName = _ref.editorName,
    editorName = _ref$editorName === void 0 ? "StandaloneEditor" : _ref$editorName,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  if (!store) {
    store = (0, _makeStore["default"])();
  }
  var node;
  if (_node === "createDomNodeForMe") {
    node = document.createElement("div");
    node.className = "ove-created-div";
    document.body.appendChild(node);
  } else {
    node = _node;
  }
  var editor = {};
  editor.renderResponse = (0, _reactDom.render)( /*#__PURE__*/_react["default"].createElement(StandaloneEditor, _extends({
    editorName: editorName
  }, rest)), node);
  editor.close = function () {
    (0, _reactDom.unmountComponentAtNode)(node);
    node.remove();
  };
  editor.updateEditor = function (values) {
    (0, _updateEditor["default"])(store, editorName, values);
  };
  editor.addAlignment = function (values) {
    (0, _addAlignment["default"])(store, values);
  };
  editor.getState = function () {
    return store.getState().VectorEditor[editorName];
  };
  return editor;
}
function createVersionHistoryView(node, _temp2) {
  var _ref2 = _temp2 === void 0 ? {} : _temp2,
    _ref2$editorName = _ref2.editorName,
    editorName = _ref2$editorName === void 0 ? "StandaloneVersionHistoryView" : _ref2$editorName,
    rest = _objectWithoutPropertiesLoose(_ref2, _excluded2);
  if (!store) {
    store = (0, _makeStore["default"])();
  }
  var editor = {};
  editor.renderResponse = (0, _reactDom.render)( /*#__PURE__*/_react["default"].createElement(StandaloneVersionHistoryView, _extends({
    editorName: editorName
  }, rest)), node);
  editor.updateEditor = function (values) {
    (0, _updateEditor["default"])(store, editorName, values);
  };
  editor.getState = function () {
    return store.getState().VectorEditor["StandaloneVersionHistoryView"];
  };
  return editor;
}
var SizedStandaloneAlignment = (0, _reactSizeme["default"])()(StandaloneAlignment);
function createAlignmentView(node, props) {
  if (props === void 0) {
    props = {};
  }
  if (!store) {
    store = (0, _makeStore["default"])();
  }
  var editor = {};
  editor.renderResponse = (0, _reactDom.render)( /*#__PURE__*/_react["default"].createElement(SizedStandaloneAlignment, props), node);
  editor.updateAlignment = function (values) {
    (0, _addAlignment["default"])(store, values);
  };
  editor.updateAlignment(props);
  editor.getState = function () {
    if (!props.id) {
      throw new Error('Please pass an id when using createAlignmentView. eg createAlignmentView(myDiv, {id: "someUniqueId"})');
    }
    return store.getState().VectorEditor.__allEditorsOptions.alignments[props.id];
  };
  return editor;
}
window.createVectorEditor = createVectorEditor;
window.createAlignmentView = createAlignmentView;
window.createVersionHistoryView = createVersionHistoryView;