"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

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

var _immutable = _interopRequireDefault(require("immutable"));

var _get = _interopRequireDefault(require("lodash/get"));

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

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

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

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

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

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

class SubrecordEditor extends _react.Component {
  constructor() {
    super();
    this.handleDetachButtonClick = this.handleDetachButtonClick.bind(this);
  }

  handleDetachButtonClick() {
    const {
      config,
      containerCsid,
      name,
      subrecordConfig,
      detachSubrecord
    } = this.props;
    const subrecordTypeConfig = (0, _get.default)(config, ['recordTypes', subrecordConfig.recordType]);

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

  render() {
    const {
      config,
      csid,
      data,
      formName,
      perms,
      showDetachButton,
      subrecordConfig,
      readOnly
    } = this.props;
    const {
      recordType,
      vocabulary
    } = subrecordConfig;
    const recordTypeConfig = config.recordTypes[recordType];

    if (!recordTypeConfig) {
      return null;
    }

    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) ? _react.default.createElement(_SubrecordDetachButton.default, {
      onClick: this.handleDetachButtonClick
    }) : null;
    return _react.default.createElement("div", {
      className: _SubrecordEditor.default.common
    }, _react.default.createElement("header", null, detachButton), _react.default.createElement("div", null, _react.default.createElement(_RecordFormContainer.default, {
      config: config,
      recordType: recordType,
      recordTypeConfig: recordTypeConfig,
      vocabulary: vocabulary,
      csid: csid,
      data: data,
      formName: formName,
      readOnly: subrecordReadOnly
    })));
  }

}

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