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

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

var _get2 = _interopRequireDefault(_get);

var _searchOperators = require('../../constants/searchOperators');

var _VocabularyUsedByPanelContainer = require('../../containers/admin/VocabularyUsedByPanelContainer');

var _VocabularyUsedByPanelContainer2 = _interopRequireDefault(_VocabularyUsedByPanelContainer);

var _RecordEditorContainer = require('../../containers/record/RecordEditorContainer');

var _RecordEditorContainer2 = _interopRequireDefault(_RecordEditorContainer);

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

var _SearchPanelContainer2 = _interopRequireDefault(_SearchPanelContainer);

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

var _VocabularySearchBar = require('../admin/VocabularySearchBar');

var _VocabularySearchBar2 = _interopRequireDefault(_VocabularySearchBar);

var _AdminTab = require('../../../styles/cspace-ui/AdminTab.css');

var _AdminTab2 = _interopRequireDefault(_AdminTab);

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

/* global window */

const propTypes = {
  history: _propTypes2.default.object,
  match: _propTypes2.default.object,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  filterDelay: _propTypes2.default.number,
  setAdminTab: _propTypes2.default.func
};

const defaultProps = {
  filterDelay: 500
};

const contextTypes = {
  config: _propTypes2.default.object.isRequired
};

const recordType = 'vocabulary';

const getSearchDescriptor = () => _immutable2.default.fromJS({
  recordType,
  searchQuery: {
    size: 20
  }
});

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

    this.handleItemClick = this.handleItemClick.bind(this);
    this.handleRecordSaved = this.handleRecordSaved.bind(this);
    this.handleSearchDescriptorChange = this.handleSearchDescriptorChange.bind(this);
    this.handleSearchBarChange = this.handleSearchBarChange.bind(this);
    this.renderSearchBar = this.renderSearchBar.bind(this);

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

  componentDidMount() {
    const setAdminTab = this.props.setAdminTab;


    if (setAdminTab) {
      setAdminTab(recordType);
    }
  }

  filter(value) {
    const searchDescriptor = this.state.searchDescriptor;


    const searchQuery = searchDescriptor.get('searchQuery');

    let updatedSearchQuery;

    if (value) {
      updatedSearchQuery = searchQuery.set('as', _immutable2.default.Map({
        value,
        op: _searchOperators.OP_CONTAIN,
        path: 'ns2:vocabularies_common/displayName'
      }));
    } else {
      updatedSearchQuery = searchQuery.delete('as');
    }

    updatedSearchQuery = updatedSearchQuery.set('p', 0);

    this.setState({
      filterValue: value,
      searchDescriptor: searchDescriptor.set('searchQuery', updatedSearchQuery)
    });
  }

  handleItemClick(item) {
    var _props = this.props;
    const history = _props.history,
          perms = _props.perms;


    if ((0, _permissionHelpers.canRead)(recordType, perms)) {
      const csid = item.get('csid');

      history.replace(`/admin/${recordType}/${csid}`);
    }

    // Prevent the default action.

    return false;
  }

  handleRecordSaved() {
    const searchDescriptor = this.state.searchDescriptor;


    this.setState({
      searchDescriptor: searchDescriptor.set('seqId', new Date().toISOString())
    });
  }

  handleSearchBarChange(value) {
    if (this.filterTimer) {
      window.clearTimeout(this.filterTimer);

      this.filterTimer = null;
    }

    if (value) {
      const filterDelay = this.props.filterDelay;


      this.filterTimer = window.setTimeout(() => {
        this.filter(value);
        this.filterTimer = null;
      }, filterDelay);
    } else {
      this.filter(value);
    }
  }

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

  renderSearchBar() {
    const filterValue = this.state.filterValue;


    return _react2.default.createElement(_VocabularySearchBar2.default, { value: filterValue, onChange: this.handleSearchBarChange });
  }

  render() {
    const config = this.context.config;
    var _props2 = this.props;
    const history = _props2.history,
          match = _props2.match,
          perms = _props2.perms;
    var _state = this.state;
    const filterValue = _state.filterValue,
          searchDescriptor = _state.searchDescriptor;
    const csid = match.params.csid;


    const normalizedCsid = csid === 'new' ? '' : csid;
    const recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);

    const title = _react2.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.collectionName);

    let recordEditor;
    let usedBy;

    if (typeof normalizedCsid !== 'undefined' && normalizedCsid !== null) {
      // Don't allow creating or deleting.

      let restrictedPerms = perms;

      restrictedPerms = (0, _permissionHelpers.disallowCreate)(recordType, restrictedPerms);
      restrictedPerms = (0, _permissionHelpers.disallowDelete)(recordType, restrictedPerms);
      restrictedPerms = (0, _permissionHelpers.disallowSoftDelete)(recordType, restrictedPerms);

      recordEditor = _react2.default.createElement(_RecordEditorContainer2.default, {
        config: config,
        csid: normalizedCsid,
        recordType: recordType,
        perms: restrictedPerms,
        onRecordSaved: this.handleRecordSaved
      });

      if (normalizedCsid) {
        usedBy = _react2.default.createElement(_VocabularyUsedByPanelContainer2.default, {
          config: config,
          csid: normalizedCsid
        });
      }
    }

    return _react2.default.createElement(
      'div',
      { className: _AdminTab2.default.common },
      _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_SearchPanelContainer2.default, {
          config: config,
          history: history,
          isFiltered: !!filterValue,
          linkItems: false,
          name: 'vocabularyPage',
          searchDescriptor: searchDescriptor,
          title: title,
          recordType: recordType,
          showSearchButton: false,
          renderTableHeader: this.renderSearchBar,
          onItemClick: this.handleItemClick,
          onSearchDescriptorChange: this.handleSearchDescriptorChange
        })
      ),
      _react2.default.createElement(
        'div',
        null,
        recordEditor,
        usedBy
      )
    );
  }
}

exports.default = VocabularyPage;
VocabularyPage.propTypes = propTypes;
VocabularyPage.defaultProps = defaultProps;
VocabularyPage.contextTypes = contextTypes;