"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 _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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,
      title = props.schema.title,
      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$colla.separate,
      separate = _props$uiSchema$colla5 === undefined ? true : _props$uiSchema$colla5,
      _props$uiSchema$colla6 = _props$uiSchema$colla.wrapClassName,
      wrapClassName = _props$uiSchema$colla6 === undefined ? "lead" : _props$uiSchema$colla6;

  return _react2.default.createElement(
    "div",
    { className: wrapClassName },
    _react2.default.createElement(
      "span",
      null,
      title
    ),
    "\xA0",
    _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.handleCollapsed = function () {
      _this.setState(function (state) {
        return { collapsed: !state.collapsed };
      });
    };

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


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

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

      var CollapseElement = fields[field];

      return _react2.default.createElement(
        "div",
        null,
        _react2.default.createElement(CollapseMenu, {
          schema: schema,
          uiSchema: uiSchema,
          collapsed: collapsed,
          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;