"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 _utils = require("../utils");

var _OnDarkContext = _interopRequireDefault(require("../OnDarkContext"));

var _Layer = _interopRequireDefault(require("../Layer"));

var _SpotPulsar = _interopRequireDefault(require("./SpotPulsar"));

var _SpotPopup = _interopRequireDefault(require("./SpotPopup"));

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 isEqual = (a, b) => a.top === b.top && a.left === b.left && a.right === b.right && a.bottom === b.bottom && a.width === b.width && a.height === b.height;
/**
 * The component can be used to attract the user's attention to a specific element on the page.
 * @since 3.0.1
 */


const Spot = ({
  selector,
  popup,
  meta,
  placement,
  ...props
}) => {
  const [rect, setRect] = (0, _react.useState)();
  const pulsarRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    let lastRect;
    let lastPageYOffset = window.pageYOffset;
    let isMoving = false;
    let needUpdate = false;
    let needScrollIntoView = true;

    const check = () => {
      const node = document.querySelector(selector);

      if (node) {
        const nextRect = node.getBoundingClientRect();

        if (!lastRect) {
          isMoving = true;
          lastRect = nextRect;
        } else if (!isEqual(nextRect, lastRect) || window.pageYOffset !== lastPageYOffset) {
          isMoving = true;
          lastPageYOffset = window.pageYOffset;
          lastRect = nextRect;
        } else if (isMoving) {
          isMoving = false;
          needUpdate = true;
        }

        if (needUpdate) {
          setRect(nextRect);
          needUpdate = false;

          if (needScrollIntoView) {
            needScrollIntoView = false;
            (0, _utils.scrollIntoView)(node, {
              scrollMode: 'if-needed'
            });
          }
        }
      } else {
        setRect(undefined);
      }
    };

    check();
    const retryId = window.setInterval(check, 125);
    return () => {
      window.clearInterval(retryId);
    };
  }, [selector]);

  if (!rect) {
    return null;
  }

  const pulsar = /*#__PURE__*/_react.default.createElement(_SpotPulsar.default, (0, _extends2.default)({
    rect: rect,
    placement: placement
  }, props, {
    ref: pulsarRef
  }));

  return /*#__PURE__*/_react.default.createElement(_OnDarkContext.default.Provider, {
    value: true
  }, /*#__PURE__*/_react.default.createElement(_Layer.default, null, popup ? /*#__PURE__*/_react.default.createElement(_SpotPopup.default, (0, _extends2.default)({
    pulsar: pulsar,
    pulsarRef: pulsarRef,
    meta: meta
  }, popup)) : /*#__PURE__*/_react.default.createElement("div", null, pulsar)));
};

var _default = Spot;
exports.default = _default;