"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 _ExportFieldEditorContainer = _interopRequireDefault(require("../../containers/search/ExportFieldEditorContainer"));
var _ExportButton = _interopRequireDefault(require("./ExportButton"));
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)({
  cancel: {
    "id": "exportModal.cancel",
    "defaultMessage": "Cancel"
  },
  export: {
    "id": "exportModal.export",
    "defaultMessage": "Export"
  },
  label: {
    "id": "exportModal.label",
    "defaultMessage": "Export"
  },
  title: {
    "id": "exportModal.title",
    "defaultMessage": "Export as CSV"
  }
});
const propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  isOpen: _propTypes.default.bool,
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  recordType: _propTypes.default.string,
  selectedItems: _propTypes.default.instanceOf(_immutable.default.Map),
  vocabulary: _propTypes.default.string,
  openExport: _propTypes.default.func,
  onCancelButtonClick: _propTypes.default.func,
  onCloseButtonClick: _propTypes.default.func,
  onExportOpened: _propTypes.default.func
};
const defaultProps = {
  selectedItems: _immutable.default.Map()
};
class ExportModal extends _react.Component {
  constructor(props) {
    super(props);
    this.handleExportButtonClick = this.handleExportButtonClick.bind(this);
    this.handleIncludeFieldsAddInstance = this.handleIncludeFieldsAddInstance.bind(this);
    this.handleIncludeFieldsCommit = this.handleIncludeFieldsCommit.bind(this);
    this.handleIncludeFieldsMoveInstance = this.handleIncludeFieldsMoveInstance.bind(this);
    this.handleIncludeFieldsRemoveInstance = this.handleIncludeFieldsRemoveInstance.bind(this);
    this.renderButtonBar = this.renderButtonBar.bind(this);
    this.state = {
      invocationDescriptor: _immutable.default.fromJS({
        mode: 'list',
        outputMIME: 'text/csv',
        includeFields: []
      })
    };
  }

  // eslint-disable-next-line camelcase
  UNSAFE_componentWillReceiveProps(nextProps) {
    const {
      isOpen
    } = this.props;
    const {
      isOpen: nextIsOpen
    } = nextProps;
    if (!isOpen && nextIsOpen) {
      this.initInvocationDescriptor(nextProps);
    }
  }
  handleIncludeFieldsAddInstance(position) {
    const {
      invocationDescriptor
    } = this.state;
    const includeFields = invocationDescriptor.get('includeFields');
    const nextIncludeFields = typeof position === 'undefined' || position < 0 || position >= includeFields.size ? includeFields.push('') : includeFields.insert(position, '');
    const nextInvocationDescriptor = invocationDescriptor.set('includeFields', nextIncludeFields);
    this.setState({
      invocationDescriptor: nextInvocationDescriptor
    });
  }
  handleIncludeFieldsCommit(position, value) {
    const {
      invocationDescriptor
    } = this.state;
    const nextInvocationDescriptor = invocationDescriptor.setIn(['includeFields', position], value);
    this.setState({
      invocationDescriptor: nextInvocationDescriptor
    });
  }
  handleIncludeFieldsMoveInstance(fromPosition, toPosition) {
    const {
      invocationDescriptor
    } = this.state;
    const includeFields = invocationDescriptor.get('includeFields');
    const value = includeFields.get(fromPosition);
    const nextIncludeFields = includeFields.delete(fromPosition).insert(toPosition, value);
    const nextInvocationDescriptor = invocationDescriptor.set('includeFields', nextIncludeFields);
    this.setState({
      invocationDescriptor: nextInvocationDescriptor
    });
  }
  handleIncludeFieldsRemoveInstance(position) {
    const {
      invocationDescriptor
    } = this.state;
    const nextInvocationDescriptor = invocationDescriptor.deleteIn(['includeFields', position]);
    this.setState({
      invocationDescriptor: nextInvocationDescriptor
    });
  }
  handleExportButtonClick() {
    const {
      config,
      openExport,
      onExportOpened
    } = this.props;
    const {
      invocationDescriptor
    } = this.state;
    if (openExport) {
      openExport(config, invocationDescriptor).then(() => {
        if (onExportOpened) {
          onExportOpened();
        }
      }).catch(() => {});
    }
  }
  initInvocationDescriptor(props) {
    const {
      invocationDescriptor
    } = this.state;
    const {
      recordType,
      vocabulary,
      selectedItems
    } = props;
    const nextInvocationDescriptor = invocationDescriptor.set('recordType', recordType).set('vocabulary', vocabulary).set('includeFields', _immutable.default.List()).set('csid', _immutable.default.List(selectedItems.keySeq()));
    this.setState({
      invocationDescriptor: nextInvocationDescriptor
    });
  }
  renderButtonBar() {
    const {
      onCancelButtonClick
    } = this.props;
    return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_CancelButton.default, {
      label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.cancel),
      onClick: onCancelButtonClick
    }), /*#__PURE__*/_react.default.createElement(_ExportButton.default, {
      label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.export),
      onClick: this.handleExportButtonClick
    }));
  }
  render() {
    const {
      config,
      isOpen,
      recordType,
      onCancelButtonClick,
      onCloseButtonClick
    } = this.props;
    const {
      invocationDescriptor
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_cspaceLayout.Modal, {
      isOpen: isOpen,
      title: /*#__PURE__*/_react.default.createElement("h1", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.title)),
      renderButtonBar: this.renderButtonBar,
      onCancelButtonClick: onCancelButtonClick,
      onCloseButtonClick: onCloseButtonClick
    }, /*#__PURE__*/_react.default.createElement(_ExportFieldEditorContainer.default, {
      config: config,
      recordType: recordType,
      includeFields: invocationDescriptor.get('includeFields'),
      onIncludeFieldsAddInstance: this.handleIncludeFieldsAddInstance,
      onIncludeFieldsCommit: this.handleIncludeFieldsCommit,
      onIncludeFieldsMoveInstance: this.handleIncludeFieldsMoveInstance,
      onIncludeFieldsRemoveInstance: this.handleIncludeFieldsRemoveInstance
    }));
  }
}
exports.default = ExportModal;
ExportModal.propTypes = propTypes;
ExportModal.defaultProps = defaultProps;