"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.ConnectedTermPickerInput = void 0;

var _reactRedux = require("react-redux");

var _vocabulary = require("../../actions/vocabulary");

var _reducers = require("../../reducers");

var _TermPickerInput = _interopRequireDefault(require("../../components/record/TermPickerInput"));

var _withCsid = _interopRequireDefault(require("../../enhancers/withCsid"));

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

const mapStateToProps = (state, ownProps) => {
  const {
    csid,
    transformTerms,
    source
  } = ownProps;
  const vocabulary = (0, _reducers.getVocabulary)(state, source);
  let items = vocabulary ? vocabulary.items : null;

  if (transformTerms && items) {
    const recordData = (0, _reducers.getRecordData)(state, csid);
    items = transformTerms({
      recordData
    }, items);
  }

  return {
    perms: (0, _reducers.getUserPerms)(state),
    terms: items
  };
};

const mapDispatchToProps = {
  readTerms: _vocabulary.readVocabularyItems
};

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const {
    csid,
    transformTerms,
    ...remainingOwnProps
  } = ownProps;
  return { ...remainingOwnProps,
    ...stateProps,
    ...dispatchProps
  };
};

const ConnectedTermPickerInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(_TermPickerInput.default);
exports.ConnectedTermPickerInput = ConnectedTermPickerInput;
const WithCsidTermPickerInput = (0, _withCsid.default)(ConnectedTermPickerInput);
WithCsidTermPickerInput.propTypes = _TermPickerInput.default.propTypes;
var _default = WithCsidTermPickerInput;
exports.default = _default;