'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 _reactIntl = require('react-intl');

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _cspaceInput = require('cspace-input');

var _cspaceLayout = require('cspace-layout');

var _Dock = require('../sections/Dock');

var _Dock2 = _interopRequireDefault(_Dock);

var _SearchButtonBar = require('./SearchButtonBar');

var _SearchButtonBar2 = _interopRequireDefault(_SearchButtonBar);

var _AdvancedSearchBuilder = require('./AdvancedSearchBuilder');

var _AdvancedSearchBuilder2 = _interopRequireDefault(_AdvancedSearchBuilder);

var _searchHelpers = require('../../helpers/searchHelpers');

var _PanelContainer = require('../../containers/layout/PanelContainer');

var _SearchForm = require('../../../styles/cspace-ui/SearchForm.css');

var _SearchForm2 = _interopRequireDefault(_SearchForm);

var _SearchFormRecordType = require('../../../styles/cspace-ui/SearchFormRecordType.css');

var _SearchFormRecordType2 = _interopRequireDefault(_SearchFormRecordType);

var _SearchFormVocab = require('../../../styles/cspace-ui/SearchFormVocab.css');

var _SearchFormVocab2 = _interopRequireDefault(_SearchFormVocab);

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

const Label = _cspaceInput.components.Label,
      LineInput = _cspaceInput.components.LineInput,
      RecordTypeInput = _cspaceInput.components.RecordTypeInput,
      VocabularyInput = _cspaceInput.components.VocabularyInput;


const messages = (0, _reactIntl.defineMessages)({
  recordType: {
    'id': 'searchForm.recordType',
    'defaultMessage': 'Find'
  },
  vocabulary: {
    'id': 'searchForm.vocabulary',
    'defaultMessage': 'in vocabulary'
  },
  keyword: {
    'id': 'searchForm.keyword',
    'defaultMessage': 'Keywords'
  },
  fullTextSearch: {
    'id': 'searchForm.fullTextSearch',
    'defaultMessage': 'Full Text Search'
  }
});

const propTypes = {
  config: _propTypes2.default.object,
  dockTop: _propTypes2.default.number,
  intl: _reactIntl.intlShape,
  keywordValue: _propTypes2.default.string,
  recordTypeValue: _propTypes2.default.string,
  vocabularyValue: _propTypes2.default.string,
  advancedSearchCondition: _propTypes2.default.object,
  preferredAdvancedSearchBooleanOp: _propTypes2.default.string,
  recordTypeInputReadOnly: _propTypes2.default.bool,
  recordTypeInputRootType: _propTypes2.default.string,
  recordTypeInputServiceTypes: _propTypes2.default.arrayOf(_propTypes2.default.string),
  showButtons: _propTypes2.default.bool,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  getAuthorityVocabCsid: _propTypes2.default.func,
  onAdvancedSearchConditionCommit: _propTypes2.default.func,
  onClearButtonClick: _propTypes2.default.func,
  onKeywordCommit: _propTypes2.default.func,
  onRecordTypeCommit: _propTypes2.default.func,
  onVocabularyCommit: _propTypes2.default.func,
  onSearch: _propTypes2.default.func
};

class SearchForm extends _react.Component {
  constructor() {
    super();

    this.formatRecordTypeLabel = this.formatRecordTypeLabel.bind(this);
    this.formatVocabularyLabel = this.formatVocabularyLabel.bind(this);
    this.handleFormSubmit = this.handleFormSubmit.bind(this);
    this.handleKeywordInputCommit = this.handleKeywordInputCommit.bind(this);
    this.handleRecordTypeDropdownCommit = this.handleRecordTypeDropdownCommit.bind(this);
    this.handleVocabularyDropdownCommit = this.handleVocabularyDropdownCommit.bind(this);
  }

  formatRecordTypeLabel(name, config) {
    const intl = this.props.intl;


    return intl.formatMessage(config.messages.record.collectionName) || name;
  }

  formatVocabularyLabel(name, config) {
    const intl = this.props.intl;


    return intl.formatMessage(config.messages.name) || name;
  }

  handleFormSubmit(event) {
    event.preventDefault();

    const onSearch = this.props.onSearch;


    if (onSearch) {
      onSearch();
    }
  }

  handleKeywordInputCommit(path, value) {
    const onKeywordCommit = this.props.onKeywordCommit;


    if (onKeywordCommit) {
      onKeywordCommit(value);
    }
  }

  handleRecordTypeDropdownCommit(path, value) {
    const onRecordTypeCommit = this.props.onRecordTypeCommit;


    if (onRecordTypeCommit) {
      onRecordTypeCommit(value);
    }
  }

  handleVocabularyDropdownCommit(path, value) {
    const onVocabularyCommit = this.props.onVocabularyCommit;


    if (onVocabularyCommit) {
      onVocabularyCommit(value);
    }
  }

