"use strict";

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

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

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

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

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

var _react = _interopRequireDefault(require("react"));

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

var _CounterText = require("../CounterText");

var StoreCounterText =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(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.default.createElement(_CounterText.CounterText, (0, _extends2.default)({}, this.props, {
      counter: this.state.counter
    }));
  };

  return StoreCounterText;
}(_react.default.Component);

exports.StoreCounterText = StoreCounterText;
(0, _defineProperty2.default)(StoreCounterText, "propTypes", Object.assign({}, _CounterText.CounterText.propTypes, {
  store: _propTypes.default.object.isRequired
}));