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; };

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; }

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 === 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 = useRef(null);
  useImperativeHandle(ref, function () {
    return refEl.current;
  });

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

  function initInstance() {
    instance.current = new Instance(refEl.current);
    if (autoPlay) {
      instance.current.play();
    }
    refEl.current.instance = instance;
  }
  return React.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'
  );
});

export default Counter;