'use strict';

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

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _get2 = _interopRequireDefault(_get);

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

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

var _CancelButton = require('../navigation/CancelButton');

var _CancelButton2 = _interopRequireDefault(_CancelButton);

var _DeleteButton = require('../record/DeleteButton');

var _DeleteButton2 = _interopRequireDefault(_DeleteButton);

var _ConfirmRecordDeleteModal = require('../../../styles/cspace-ui/ConfirmRecordDeleteModal.css');

var _ConfirmRecordDeleteModal2 = _interopRequireDefault(_ConfirmRecordDeleteModal);

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

const messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'confirmRecordDeleteModal.title',
    'defaultMessage': 'Delete {recordName}'
  },
  prompt: {
    'id': 'confirmRecordDeleteModal.prompt',
    'defaultMessage': 'Delete {title}?'
  },
  hasRelations: {
    'id': 'confirmRecordDeleteModal.hasRelations',
    'defaultMessage': 'This record is related to other records. Deleting this record will cause those relationships to be lost.'
  },
  hasUses: {
    'id': 'confirmRecordDeleteModal.hasUses',
    'defaultMessage': '{title} cannot be deleted because it is used by other records.'
  },
  hasRoleUses: {
    'id': 'confirmRecordDeleteModal.hasRoleUses',
    'defaultMessage': '{title} cannot be deleted because it is associated with user accounts.'
  },
  hasHierarchy: {
    'id': 'confirmRecordDeleteModal.hasHierarchy',
    'defaultMessage': '{title} cannot be deleted because it belongs to a hierarchy. To delete this record, first remove its broader and narrower records.'
  }
});

const propTypes = {
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  data: _propTypes2.default.instanceOf(_immutable2.default.Map),
  isOpen: _propTypes2.default.bool,
  isSavePending: _propTypes2.default.bool,
  recordType: _propTypes2.default.string,
  /* eslint-disable react/no-unused-prop-types */
  // These actually are used, but not detected by eslint.
  checkForRelations: _propTypes2.default.func,
  checkForUses: _propTypes2.default.func,
  /* eslint-enable react/no-unused-prop-types */
  onCancelButtonClick: _propTypes2.default.func,
  onCloseButtonClick: _propTypes2.default.func,
  onDeleteButtonClick: _propTypes2.default.func
};

const contextTypes = {
  intl: _reactIntl.intlShape
};

class ConfirmRecordDeleteModal extends _react.Component {
  constructor(props) {
    super(props);

    this.renderButtonBar = this.renderButtonBar.bind(this);

    this.state = {};
  }

  componentWillMount() {
    if (this.props.isOpen) {
      this.init(this.props);
    }
  }

  componentWillUpdate(nextProps) {
    if (!this.props.isOpen && nextProps.isOpen) {
      this.init(nextProps);
    }
  }

  init(props) {
    // On open check if the record has relations.

    const config = props.config,
          recordType = props.recordType,
          checkForRelations = props.checkForRelations,
          checkForUses = props.checkForUses,
          checkForRoleUses = props.checkForRoleUses;


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

    if ((serviceType === 'procedure' || serviceType === 'object') && checkForRelations) {
      this.setState({
        hasRelations: undefined,
        hasUses: false
      });

      checkForRelations('affects').then(hasRelations => {
        this.setState({
          hasRelations
        });
      });
    } else if (serviceType === 'authority' && checkForUses) {
      this.setState({
        hasRelations: false,
        hasUses: undefined
      });

      checkForUses().then(hasUses => {
        this.setState({
          hasUses
        });
      });
    } else if (recordType === 'authrole' && checkForRoleUses) {
      this.setState({
        hasRelations: false,
        hasUses: undefined
      });

      checkForRoleUses().then(hasUses => {
        this.setState({
          hasUses
        });
      });
    } else {
      this.setState({
        hasRelations: false,
        hasUses: false
      });
    }
  }

  renderButtonBar() {
    var _props = this.props;
    const csid = _props.csid,
          data = _props.data,
          isSavePending = _props.isSavePending,
          onCancelButtonClick = _props.onCancelButtonClick,
          onDeleteButtonClick = _props.onDeleteButtonClick;
    var _state = this.state;
    const hasRelations = _state.hasRelations,
          hasUses = _state.hasUses;


    let deleteButton;

    if (!(0, _recordDataHelpers.hasHierarchyRelations)(data) && !hasUses) {
      deleteButton = _react2.default.createElement(_DeleteButton2.default, {
        csid: csid
        // Disable the delete button while the check for relations is pending.
        , disabled: typeof hasRelations === 'undefined'
        // Assume the record is deletable if this modal is being shown.
        , isDeletable: true,
        isSavePending: isSavePending,
        onClick: onDeleteButtonClick
      });
    }

    return _react2.default.createElement(
      'div',
      null,
      _react2.default.createElement(_CancelButton2.default, {
        disabled: isSavePending,
        onClick: onCancelButtonClick
      }),
      deleteButton
    );
  }

  render() {
    var _props2 = this.props;
    const config = _props2.config,
          data = _props2.data,
          isOpen = _props2.isOpen,
          recordType = _props2.recordType,
          onCloseButtonClick = _props2.onCloseButtonClick;


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

    const intl = this.context.intl;
    var _state2 = this.state;
    const hasRelations = _state2.hasRelations,
          hasUses = _state2.hasUses;


    const recordTypeConfig = config.recordTypes[recordType];
    const recordName = intl.formatMessage(recordTypeConfig.messages.record.name);
    const title = recordTypeConfig.title(data, { config, intl });

    let prompt;
    let hasRelationsMessage;
    let hasHierarchyMessage;
    let hasUsesMessage;

    if (typeof hasRelations !== 'undefined' && typeof hasUses !== 'undefined') {
      if ((0, _recordDataHelpers.hasHierarchyRelations)(data)) {
        hasHierarchyMessage = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.hasHierarchy, { values: { title } }));
      } else if (hasUses) {
        hasUsesMessage = recordType === 'authrole' ? _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.hasRoleUses, { values: { title } })) : _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.hasUses, { values: { title } }));
      } else {
        prompt = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.prompt, { values: { title } }));

        hasRelationsMessage = hasRelations ? _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement('br', null),
          _react2.default.createElement(_reactIntl.FormattedMessage, messages.hasRelations)
        ) : null;
      }
    }

    return _react2.default.createElement(
      _cspaceLayout.Modal,
      {
        className: _ConfirmRecordDeleteModal2.default.common,
        isOpen: isOpen,
        title: _react2.default.createElement(
          'h1',
          null,
          _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.title, { values: { recordName } }))
        ),
        closeButtonClassName: 'material-icons',
        closeButtonLabel: 'close',
        renderButtonBar: this.renderButtonBar,
        onCloseButtonClick: onCloseButtonClick
      },
      prompt,
      hasRelationsMessage,
      hasHierarchyMessage,
      hasUsesMessage
    );
  }
}

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