import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import { CounterText } from '../CounterText';
export var StoreCounterText =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(StoreCounterText, _React$Component);

  function StoreCounterText(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;
    _this.state = {
      counter: _this.props.store.getFilteredCount()
    };

    _this.onStoreChange = function () {
      if (_this.unmounted) {
        return;
      }

      _this.setState({
        counter: _this.props.store.getFilteredCount()
      });
    };

    return _this;
  }

  var _proto = StoreCounterText.prototype;

  _proto.componentDidMount = function componentDidMount() {
    this.props.store.on('change', this.onStoreChange, this);
  };

  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    this.props.store.off('change', this.onStoreChange);
    nextProps.store.on('change', this.onStoreChange, this);
  };

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

  _proto.render = function render() {
    return React.createElement(CounterText, _extends({}, this.props, {
      counter: this.state.counter
    }));
  };

  return StoreCounterText;
}(React.Component);

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