"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _animation = require("./animation");

var _timeout = require("./timeout");

var _presets = require("./presets");

var _useReducedMotion = _interopRequireDefault(require("../utils/useReducedMotion"));

var _Particle = _interopRequireDefault(require("./Particle"));

var _excluded = ["style", "colorIndex", "animation"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

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

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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 = (0, _useReducedMotion.default)();
  var animationConfig = _presets.shapeAnimationMap[shape][hasReduceMotion ? 'reduced' : 'default'];

  var _useAnimation = (0, _animation.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 = (0, _timeout.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 = _presets.shapeColorMap[shape];
  var componnetCssVariables = {
    '--sparks-display': display,
    '--sparks-width': width,
    '--sparks-height': height
  };
  var canvasCssVariables = {
    '--sparks-inset': placement
  };
  return /*#__PURE__*/React.createElement("div", {
    className: (0, _classnames.default)('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
  }, _presets.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.default, _extends({
      key: index,
      shape: shape,
      style: _objectSpread(_objectSpread({}, style), {}, {
        color: (colors === null || colors === void 0 ? void 0 : colors[colorIndex]) || shapeColor[colorIndex]
      })
    }, particle, register(animation)));
  })));
});
var _default = Sparks;
exports.default = _default;