"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

var _Icon = _interopRequireDefault(require("../Icon"));

var _constants = require("../../constants");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
const DEFAULT_ICONS = ['clock', 'emoticon-smile', 'rocket', 'sand-clock', 'sleep', 'web', 'star'];

const useIcon = (icons, initialCursor, isSync) => {
  const targetRef = (0, _react.useRef)(null);
  const [cursor, setCursor] = (0, _react.useState)(initialCursor);
  (0, _react.useEffect)(() => {
    const target = targetRef.current;

    const listener = () => {
      if (isSync()) {
        setCursor(c => (c + 2) % icons.length);
      }
    };

    if (target) {
      target.addEventListener('animationiteration', listener);
    }

    return () => {
      if (target) {
        target.removeEventListener('animationiteration', listener);
      }
    }; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [icons]);
  return {
    name: icons[cursor],
    targetRef
  };
};

const IconsLoader = ({
  baseClassName = `${_constants.CLS_PREFIX}icons-loader`,
  className,
  icons,
  ...props
}) => {
  const syncRef = (0, _react.useRef)(0);
  icons = icons && icons.length > 0 ? icons : DEFAULT_ICONS;
  const icon1 = useIcon(icons, 0, () => {
    if (syncRef.current === 0) {
      syncRef.current += 1;
      return true;
    }

    return false;
  });
  const icon2 = useIcon(icons, 1, () => {
    if (syncRef.current === 1) {
      syncRef.current -= 1;
      return true;
    }

    return false;
  });
  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    className: (0, _classnames.default)(baseClassName, className)
  }, props), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(`${baseClassName}__icon`, `${baseClassName}__icon--1`),
    ref: icon1.targetRef
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
    name: icon1.name,
    size: "64"
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(`${baseClassName}__icon`, `${baseClassName}__icon--2`),
    ref: icon2.targetRef
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
    name: icon2.name,
    size: "64"
  })));
};

var _default = IconsLoader;
exports.default = _default;