'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);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _RecordFormContainer = require('../../containers/record/RecordFormContainer');

var _RecordFormContainer2 = _interopRequireDefault(_RecordFormContainer);

var _SubrecordDetachButton = require('../../components/record/SubrecordDetachButton');

var _SubrecordDetachButton2 = _interopRequireDefault(_SubrecordDetachButton);

var _permissionHelpers = require('../../helpers/permissionHelpers');

var _recordDataHelpers = require('../../helpers/recordDataHelpers');

var _SubrecordEditor = require('../../../styles/cspace-ui/SubrecordEditor.css');

var _SubrecordEditor2 = _interopRequireDefault(_SubrecordEditor);

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

var propTypes = {
  config: _propTypes2.default.object,
  subrecordConfig: _propTypes2.default.object,
  containerCsid: _propTypes2.default.string,
  csid: _propTypes2.default.string,
  data: _propTypes2.default.instanceOf(_immutable2.default.Map),
  formName: _propTypes2.default.string,
  name: _propTypes2.default.string,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  showDetachButton: _propTypes2.default.bool,
  detachSubrecord: _propTypes2.default.func,
  readOnly: _propTypes2.default.bool
};

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

  function SubrecordEditor() {
    _classCallCheck(this, SubrecordEditor);

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

    _this.handleDetachButtonClick = _this.handleDetachButtonClick.bind(_this);
    return _this;
  }

  _createClass(SubrecordEditor, [{
    key: 'handleDetachButtonClick',
    value: function handleDetachButtonClick() {
      var _props = this.props,
          config = _props.config,
          containerCsid = _props.containerCsid,
          name = _props.name,
          subrecordConfig = _props.subrecordConfig,
          detachSubrecord = _props.detachSubrecord;


      var subrecordTypeConfig = (0, _get2.default)(config, ['recordTypes', subrecordConfig.recordType]);

      if (detachSubrecord) {
        detachSubrecord(config, containerCsid, subrecordConfig.csidField, name, subrecordTypeConfig);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          config = _props2.config,
          csid = _props2.csid,
          data = _props2.data,
          formName = _props2.formName,
          perms = _props2.perms,
          showDetachButton = _props2.showDetachButton,
          subrecordConfig = _props2.subrecordConfig,
          readOnly = _props2.readOnly;
      var recordType = subrecordConfig.recordType,
          vocabulary = subrecordConfig.vocabulary;


      if (!(0, _permissionHelpers.canRead)(recordType, perms)) {
        return null;
      }

      var subrecordReadOnly = readOnly || !(csid ? (0, _permissionHelpers.canUpdate)(recordType, perms) : (0, _permissionHelpers.canCreate)(recordType, perms)) || (0, _recordDataHelpers.isRecordImmutable)(data);

      var detachButton = showDetachButton && !readOnly && (0, _recordDataHelpers.isExistingRecord)(data) ? _react2.default.createElement(_SubrecordDetachButton2.default, { onClick: this.handleDetachButtonClick }) : null;

      return _react2.default.createElement(
        'div',
        { className: _SubrecordEditor2.default.common },
        _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement(_RecordFormContainer2.default, {
            config: config,
            recordType: recordType,
            vocabulary: vocabulary,
            csid: csid,
            data: data,
            formName: formName,
            readOnly: subrecordReadOnly
          })
        ),
        detachButton
      );
    }
  }]);

  return SubrecordEditor;
}(_react.Component);

exports.default = SubrecordEditor;


SubrecordEditor.propTypes = propTypes;