"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 _cspaceInput = require("cspace-input");
var _FieldInput = _interopRequireDefault(require("./input/FieldInput"));
var _ExportFieldEditor = _interopRequireDefault(require("../../../styles/cspace-ui/ExportFieldEditor.css"));
var _configHelpers = require("../../helpers/configHelpers");
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 propTypes = {
  config: _propTypes.default.shape({
    locale: _propTypes.default.string
  }).isRequired,
  includeFields: _propTypes.default.instanceOf(_immutable.default.List),
  recordType: _propTypes.default.string,
  buildRecordFieldOptionLists: _propTypes.default.func,
  deleteOptionList: _propTypes.default.func,
  onIncludeFieldsAddInstance: _propTypes.default.func,
  onIncludeFieldsCommit: _propTypes.default.func,
  onIncludeFieldsMoveInstance: _propTypes.default.func,
  onIncludeFieldsRemoveInstance: _propTypes.default.func
};
const {
  RepeatingInput
} = _cspaceInput.components;
const messages = (0, _reactIntl.defineMessages)({
  prompt: {
    "id": "exportFieldEditor.prompt",
    "defaultMessage": "Select fields to include."
  }
});
class ExportFieldEditor extends _react.Component {
  constructor(props) {
    super(props);
    this.handleIncludeFieldsAddInstance = this.handleIncludeFieldsAddInstance.bind(this);
    this.handleIncludeFieldsCommit = this.handleIncludeFieldsCommit.bind(this);
    this.handleIncludeFieldsMoveInstance = this.handleIncludeFieldsMoveInstance.bind(this);
    this.handleIncludeFieldsRemoveInstance = this.handleIncludeFieldsRemoveInstance.bind(this);
  }
  componentDidMount() {
    const {
      config,
      recordType,
      buildRecordFieldOptionLists
    } = this.props;
    if (buildRecordFieldOptionLists) {
      // Converter doesn't support importing struct date parts, so set the includeStructDateFields
      // arg to false.

      buildRecordFieldOptionLists(config, recordType, undefined, false);
    }
  }
  componentWillUnmount() {
    const {
      recordType,
      deleteOptionList
    } = this.props;
    if (deleteOptionList) {
      deleteOptionList((0, _configHelpers.getRecordFieldOptionListName)(recordType));
      deleteOptionList((0, _configHelpers.getRecordGroupOptionListName)(recordType));
    }
  }
  handleIncludeFieldsAddInstance(path, position) {
    const {
      onIncludeFieldsAddInstance
    } = this.props;
    if (onIncludeFieldsAddInstance) {
      onIncludeFieldsAddInstance(position);
    }
  }
  handleIncludeFieldsCommit(path, value) {
    const {
      onIncludeFieldsCommit
    } = this.props;
    if (onIncludeFieldsCommit) {
      onIncludeFieldsCommit(path[0], value);
    }
  }
  handleIncludeFieldsMoveInstance(path, newPosition) {
    const {
      onIncludeFieldsMoveInstance
    } = this.props;
    if (onIncludeFieldsMoveInstance) {
      onIncludeFieldsMoveInstance(path[1], newPosition);
    }
  }
  handleIncludeFieldsRemoveInstance(path) {
    const {
      onIncludeFieldsRemoveInstance
    } = this.props;
    if (onIncludeFieldsRemoveInstance) {
      onIncludeFieldsRemoveInstance(path[1]);
    }
  }
  render() {
    const {
      config,
      includeFields,
      recordType
    } = this.props;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _ExportFieldEditor.default.common
    }, /*#__PURE__*/_react.default.createElement("p", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.prompt)), /*#__PURE__*/_react.default.createElement(RepeatingInput, {
      name: "includeFields",
      value: includeFields,
      onAddInstance: this.handleIncludeFieldsAddInstance,
      onCommit: this.handleIncludeFieldsCommit,
      onMoveInstance: this.handleIncludeFieldsMoveInstance,
      onRemoveInstance: this.handleIncludeFieldsRemoveInstance
    }, /*#__PURE__*/_react.default.createElement(_FieldInput.default, {
      config: config,
      embedded: true,
      recordType: recordType
    })));
  }
}
exports.default = ExportFieldEditor;
ExportFieldEditor.propTypes = propTypes;