"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DEFAULT_EDITORS = undefined;

var _extends = Object.assign || 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; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.default = playground;

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _EditorsPanel = require("./editor/EditorsPanel");

var _EditorsPanel2 = _interopRequireDefault(_EditorsPanel);

var _Header = require("./Header");

var _Header2 = _interopRequireDefault(_Header);

var _deepEqual = require("deep-equal");

var _deepEqual2 = _interopRequireDefault(_deepEqual);

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

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var DEFAULT_EDITORS = exports.DEFAULT_EDITORS = [{
  type: "json",
  title: "Schema",
  prop: "schema"
}, {
  type: "json",
  title: "UI",
  prop: "uiSchema"
}, {
  type: "json",
  title: "Data",
  prop: "formData"
}];

function playground(FormComponent) {
  var extraEditors = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

  var editors = DEFAULT_EDITORS.concat(extraEditors);

  var App = function (_Component) {
    _inherits(App, _Component);

    function App(props) {
      _classCallCheck(this, App);

      var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));

      _initialiseProps.call(_this);

      _this.state = _this.toState(_this.props);
      return _this;
    }

    _createClass(App, [{
      key: "componentWillReceiveProps",
      value: function componentWillReceiveProps(nextProps) {
        if (!(0, _deepEqual2.default)(this.props, nextProps)) {
          this.setState(this.toState(nextProps));
        }
      }
    }, {
      key: "render",
      value: function render() {
        var _this2 = this;

        console.log("Rendering");
        editors.map(function (editor) {
          editor.source = _this2.state[editor.prop] ? _this2.state[editor.prop] : _this2.props[editor.prop];
          editor.onChange = function (data) {
            _this2.setState(_defineProperty({}, editor.prop, data));
          };
        });

        var conf = Object.assign({}, this.props, this.state, this.state.formParams);

        return _react2.default.createElement(
          "div",
          { className: "container-fluid" },
          _react2.default.createElement(_Header2.default, {
            load: this.load,
            onUpdateMeta: this.onUpdateMeta,
            formParams: this.state.formParams
          }),
          _react2.default.createElement(
            "div",
            { className: "col-md-6" },
            _react2.default.createElement(FormComponent, _extends({ onChange: this.onFormChange }, conf))
          ),
          _react2.default.createElement(
            "div",
            { className: "col-md-6" },
            _react2.default.createElement(_EditorsPanel2.default, { editors: editors })
          )
        );
      }
    }]);

    return App;
  }(_react.Component);

  var _initialiseProps = function _initialiseProps() {
    var _this3 = this;

    this.onUpdateMeta = function (formParams) {
      return _this3.setState({ formParams: formParams });
    };

    this.onFormChange = function (_ref) {
      var formData = _ref.formData;
      return _this3.setState({ formData: formData });
    };

    this.toState = function (props) {
      return editors.reduce(function (agg, _ref2) {
        var prop = _ref2.prop;
        return Object.assign({}, agg, _defineProperty({}, prop, props[prop]));
      }, {});
    };
  };

  return App;
}