  renderVocabularyInput(recordTypes) {
    var _props = this.props;
    const intl = _props.intl,
          recordTypeValue = _props.recordTypeValue,
          vocabularyValue = _props.vocabularyValue;


    if (!recordTypeValue || !recordTypes[recordTypeValue] || !recordTypes[recordTypeValue].vocabularies) {
      return null;
    }

    return _react2.default.createElement(
      'div',
      { className: _SearchFormVocab2.default.common },
      _react2.default.createElement(
        Label,
        null,
        intl.formatMessage(messages.vocabulary)
      ),
      _react2.default.createElement(VocabularyInput, {
        recordTypes: recordTypes,
        recordType: recordTypeValue,
        value: vocabularyValue,
        formatVocabularyLabel: this.formatVocabularyLabel,
        onCommit: this.handleVocabularyDropdownCommit
      })
    );
  }

  render() {
    var _props2 = this.props;
    const advancedSearchCondition = _props2.advancedSearchCondition,
          config = _props2.config,
          dockTop = _props2.dockTop,
          intl = _props2.intl,
          keywordValue = _props2.keywordValue,
          perms = _props2.perms,
          preferredAdvancedSearchBooleanOp = _props2.preferredAdvancedSearchBooleanOp,
          recordTypeValue = _props2.recordTypeValue,
          recordTypeInputReadOnly = _props2.recordTypeInputReadOnly,
          recordTypeInputRootType = _props2.recordTypeInputRootType,
          recordTypeInputServiceTypes = _props2.recordTypeInputServiceTypes,
          showButtons = _props2.showButtons,
          getAuthorityVocabCsid = _props2.getAuthorityVocabCsid,
          onAdvancedSearchConditionCommit = _props2.onAdvancedSearchConditionCommit,
          onClearButtonClick = _props2.onClearButtonClick;


    const fullTextPanelHeader = _react2.default.createElement(
      'h3',
      null,
      _react2.default.createElement(_reactIntl.FormattedMessage, messages.fullTextSearch)
    );

    // If showButtons is false, render the button bar anyway, but with height 0, so that the submit
    // button will exist on the page, invisibly. This allows pressing enter on fields to submit the
    // form.

    const topButtonBar = _react2.default.createElement(_SearchButtonBar2.default, { onClearButtonClick: onClearButtonClick });

    let header;

    if (showButtons) {
      header = _react2.default.createElement(
        _Dock2.default,
        { dockTop: dockTop, isSidebarOpen: false },
        topButtonBar
      );
    } else {
      header = _react2.default.createElement(
        'div',
        { style: { height: '0', overflow: 'hidden', margin: '0' } },
        topButtonBar
      );
    }

    let footer;

    if (showButtons) {
      footer = _react2.default.createElement(
        'footer',
        null,
        _react2.default.createElement(_SearchButtonBar2.default, { onClearButtonClick: onClearButtonClick })
      );
    }

    const searchableRecordTypes = (0, _searchHelpers.getSearchableRecordTypes)(getAuthorityVocabCsid, config, perms);

    return _react2.default.createElement(
      'form',
      { autoComplete: 'off', className: _SearchForm2.default.common, onSubmit: this.handleFormSubmit },
      header,
      _react2.default.createElement(
        _cspaceLayout.Panel,
        null,
        _react2.default.createElement(
          'div',
          { className: _SearchFormRecordType2.default.common },
          _react2.default.createElement(RecordTypeInput, {
            label: intl.formatMessage(messages.recordType),
            recordTypes: searchableRecordTypes,
            rootType: recordTypeInputRootType,
            serviceTypes: recordTypeInputServiceTypes,
            value: recordTypeValue,
            formatRecordTypeLabel: this.formatRecordTypeLabel,
            onCommit: this.handleRecordTypeDropdownCommit,
            readOnly: recordTypeInputReadOnly
          }),
          this.renderVocabularyInput(searchableRecordTypes)
        ),
        _react2.default.createElement(
          _PanelContainer.ConnectedPanel,
          {
            collapsible: true,
            header: fullTextPanelHeader,
            name: 'fullTextSearch',
            recordType: recordTypeValue
          },
          _react2.default.createElement(LineInput, {
            label: intl.formatMessage(messages.keyword),
            value: keywordValue,
            onCommit: this.handleKeywordInputCommit
          })
        ),
        _react2.default.createElement(_AdvancedSearchBuilder2.default, {
          condition: advancedSearchCondition,
          config: config,
          preferredBooleanOp: preferredAdvancedSearchBooleanOp,
          recordType: recordTypeValue,
          onConditionCommit: onAdvancedSearchConditionCommit
        })
      ),
      footer
    );
  }
}

exports.default = SearchForm;
SearchForm.propTypes = propTypes;