"use strict";

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

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; }; }();

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _utils = require("react-jsonschema-form/lib/utils");

var _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

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; }

function CollapseMenu(props) {
  var onChange = props.onChange,
      onAdd = props.onAdd,
      title = props.title,
      name = props.name,
      collapsed = props.collapsed;
  var _props$uiSchema$colla = props.uiSchema.collapse,
      _props$uiSchema$colla2 = _props$uiSchema$colla.icon;
  _props$uiSchema$colla2 = _props$uiSchema$colla2 === undefined ? {} : _props$uiSchema$colla2;
  var _props$uiSchema$colla3 = _props$uiSchema$colla2.enabled,
      enabled = _props$uiSchema$colla3 === undefined ? "glyphicon glyphicon-chevron-down" : _props$uiSchema$colla3,
      _props$uiSchema$colla4 = _props$uiSchema$colla2.disabled,
      disabled = _props$uiSchema$colla4 === undefined ? "glyphicon glyphicon-chevron-right" : _props$uiSchema$colla4,
      _props$uiSchema$colla5 = _props$uiSchema$colla2.add,
      add = _props$uiSchema$colla5 === undefined ? "glyphicon glyphicon-plus-sign" : _props$uiSchema$colla5,
      _props$uiSchema$colla6 = _props$uiSchema$colla.separate,
      separate = _props$uiSchema$colla6 === undefined ? true : _props$uiSchema$colla6,
      addTo = _props$uiSchema$colla.addTo,
      _props$uiSchema$colla7 = _props$uiSchema$colla.wrapClassName,
      wrapClassName = _props$uiSchema$colla7 === undefined ? "lead" : _props$uiSchema$colla7;

  return _react2.default.createElement(
    "div",
    { className: wrapClassName },
    _react2.default.createElement(
      "span",
      null,
      title || name
    ),
    "\xA0",
    addTo && _react2.default.createElement(
      "a",
      { onClick: onAdd },
      _react2.default.createElement("i", { className: add })
    ),
    _react2.default.createElement(
      "a",
      { onClick: onChange },
      _react2.default.createElement("i", { className: collapsed ? disabled : enabled })
    ),
    separate && _react2.default.createElement("hr", null)
  );
}

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

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

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

    _this.appendToArray = function () {
      var formData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var newVal = arguments[1];

      if (formData.some(function (v) {
        return (0, _utils.deepEquals)(v, newVal);
      })) {
        return formData;
      } else {
        // newVal can be either array or a single element, concat flattens value
        return formData.concat(newVal);
      }
    };

    _this.doAdd = function (field, formData, newVal) {
      if (field === "self") {
        _this.props.onChange(_this.appendToArray(formData, newVal));
      } else {
        var fieldVal = _this.appendToArray(formData[field], newVal);
        var change = Object.assign({}, formData, _defineProperty({}, field, fieldVal));
        _this.props.onChange(change);
      }
    };

    _this.handleAdd = function () {
      var _this$props = _this.props,
          schema = _this$props.schema,
          uiSchema = _this$props.uiSchema,
          formData = _this$props.formData,
          fields = _this$props.registry.fields;
      var _uiSchema$collapse = uiSchema.collapse,
          addTo = _uiSchema$collapse.addTo,
          addElement = _uiSchema$collapse.addElement;


      var fieldSchema = addTo === "self" ? schema.items : schema.properties[addTo].items;
      var fieldUiSchema = addTo === "self" ? uiSchema : uiSchema[addTo];

      if (addElement) {
        if (typeof addElement === "function") {
          var onSubmit = function onSubmit(newVal) {
            _this.setState({ AddElement: undefined });
            _this.doAdd(addTo, formData, newVal);
          };
          var AddElement = addElement(fieldSchema, fieldUiSchema, onSubmit);
          _this.setState({ AddElement: AddElement });
        } else {
          var FieldElement = fields[addElement];
          var onBlur = function onBlur(newVal) {
            _this.setState({ AddElement: undefined });
            _this.doAdd(addTo, formData, newVal);
          };
          var _AddElement = function _AddElement() {
            return _react2.default.createElement(FieldElement, {
              schema: fieldSchema,
              uiSchema: fieldUiSchema,
              onChange: function onChange(formData) {
                onBlur(formData);
              }
            });
          };
          _this.setState({ AddElement: _AddElement });
        }
      } else {
        var newVal = (0, _utils.getDefaultFormState)(fieldSchema, {});
        _this.doAdd(addTo, formData, newVal);
      }

      _this.setState({ collapsed: false });
    };

    _this.handleCollapsed = function () {
      _this.setState(function (state) {
        return { collapsed: !state.collapsed };
      });
    };

    var _props$uiSchema$colla8 = props.uiSchema.collapse;
    _props$uiSchema$colla8 = _props$uiSchema$colla8 === undefined ? {} : _props$uiSchema$colla8;
    var _props$uiSchema$colla9 = _props$uiSchema$colla8.collapsed,
        collapsed = _props$uiSchema$colla9 === undefined ? true : _props$uiSchema$colla9;


    _this.state = { collapsed: collapsed };
    return _this;
  }

  _createClass(CollapsibleField, [{
    key: "render",
    value: function render() {
      var _props = this.props,
          title = _props.schema.title,
          uiSchema = _props.uiSchema,
          fields = _props.registry.fields,
          name = _props.name;
      var _state = this.state,
          collapsed = _state.collapsed,
          AddElement = _state.AddElement;
      var field = uiSchema.collapse.field;

      var CollapseElement = fields[field];

      title = uiSchema["ui:title"] ? uiSchema["ui:title"] : title ? title : name;

      return _react2.default.createElement(
        "div",
        null,
        _react2.default.createElement(CollapseMenu, {
          title: title,
          uiSchema: uiSchema,
          collapsed: collapsed,
          onAdd: this.handleAdd,
          onChange: this.handleCollapsed
        }),
        _react2.default.createElement(
          "div",
          { className: "form-group" },
          AddElement && _react2.default.createElement(AddElement, null),
          !collapsed && _react2.default.createElement(CollapseElement, this.props)
        )
      );
    }
  }]);

  return CollapsibleField;
}(_react.Component);

CollapsibleField.propTypes = {
  uiSchema: _propTypes2.default.shape({
    collapse: _propTypes2.default.shape({
      field: _propTypes2.default.string.isRequired,
      icon: _propTypes2.default.shape({
        enabled: _propTypes2.default.string,
        disabled: _propTypes2.default.string,
        add: _propTypes2.default.string
      }),
      separate: _propTypes2.default.boolean,
      addTo: _propTypes2.default.string,
      addElement: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.string]),
      wrapClassName: _propTypes2.default.string
    }).isRequired
  }).isRequired,
  registry: _propTypes2.default.shape({
    fields: _propTypes2.default.object.isRequired
  }).isRequired
};

exports.default = CollapsibleField;