import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["style", "colorIndex", "animation"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

import * as React from 'react';
import cx from 'classnames';
import { useAnimation } from './animation';
import { useTimeout } from './timeout';
import { shapeAnimationMap, shapeColorMap, variants } from './presets';
import useReducedMotion from '../utils/useReducedMotion';
import Particle from './Particle';
var Sparks = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var children = _ref.children,
      className = _ref.className,
      style = _ref.style,
      _ref$shape = _ref.shape,
      shape = _ref$shape === void 0 ? 'spark' : _ref$shape,
      _ref$variant = _ref.variant,
      variant = _ref$variant === void 0 ? 'l' : _ref$variant,
      _ref$active = _ref.active,
      active = _ref$active === void 0 ? false : _ref$active,
      _ref$duration = _ref.duration,
      duration = _ref$duration === void 0 ? 6000 : _ref$duration,
      _ref$delay = _ref.delay,
      delay = _ref$delay === void 0 ? 0 : _ref$delay,
      _ref$iterationDelay = _ref.iterationDelay,
      iterationDelay = _ref$iterationDelay === void 0 ? 500 : _ref$iterationDelay,
      _ref$iterationCount = _ref.iterationCount,
      iterationCount = _ref$iterationCount === void 0 ? 3 : _ref$iterationCount,
      colors = _ref.colors,
      placement = _ref.placement,
      display = _ref.display,
      width = _ref.width,
      height = _ref.height;
  var iteration = React.useRef(0);
  var hasReduceMotion = useReducedMotion();
  var animationConfig = shapeAnimationMap[shape][hasReduceMotion ? 'reduced' : 'default'];

  var _useAnimation = useAnimation(animationConfig),
      register = _useAnimation.register,
      phase = _useAnimation.phase,
      setPhase = _useAnimation.setPhase;

  var _React$useState = React.useState(null),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      timeoutDelay = _React$useState2[0],
      setTimeoutDelay = _React$useState2[1];

  var restartTimeout = useTimeout(function () {
    if (!active) {
      return;
    }

    if (phase === 'entry') {
      setPhase('exit');
    } else if (iteration.current < iterationCount) {
      setPhase('entry');
    }
  }, timeoutDelay);
  React.useEffect(function () {
    if (active) {
      setPhase('initial');
    } else {
      setPhase('exit');
    }
  }, [active, setPhase]);
  React.useEffect(function () {
    if (phase === 'initial' && active) {
      iteration.current = 0;
      restartTimeout();
      setTimeoutDelay(delay);
    }

    if (phase === 'entry' && active) {
      iteration.current++;

      if (duration !== Infinity) {
        restartTimeout();
        setTimeoutDelay(duration);
      }
    }

    if (phase === 'finished') {
      restartTimeout();

      if (iterationDelay !== Infinity) {
        setTimeoutDelay(iterationDelay);
      }
    }
  }, [phase, delay, iterationDelay, active, duration, iterationCount, restartTimeout]);
  var shapeColor = shapeColorMap[shape];
  var componnetCssVariables = {
    '--sparks-display': display,
    '--sparks-width': width,
    '--sparks-height': height
  };
  var canvasCssVariables = {
    '--sparks-inset': placement
  };
  return /*#__PURE__*/React.createElement("div", {
    className: cx('sg-sparks', className, {
      'sg-sparks--s': variant === 's'
    }),
    style: _objectSpread(_objectSpread({}, style), componnetCssVariables)
  }, children, /*#__PURE__*/React.createElement("div", {
    className: "sg-sparks__canvas",
    ref: ref,
    "aria-hidden": true,
    role: "img",
    "aria-label": "",
    style: canvasCssVariables
  }, variants[variant].map(function (_ref2, index) {
    var style = _ref2.style,
        colorIndex = _ref2.colorIndex,
        animation = _ref2.animation,
        particle = _objectWithoutProperties(_ref2, _excluded);

    return /*#__PURE__*/React.createElement(Particle, _extends({
      key: index,
      shape: shape,
      style: _objectSpread(_objectSpread({}, style), {}, {
        color: (colors === null || colors === void 0 ? void 0 : colors[colorIndex]) || shapeColor[colorIndex]
      })
    }, particle, register(animation)));
  })));
});
export default Sparks;