"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 _reactIntl = require("react-intl");
var _permissionHelpers = require("../../helpers/permissionHelpers");
var _InvocationModalContainer = _interopRequireDefault(require("../../containers/invocable/InvocationModalContainer"));
var _SearchPanelContainer = _interopRequireDefault(require("../../containers/search/SearchPanelContainer"));
var _searchNames = require("../../constants/searchNames");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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)({
  title: {
    "id": "searchResultReportPanel.title",
    "defaultMessage": "Reports"
  }
});
const getSearchDescriptor = (config, recordType) => {
  const objectName = (0, _get.default)(config, ['recordTypes', recordType, 'serviceConfig', 'objectName']);
  return _immutable.default.fromJS({
    recordType: 'report',
    searchQuery: {
      p: 0,
      size: config.defaultSearchPanelSize || 5,
      doctype: objectName,
      mode: 'list'
    }
  });
};
const propTypes = {
  color: _propTypes.default.string,
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  recordType: _propTypes.default.string,
  selectedItems: _propTypes.default.instanceOf(_immutable.default.Map),
  openReport: _propTypes.default.func
};
class SearchResultReportPanel extends _react.Component {
  constructor(props) {
    super(props);
    this.handleItemClick = this.handleItemClick.bind(this);
    this.handleModalCancelButtonClick = this.handleModalCancelButtonClick.bind(this);
    this.handleModalCloseButtonClick = this.handleModalCloseButtonClick.bind(this);
    this.handleModalInvokeButtonClick = this.handleModalInvokeButtonClick.bind(this);
    this.handleSearchDescriptorChange = this.handleSearchDescriptorChange.bind(this);
    const {
      config,
      recordType
    } = this.props;
    this.state = {
      searchDescriptor: getSearchDescriptor(config, recordType)
    };
  }

  // eslint-disable-next-line camelcase
  UNSAFE_componentWillReceiveProps(nextProps) {
    const {
      recordType
    } = this.props;
    const {
      config,
      recordType: nextRecordType
    } = nextProps;
    if (nextRecordType !== recordType) {
      this.setState({
        searchDescriptor: getSearchDescriptor(config, nextRecordType)
      });
    }
  }
  handleItemClick(item) {
    this.setState({
      isModalOpen: true,
      selectedItem: item
    });
  }
  handleModalCancelButtonClick() {
    this.setState({
      isModalOpen: false
    });
  }
  handleModalCloseButtonClick() {
    this.setState({
      isModalOpen: false
    });
  }
  handleModalInvokeButtonClick(reportMetadata, invocationDescriptor) {
    const {
      config,
      openReport
    } = this.props;
    if (openReport) {
      openReport(config, reportMetadata, invocationDescriptor).then(() => {
        this.setState({
          isModalOpen: false
        });
      }).catch(() => {});
    }
  }
  handleSearchDescriptorChange(searchDescriptor) {
    this.setState({
      searchDescriptor
    });
  }
  render() {
    const {
      color,
      config,
      perms,
      recordType,
      selectedItems
    } = this.props;
    const {
      isModalOpen,
      searchDescriptor,
      selectedItem
    } = this.state;
    if (!(0, _permissionHelpers.canList)('report', perms)) {
      return null;
    }
    const canRun = (0, _permissionHelpers.canCreate)('report', perms);
    return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_SearchPanelContainer.default, {
      collapsed: true,
      color: color,
      config: config,
      linkItems: false,
      name: _searchNames.SEARCH_RESULT_REPORT_PANEL_SEARCH_NAME,
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      showSearchButton: false,
      title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onItemClick: canRun ? this.handleItemClick : undefined,
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    }), /*#__PURE__*/_react.default.createElement(_InvocationModalContainer.default, {
      config: config,
      csid: selectedItem && selectedItem.get('csid'),
      initialInvocationDescriptor: _immutable.default.Map({
        recordType,
        items: selectedItems,
        mode: 'list'
      }),
      modeReadOnly: true,
      invocationTargetReadOnly: true,
      isOpen: isModalOpen,
      recordType: "report",
      onCancelButtonClick: this.handleModalCancelButtonClick,
      onCloseButtonClick: this.handleModalCloseButtonClick,
      onInvokeButtonClick: this.handleModalInvokeButtonClick
    }));
  }
}
exports.default = SearchResultReportPanel;
SearchResultReportPanel.propTypes = propTypes;