"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.List = void 0;

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _utils = require("../utils");

var _CommonList2 = require("../common/CommonList");

var _LocalStorage = require("../../../data/LocalStorage");

var List =
/*#__PURE__*/
function (_CommonList) {
  (0, _inheritsLoose2.default)(List, _CommonList);

  function List(props, context) {
    var _this;

    _this = _CommonList.call(this, props, context) || this;
    _this.renderItem = _this.renderItem.bind((0, _assertThisInitialized2.default)(_this));
    _this.onStoreChange = _this.onStoreChange.bind((0, _assertThisInitialized2.default)(_this));
    _this.state = {
      rows: _this.props.store.getData()
    };
    _this._context = {
      listStore: _this.props.store,
      localStorage: new _LocalStorage.LocalStorage(_this.props.store.stateName || 'List')
    };
    return _this;
  }

  var _proto = List.prototype;

  _proto.getChildContext = function getChildContext() {
    return this._context;
  };

  _proto.componentDidMount = function componentDidMount() {
    this.props.store.onChange(this.onStoreChange);
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    this.unmounted = true;
    this.props.store.off('change', this.onStoreChange);
  };

  _proto.onStoreChange = function onStoreChange(data) {
    if (this.unmounted) {
      return;
    }

    this.setState({
      rows: data
    });
  };

  _proto.highlight = function highlight(text) {
    return (0, _utils.highlight)(this.props.store.textFilterPattern, text);
  };

  _proto.getDetails = function getDetails() {
    return {
      sortingDirection: this.props.store.getSortingDirection(),
      highlight: this.highlight.bind(this)
    };
  };

  _proto.hasItems = function hasItems() {
    return this.props.store.getFilteredCount() > 0;
  };

  _proto.isEmptyList = function isEmptyList() {
    return this.props.store.isLoaded() && !this.props.store.hasFilters() && !this.props.store.getTotalCount();
  };

  _proto.isEmptyListMessageVisible = function isEmptyListMessageVisible() {
    return this.props.store.isLoaded() && !this.hasItems();
  };

  _proto.renderItems = function renderItems() {
    return this.state.rows.map(this.renderItem);
  };

  return List;
}(_CommonList2.CommonList);

exports.List = List;
(0, _defineProperty2.default)(List, "childContextTypes", {
  listStore: _propTypes.default.object,
  localStorage: _propTypes.default.instanceOf(_LocalStorage.LocalStorage)
});
(0, _defineProperty2.default)(List, "propTypes", Object.assign({}, _CommonList2.CommonList.propTypes, {
  store: _propTypes.default.object.isRequired
}));