'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _instance = require('./instance.js');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var Counter = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$duration = _ref.duration,
      duration = _ref$duration === undefined ? 5000 : _ref$duration,
      _ref$from = _ref.from,
      from = _ref$from === undefined ? 0 : _ref$from,
      _ref$to = _ref.to,
      to = _ref$to === undefined ? 10 : _ref$to,
      _ref$suffix = _ref.suffix,
      suffix = _ref$suffix === undefined ? '' : _ref$suffix,
      _ref$autoPlay = _ref.autoPlay,
      autoPlay = _ref$autoPlay === undefined ? true : _ref$autoPlay,
      others = _objectWithoutProperties(_ref, ['duration', 'from', 'to', 'suffix', 'autoPlay']);

  var refEl = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });

  var instance = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    if (instance.current) return;
    initInstance();
  }, []);

  function initInstance() {
    instance.current = new _instance.Counter(refEl.current);
    if (autoPlay) {
      instance.current.play();
    }
    refEl.current.instance = instance;
  }
  return _react2.default.createElement(
    'span',
    _extends({ ref: refEl }, others, { className: 'counter' + (others.className ? ' ' + others.className : ''), 'data-duration': duration, 'data-from': from, 'data-to': to, 'data-suffix': suffix }),
    '1'
  );
});

exports.default = Counter;
module.exports = exports['default'];