'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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 }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* global window */

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

var defaultProps = {
  filterDelay: 500
};

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

var recordType = 'vocabulary';

var getSearchDescriptor = function getSearchDescriptor() {
  return _immutable2.default.fromJS({
    recordType: recordType,
    searchQuery: {
      size: 20
    }
  });
};

var VocabularyPage = function (_Component) {
  _inherits(VocabularyPage, _Component);

  function VocabularyPage() {
    _classCallCheck(this, VocabularyPage);

    var _this = _possibleConstructorReturn(this, (VocabularyPage.__proto__ || Object.getPrototypeOf(VocabularyPage)).call(this));

    _this.handleItemClick = _this.handleItemClick.bind(_this);
    _this.handleSearchDescriptorChange = _this.handleSearchDescriptorChange.bind(_this);
    _this.handleSearchBarChange = _this.handleSearchBarChange.bind(_this);
    _this.renderSearchBar = _this.renderSearchBar.bind(_this);

    _this.state = {
      searchDescriptor: getSearchDescriptor()
    };
    return _this;
  }

  _createClass(VocabularyPage, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var setAdminTab = this.props.setAdminTab;


      if (setAdminTab) {
        setAdminTab(recordType);
      }
    }
  }, {
    key: 'filter',
    value: function filter(value) {
      var searchDescriptor = this.state.searchDescriptor;


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

      var updatedSearchQuery = void 0;

      if (value) {
        updatedSearchQuery = searchQuery.set('as', _immutable2.default.Map({
          value: 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)
      });
    }
  }, {
    key: 'handleItemClick',
    value: function handleItemClick(item) {
      var _props = this.props,
          history = _props.history,
          perms = _props.perms;


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

        history.replace('/admin/' + recordType + '/' + csid);
      }

      // Prevent the default action.

      return false;
    }
  }, {
    key: 'handleSearchBarChange',
    value: function handleSearchBarChange(value) {
      var _this2 = this;

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

        this.filterTimer = null;
      }

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


        this.filterTimer = window.setTimeout(function () {
          _this2.filter(value);
          _this2.filterTimer = null;
        }, filterDelay);
      } else {
        this.filter(value);
      }
    }
  }, {
    key: 'handleSearchDescriptorChange',
    value: function handleSearchDescriptorChange(searchDescriptor) {
      this.setState({
        searchDescriptor: searchDescriptor
      });
    }
  }, {
    key: 'renderSearchBar',
    value: function renderSearchBar() {
      var filterValue = this.state.filterValue;


      return _react2.default.createElement(_VocabularySearchBar2.default, { value: filterValue, onChange: this.handleSearchBarChange });
    }
  }, {
    key: 'render',
    value: function render() {
      var config = this.context.config;
      var _props2 = this.props,
          history = _props2.history,
          match = _props2.match,
          perms = _props2.perms;
      var _state = this.state,
          filterValue = _state.filterValue,
          searchDescriptor = _state.searchDescriptor;
      var csid = match.params.csid;


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

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

      var recordEditor = void 0;
      var usedBy = void 0;

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

        var 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
        });

        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
        )
      );
    }
  }]);

  return VocabularyPage;
}(_react.Component);

exports.default = VocabularyPage;


VocabularyPage.propTypes = propTypes;
VocabularyPage.defaultProps = defaultProps;
VocabularyPage.contextTypes = contextTypes;