"use strict";

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

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

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

var _reactIntl = require("react-intl");

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

var _cspaceLayout = require("cspace-layout");

var _CancelButton = _interopRequireDefault(require("../navigation/CancelButton"));

var _UnrelateButton = _interopRequireDefault(require("../record/UnrelateButton"));

var _ConfirmRecordUnrelateModal = _interopRequireDefault(require("../../../styles/cspace-ui/ConfirmRecordUnrelateModal.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; } }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "confirmRecordUnrelateModal.title",
    "defaultMessage": "Unrelate {recordName}"
  },
  prompt: {
    "id": "confirmRecordUnrelateModal.prompt",
    "defaultMessage": "Unrelate {title} from the primary record?"
  },
  promptMultiple: {
    "id": "confirmRecordUnrelateModal.promptMultiple",
    "defaultMessage": "Unrelate {recordCount, plural, one {the selected record} other {# selected records}} from the primary record?"
  },
  unrelating: {
    "id": "confirmRecordUnrelateModal.unrelating",
    "defaultMessage": "Unrelating..."
  }
});
const propTypes = {
  config: _propTypes.default.object,
  data: _propTypes.default.instanceOf(_immutable.default.Map),
  isOpen: _propTypes.default.bool,
  isMultiSelect: _propTypes.default.bool,
  isUnrelating: _propTypes.default.bool,
  recordCount: _propTypes.default.number,
  recordType: _propTypes.default.string,
  onCancelButtonClick: _propTypes.default.func,
  onCloseButtonClick: _propTypes.default.func,
  onUnrelateButtonClick: _propTypes.default.func
};
const contextTypes = {
  intl: _reactIntl.intlShape
};

class ConfirmRecordUnrelateModal extends _react.Component {
  constructor(props) {
    super(props);
    this.renderButtonBar = this.renderButtonBar.bind(this);
  }

  renderButtonBar() {
    const {
      isUnrelating,
      onCancelButtonClick,
      onUnrelateButtonClick
    } = this.props;
    return _react.default.createElement("div", null, _react.default.createElement(_CancelButton.default, {
      disabled: isUnrelating,
      onClick: onCancelButtonClick
    }), _react.default.createElement(_UnrelateButton.default, {
      isUnrelating: isUnrelating,
      onClick: onUnrelateButtonClick
    }));
  }

  render() {
    const {
      config,
      data,
      isMultiSelect,
      isOpen,
      isUnrelating,
      recordCount,
      recordType,
      onCloseButtonClick
    } = this.props;

    if (!isOpen || !isMultiSelect && !data || isMultiSelect && !recordCount) {
      return null;
    }

    const {
      intl
    } = this.context;
    const recordTypeConfig = config.recordTypes[recordType];
    const recordNameMessage = isMultiSelect ? 'collectionName' : 'name';
    const recordName = intl.formatMessage(recordTypeConfig.messages.record[recordNameMessage]);
    let content;

    if (isUnrelating) {
      content = _react.default.createElement(_reactIntl.FormattedMessage, messages.unrelating);
    } else if (isMultiSelect) {
      content = _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.promptMultiple, {
        values: {
          recordCount
        }
      }));
    } else {
      const title = recordTypeConfig.title(data, {
        config,
        intl
      });
      content = _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.prompt, {
        values: {
          title
        }
      }));
    }

    return _react.default.createElement(_cspaceLayout.Modal, {
      className: _ConfirmRecordUnrelateModal.default.common,
      isOpen: isOpen,
      title: _react.default.createElement("h1", null, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.title, {
        values: {
          recordName
        }
      }))),
      closeButtonClassName: "material-icons",
      closeButtonLabel: "close",
      renderButtonBar: this.renderButtonBar,
      onCloseButtonClick: onCloseButtonClick
    }, content);
  }

}

exports.default = ConfirmRecordUnrelateModal;
ConfirmRecordUnrelateModal.modalName = 'ConfirmRecordUnrelateModal';
ConfirmRecordUnrelateModal.propTypes = propTypes;
ConfirmRecordUnrelateModal.contextTypes = contextTypes;