'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _get2 = _interopRequireDefault(_get);

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

var _ReportModal = require('./ReportModal');

var _ReportModal2 = _interopRequireDefault(_ReportModal);

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

var _reportHelpers = require('../../helpers/reportHelpers');

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

var _SearchPanelContainer = require('../../containers/search/SearchPanelContainer');

var _SearchPanelContainer2 = _interopRequireDefault(_SearchPanelContainer);

var _searchNames = require('../../constants/searchNames');

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

const messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'recordReportPanel.title',
    'defaultMessage': 'Reports'
  }
}); /* global window */

const getSearchDescriptor = objectName => _immutable2.default.fromJS({
  recordType: 'report',
  searchQuery: {
    p: 0,
    size: 5,
    doctype: objectName
  }
});

const propTypes = {
  color: _propTypes2.default.string,
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  isRecordModified: _propTypes2.default.bool,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  recordData: _propTypes2.default.instanceOf(_immutable2.default.Map),
  recordType: _propTypes2.default.string
};

class RecordReportPanel 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.handleModalRunButtonClick = this.handleModalRunButtonClick.bind(this);
    this.handleSearchDescriptorChange = this.handleSearchDescriptorChange.bind(this);

    var _props = this.props;
    const config = _props.config,
          recordType = _props.recordType;


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

    this.state = {
      searchDescriptor: getSearchDescriptor(objectName)
    };
  }

  componentWillReceiveProps(nextProps) {
    const recordType = this.props.recordType;
    const config = nextProps.config,
          nextRecordType = nextProps.recordType;


    if (nextRecordType !== recordType) {
      const nextObjectName = (0, _get2.default)(config, ['recordTypes', nextRecordType, 'serviceConfig', 'objectName']);

      this.setState({
        searchDescriptor: getSearchDescriptor(nextObjectName)
      });
    }
  }

  handleItemClick(item) {
    this.setState({
      isModalOpen: true,
      selectedItem: item
    });
  }

  handleModalCancelButtonClick() {
    this.setState({
      isModalOpen: false
    });
  }

  handleModalCloseButtonClick() {
    this.setState({
      isModalOpen: false
    });
  }

  handleModalRunButtonClick() {
    var _props2 = this.props;
    const config = _props2.config,
          csid = _props2.csid,
          recordType = _props2.recordType;
    const selectedItem = this.state.selectedItem;


    const viewerPath = (0, _reportHelpers.getReportViewerPath)(selectedItem, config, recordType, csid);

    window.open(viewerPath, _reportHelpers.VIEWER_WINDOW_NAME);

    this.setState({
      isModalOpen: false
    });
  }

  handleSearchDescriptorChange(searchDescriptor) {
    this.setState({
      searchDescriptor
    });
  }

  render() {
    var _props3 = this.props;
    const color = _props3.color,
          config = _props3.config,
          csid = _props3.csid,
          isRecordModified = _props3.isRecordModified,
          perms = _props3.perms,
          recordData = _props3.recordData,
          recordType = _props3.recordType;
    var _state = this.state;
    const isModalOpen = _state.isModalOpen,
          searchDescriptor = _state.searchDescriptor,
          selectedItem = _state.selectedItem;


    if (!(0, _recordDataHelpers.isExistingRecord)(recordData)) {
      // Don't render until after the record has loaded.

      return null;
    }

    if (!(0, _permissionHelpers.canList)('report', perms)) {
      return null;
    }

    const canRun = (0, _permissionHelpers.canCreate)('report', perms);

    return _react2.default.createElement(
      'div',
      null,
      _react2.default.createElement(_SearchPanelContainer2.default, {
        collapsed: true,
        color: color,
        config: config,
        csid: csid,
        linkItems: false,
        name: _searchNames.RECORD_REPORT_PANEL_SEARCH_NAME,
        searchDescriptor: searchDescriptor,
        recordType: recordType,
        showSearchButton: false,
        title: _react2.default.createElement(_reactIntl.FormattedMessage, messages.title),
        onItemClick: canRun ? this.handleItemClick : undefined,
        onSearchDescriptorChange: this.handleSearchDescriptorChange
      }),
      _react2.default.createElement(_ReportModal2.default, {
        isOpen: isModalOpen,
        isRecordModified: isRecordModified,
        reportItem: selectedItem,
        onCancelButtonClick: this.handleModalCancelButtonClick,
        onCloseButtonClick: this.handleModalCloseButtonClick,
        onRunButtonClick: this.handleModalRunButtonClick
      })
    );
  }
}

exports.default = RecordReportPanel;
RecordReportPanel.propTypes = propTypes;