'use strict';

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

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

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

class SubrecordEditor extends _react.Component {
  constructor() {
    super();

    this.handleDetachButtonClick = this.handleDetachButtonClick.bind(this);
  }

  handleDetachButtonClick() {
    var _props = this.props;
    const config = _props.config,
          containerCsid = _props.containerCsid,
          name = _props.name,
          subrecordConfig = _props.subrecordConfig,
          detachSubrecord = _props.detachSubrecord;


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

    if (detachSubrecord) {
      detachSubrecord(config, containerCsid, subrecordConfig.csidField, name, subrecordTypeConfig);
    }
  }

  render() {
    var _props2 = this.props;
    const config = _props2.config,
          csid = _props2.csid,
          data = _props2.data,
          formName = _props2.formName,
          perms = _props2.perms,
          showDetachButton = _props2.showDetachButton,
          subrecordConfig = _props2.subrecordConfig,
          readOnly = _props2.readOnly;
    const recordType = subrecordConfig.recordType,
          vocabulary = subrecordConfig.vocabulary;


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

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

    const 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(
        'header',
        null,
        detachButton
      ),
      _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_RecordFormContainer2.default, {
          config: config,
          recordType: recordType,
          vocabulary: vocabulary,
          csid: csid,
          data: data,
          formName: formName,
          readOnly: subrecordReadOnly
        })
      )
    );
  }
}

exports.default = SubrecordEditor;
SubrecordEditor.propTypes = propTypes;