import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import PropTypes from 'prop-types';
import { highlight as _highlight } from '../utils';
import { CommonList } from '../common/CommonList';
import { LocalStorage } from '../../../data/LocalStorage';
export var List =
/*#__PURE__*/
function (_CommonList) {
  _inheritsLoose(List, _CommonList);

  function List(props, context) {
    var _this;

    _this = _CommonList.call(this, props, context) || this;
    _this.renderItem = _this.renderItem.bind(_assertThisInitialized(_this));
    _this.onStoreChange = _this.onStoreChange.bind(_assertThisInitialized(_this));
    _this.state = {
      rows: _this.props.store.getData()
    };
    _this._context = {
      listStore: _this.props.store,
      localStorage: new 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 _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;
}(CommonList);

_defineProperty(List, "childContextTypes", {
  listStore: PropTypes.object,
  localStorage: PropTypes.instanceOf(LocalStorage)
});

_defineProperty(List, "propTypes", Object.assign({}, CommonList.propTypes, {
  store: PropTypes.object.isRequired
}));