"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 (_ref) {
      var items = _ref.items;
      var formData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

      var newVal = (0, _utils.getDefaultFormState)(items, {});
      if (formData.some(function (v) {
        return (0, _utils.deepEquals)(v, newVal);
      })) {
        return formData;
      } else {
        return formData.concat(newVal);
      }
    };

    _this.handleAdd = function () {
      var _this$props = _this.props,
          schema = _this$props.schema,
          formData = _this$props.formData,
          onChange = _this$props.onChange,
          addTo = _this$props.uiSchema.collapse.addTo;

      if (addTo === "self") {
        onChange(_this.appendToArray(schema, formData));
      } else {
        formData = formData ? formData : {};
        var fieldVal = _this.appendToArray(schema.properties[addTo], formData[addTo]);
        var change = Object.assign({}, formData, _defineProperty({}, addTo, fieldVal));
        onChange(change);
      }
      _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 collapsed = this.state.collapsed;
      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" },
          !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
    }).isRequired
  }).isRequired,
  registry: _propTypes2.default.shape({
    fields: _propTypes2.default.object.isRequired
  }).isRequired
};

exports.default = CollapsibleField;