var _excluded = ["duration", "from", "to", "suffix", "autoPlay"];

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { forwardRef, useRef, useImperativeHandle, useEffect } from 'react';
import { Counter as Instance } from './instance.js';
var Counter = forwardRef(function (_ref, ref) {
  var _ref$duration = _ref.duration,
      duration = _ref$duration === void 0 ? 5000 : _ref$duration,
      _ref$from = _ref.from,
      from = _ref$from === void 0 ? 0 : _ref$from,
      _ref$to = _ref.to,
      to = _ref$to === void 0 ? 10 : _ref$to,
      _ref$suffix = _ref.suffix,
      suffix = _ref$suffix === void 0 ? '' : _ref$suffix,
      _ref$autoPlay = _ref.autoPlay,
      autoPlay = _ref$autoPlay === void 0 ? true : _ref$autoPlay,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

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

  function initInstance() {
    instance.current = new Instance(rootRef.current);

    if (autoPlay) {
      instance.current.play();
    }

    rootRef.current.instance = instance;
  }

  return /*#__PURE__*/React.createElement("span", _extends({
    ref: rootRef
  }, others, {
    className: "counter" + (others.className ? ' ' + others.className : ''),
    "data-duration": duration,
    "data-from": from,
    "data-to": to,
    "data-suffix": suffix
  }), "1");
});
export default Counter;