var _excluded = ["editorName"],
  _excluded2 = ["editorName"];
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); }
import React from "react";
import { Provider } from "react-redux";
import makeStore from "./makeStore";
import { render, unmountComponentAtNode } from "react-dom";
import Editor from "../Editor";
import updateEditor from "../updateEditor";
import addAlignment from "../addAlignment";
import AlignmentView from "../AlignmentView";
import sizeMe from "react-sizeme";
import VersionHistoryView from "../VersionHistoryView";
var store;
function StandaloneEditor(props) {
  if (!store) {
    store = makeStore();
  }
  return /*#__PURE__*/React.createElement(Provider, {
    store: store
  }, /*#__PURE__*/React.createElement(Editor, props));
}
function StandaloneAlignment(props) {
  if (!store) {
    store = makeStore();
  }
  return /*#__PURE__*/React.createElement(Provider, {
    store: store
  }, /*#__PURE__*/React.createElement(AlignmentView, _extends({}, props, {
    dimensions: {
      width: props.size.width
    }
  })));
}
function StandaloneVersionHistoryView(props) {
  if (!store) {
    store = makeStore();
  }
  return /*#__PURE__*/React.createElement(Provider, {
    store: store
  }, /*#__PURE__*/React.createElement(VersionHistoryView, _extends({}, props)));
}
export default 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 = makeStore();
  }
  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 = render( /*#__PURE__*/React.createElement(StandaloneEditor, _extends({
    editorName: editorName
  }, rest)), node);
  editor.close = function () {
    unmountComponentAtNode(node);
    node.remove();
  };
  editor.updateEditor = function (values) {
    updateEditor(store, editorName, values);
  };
  editor.addAlignment = function (values) {
    addAlignment(store, values);
  };
  editor.getState = function () {
    return store.getState().VectorEditor[editorName];
  };
  return editor;
}
export 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 = makeStore();
  }
  var editor = {};
  editor.renderResponse = render( /*#__PURE__*/React.createElement(StandaloneVersionHistoryView, _extends({
    editorName: editorName
  }, rest)), node);
  editor.updateEditor = function (values) {
    updateEditor(store, editorName, values);
  };
  editor.getState = function () {
    return store.getState().VectorEditor["StandaloneVersionHistoryView"];
  };
  return editor;
}
var SizedStandaloneAlignment = sizeMe()(StandaloneAlignment);
export function createAlignmentView(node, props) {
  if (props === void 0) {
    props = {};
  }
  if (!store) {
    store = makeStore();
  }
  var editor = {};
  editor.renderResponse = render( /*#__PURE__*/React.createElement(SizedStandaloneAlignment, props), node);
  editor.updateAlignment = function (values) {
    addAlignment(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;