"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 _reactIntl = require("react-intl");

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

var _InvocationDescriptorEditor = _interopRequireDefault(require("./InvocationDescriptorEditor"));

var _configHelpers = require("../../helpers/configHelpers");

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

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

var _InvocationEditor = _interopRequireDefault(require("../../../styles/cspace-ui/InvocationEditor.css"));

var _FormStatusMessage = _interopRequireDefault(require("../../../styles/cspace-ui/FormStatusMessage.css"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

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

const messages = (0, _reactIntl.defineMessages)({
  loading: {
    "id": "invocationEditor.loading",
    "defaultMessage": "Loading\u2026"
  },
  noDescription: {
    "id": "invocationEditor.noDescription",
    "defaultMessage": "Description not provided."
  }
});

const renderLoading = () => /*#__PURE__*/_react.default.createElement("div", {
  className: _InvocationEditor.default.pending
}, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.loading));

const propTypes = {
  allowedModes: _propTypes.default.arrayOf(_propTypes.default.string),
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  invocationDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  modeReadOnly: _propTypes.default.bool,
  invocationTargetReadOnly: _propTypes.default.bool,
  isInvocationTargetModified: _propTypes.default.bool,
  metadata: _propTypes.default.instanceOf(_immutable.default.Map),
  paramData: _propTypes.default.instanceOf(_immutable.default.Map),
  recordType: _propTypes.default.string,
  createNewRecord: _propTypes.default.func,
  onInvocationDescriptorCommit: _propTypes.default.func
};

class InvocationEditor extends _react.Component {
  componentDidMount() {
    this.init();
  }

  componentDidUpdate(prevProps) {
    const {
      metadata: prevMetadata
    } = prevProps;
    const {
      metadata
    } = this.props;

    if (prevMetadata !== metadata) {
      this.init();
    }
  }

  getSupportedModes() {
    const {
      allowedModes,
      metadata
    } = this.props;
    let modes = [];

    if ((0, _recordDataHelpers.getCommonFieldValue)(metadata, 'supportsNoContext') === 'true') {
      modes.push('nocontext');
    }

    if ((0, _recordDataHelpers.getCommonFieldValue)(metadata, 'supportsDocList') === 'true') {
      modes.push('list');
    }

    if ((0, _recordDataHelpers.getCommonFieldValue)(metadata, 'supportsGroup') === 'true') {
      modes.push('group');
    }

    if ((0, _recordDataHelpers.getCommonFieldValue)(metadata, 'supportsSingleDoc') === 'true') {
      modes.push('single');
    }

    if (allowedModes) {
      modes = modes.filter(mode => allowedModes.includes(mode));
    }

    return modes;
  }

  getSupportedRecordTypes() {
    const {
      config,
      metadata
    } = this.props;
    const forDocTypesContainer = (0, _recordDataHelpers.getCommonFieldValue)(metadata, 'forDocTypes');
    let forDocTypes = forDocTypesContainer && forDocTypesContainer.get('forDocType');

    if (forDocTypes) {
      if (!_immutable.default.List.isList(forDocTypes)) {
        forDocTypes = _immutable.default.List.of(forDocTypes);
      }

      const recordTypes = forDocTypes.map(forDocType => (0, _configHelpers.getRecordTypeNameByServiceObjectName)(config, forDocType)).toJS();
      return recordTypes;
    }

    return [];
  }

  init() {
    const {
      createNewRecord
    } = this.props; // Create a params record.

    createNewRecord();
  }

  render() {
    const {
      config,
      invocationDescriptor,
      modeReadOnly,
      invocationTargetReadOnly,
      isInvocationTargetModified,
      metadata,
      paramData,
      recordType,
      onInvocationDescriptorCommit
    } = this.props;

    if (!metadata) {
      return renderLoading();
    }

    const invocableNameGetter = (0, _get.default)(config, ['recordTypes', recordType, 'invocableName']);
    const invocableName = invocableNameGetter && invocableNameGetter(metadata);
    const paramRecordTypeConfig = (0, _get.default)(config, ['invocables', recordType, invocableName]);

    const description = (0, _recordDataHelpers.getCommonFieldValue)(metadata, 'notes') || /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.noDescription);

    const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
    const recordTypeMessages = (0, _get.default)(recordTypeConfig, ['messages', 'record']);
    const mode = invocationDescriptor.get('mode');
    const items = invocationDescriptor.get('items');
    let missingTargetError;

    if (mode !== 'nocontext' && (!items || items.isEmpty())) {
      missingTargetError = /*#__PURE__*/_react.default.createElement("p", {
        className: _FormStatusMessage.default.error
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, recordTypeMessages[`${mode}TargetMissing`]));

      if (modeReadOnly && invocationTargetReadOnly) {
        return /*#__PURE__*/_react.default.createElement("div", {
          className: _InvocationEditor.default.common
        }, /*#__PURE__*/_react.default.createElement("p", null, description), missingTargetError);
      }
    }

    let unsavedWarning;

    if (isInvocationTargetModified) {
      unsavedWarning = /*#__PURE__*/_react.default.createElement("p", {
        className: _FormStatusMessage.default.warning
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, recordTypeMessages.invokeUnsaved));
    }

    return /*#__PURE__*/_react.default.createElement("div", {
      className: _InvocationEditor.default.common
    }, /*#__PURE__*/_react.default.createElement("p", null, description), /*#__PURE__*/_react.default.createElement(_InvocationDescriptorEditor.default, {
      config: config,
      invocationDescriptor: invocationDescriptor,
      modes: this.getSupportedModes(),
      modeReadOnly: modeReadOnly,
      invocationTargetReadOnly: invocationTargetReadOnly,
      recordTypes: this.getSupportedRecordTypes(),
      onCommit: onInvocationDescriptorCommit
    }), missingTargetError, unsavedWarning, /*#__PURE__*/_react.default.createElement(_RecordFormContainer.default, {
      config: config,
      csid: "",
      data: paramData,
      recordType: "invocable",
      recordTypeConfig: paramRecordTypeConfig
    }));
  }

}

exports.default = InvocationEditor;
InvocationEditor.propTypes = propTypes;