'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var ReactDOM = require('react-dom');
var reactDom = require('@floating-ui/react-dom');
var index = require('./utils/index.js');
var Tour_styles = require('./Tour.styles.js');
var base = require('./variatoins/_view/base.js');
var base$1 = require('./variatoins/_size/base.js');
var Tour_tokens = require('./Tour.tokens.js');
var canUseDOM = require('../../utils/canUseDOM.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var _Mask;
var _excluded = ["steps", "current", "defaultCurrent", "open", "defaultOpen", "onClose", "withOverlay", "overlayColor", "zIndex", "view", "size", "offset", "highlightOffset", "renderStep"];
var tourRoot = function tourRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (_ref, outerRef) {
    var _highlightRect$left, _highlightRect$top, _highlightRect$width, _highlightRect$height;
    var steps = _ref.steps,
      current = _ref.current,
      _ref$defaultCurrent = _ref.defaultCurrent,
      defaultCurrent = _ref$defaultCurrent === void 0 ? 0 : _ref$defaultCurrent,
      open = _ref.open,
      _ref$defaultOpen = _ref.defaultOpen,
      defaultOpen = _ref$defaultOpen === void 0 ? false : _ref$defaultOpen,
      onClose = _ref.onClose,
      _ref$withOverlay = _ref.withOverlay,
      withOverlay = _ref$withOverlay === void 0 ? true : _ref$withOverlay,
      overlayColor = _ref.overlayColor,
      _ref$zIndex = _ref.zIndex,
      zIndex = _ref$zIndex === void 0 ? 9000 : _ref$zIndex,
      view = _ref.view,
      size = _ref.size,
      _ref$offset = _ref.offset,
      offsetProp = _ref$offset === void 0 ? [12, 12] : _ref$offset,
      _ref$highlightOffset = _ref.highlightOffset,
      highlightOffset = _ref$highlightOffset === void 0 ? 4 : _ref$highlightOffset,
      renderStep = _ref.renderStep,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var TOUR_PORTAL_ID = "plasma-tour-portal-".concat(Date.now());
    var isCurrentControlled = typeof current === 'number';
    var isOpenControlled = typeof open === 'boolean';
    var _useState = React.useState(defaultCurrent),
      _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
      innerCurrent = _useState2[0],
      setInnerCurrent = _useState2[1];
    var _useState3 = React.useState(defaultOpen),
      _useState4 = _rollupPluginBabelHelpers.slicedToArray(_useState3, 2),
      innerOpen = _useState4[0],
      setInnerOpen = _useState4[1];
    var _useState5 = React.useState(null),
      _useState6 = _rollupPluginBabelHelpers.slicedToArray(_useState5, 2),
      highlightRect = _useState6[0],
      setHighlightRect = _useState6[1];
    var _useState7 = React.useState(null),
      _useState8 = _rollupPluginBabelHelpers.slicedToArray(_useState7, 2),
      highlightBorderRadius = _useState8[0],
      setHighlightBorderRadius = _useState8[1];
    var _useState9 = React.useState(null),
      _useState10 = _rollupPluginBabelHelpers.slicedToArray(_useState9, 2),
      targetElement = _useState10[0],
      setTargetElement = _useState10[1];
    var portalRef = React.useRef(null);
    var active = isCurrentControlled ? current : innerCurrent;
    var isOpen = isOpenControlled ? open : innerOpen;
    var currentStep = steps[active];
    var rawPlacement = currentStep === null || currentStep === void 0 ? void 0 : currentStep.placement;
    var placement = (Array.isArray(rawPlacement) ? rawPlacement[0] : rawPlacement) || 'bottom';
    var middleware = [reactDom.offset(offsetProp[0]), reactDom.flip({
      fallbackPlacements: Array.isArray(currentStep === null || currentStep === void 0 ? void 0 : currentStep.placement) ? currentStep.placement : undefined
    }), reactDom.shift({
      padding: offsetProp[1]
    })];
    var _useFloating = reactDom.useFloating({
        placement: placement,
        middleware: middleware,
        whileElementsMounted: reactDom.autoUpdate
      }),
      refs = _useFloating.refs,
      floatingStyles = _useFloating.floatingStyles,
      calculatedPlacement = _useFloating.placement;
    var handleSetOpen = function handleSetOpen(value) {
      if (!isOpenControlled) {
        setInnerOpen(value);
      }
      if (!value) {
        onClose === null || onClose === void 0 || onClose();
        setInnerCurrent(0);
        setHighlightRect(null);
        setTargetElement(null);
      }
    };
    var handleClose = function handleClose() {
      handleSetOpen(false);
    };
    var updateHighlight = function updateHighlight() {
      if (!isOpen || !currentStep) {
        return;
      }
      var rootItem = index.findRootElement(currentStep.target);
      var highlightElement = index.getHTMLElement(rootItem);
      if (highlightElement) {
        var _currentStep$borderRa;
        var rect = highlightElement.getBoundingClientRect();
        setHighlightRect(rect);
        setTargetElement(highlightElement);
        var borderRadius = currentStep.borderRadius === 'auto' || currentStep.borderRadius === undefined || currentStep.borderRadius === null ? index.getIncreasedRadius(rootItem, highlightOffset) : (_currentStep$borderRa = currentStep === null || currentStep === void 0 ? void 0 : currentStep.borderRadius) !== null && _currentStep$borderRa !== void 0 ? _currentStep$borderRa : '0px';
        setHighlightBorderRadius(borderRadius);
      } else {
        setHighlightRect(null);
        setTargetElement(null);
      }
    };
    React.useEffect(function () {
      refs.setReference(targetElement);
    }, [refs, targetElement]);
    React.useEffect(function () {
      if (!canUseDOM.canUseDOM) {
        return;
      }
      var portal = document.getElementById(TOUR_PORTAL_ID);
      if (!portal) {
        portal = document.createElement('div');
        portal.setAttribute('id', TOUR_PORTAL_ID);
        document.body.appendChild(portal);
      }
      portalRef.current = portal;
      return function () {
        if (portal) {
          portal.remove();
        }
      };
    }, []);
    React.useEffect(function () {
      updateHighlight();
      window.addEventListener('resize', updateHighlight);
      window.addEventListener('scroll', updateHighlight, true);
      return function () {
        window.removeEventListener('resize', updateHighlight);
        window.removeEventListener('scroll', updateHighlight, true);
      };
    }, [currentStep]);
    React.useEffect(function () {
      if (isOpen && targetElement) {
        targetElement.scrollIntoView({
          block: 'center',
          inline: 'center',
          behavior: 'smooth'
        });
      }
    }, [isOpen, active, targetElement]);
    React.useEffect(function () {
      var handleKeyDown = function handleKeyDown(e) {
        if (e.key === 'Escape') {
          handleClose();
        }
      };
      if (isOpen) {
        window.addEventListener('keydown', handleKeyDown);
      }
      return function () {
        window.removeEventListener('keydown', handleKeyDown);
      };
    }, [isOpen, handleClose]);
    if (!currentStep || !canUseDOM.canUseDOM || !portalRef.current) {
      return null;
    }
    var tourContent = /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      renderStep: renderStep,
      steps: steps,
      ref: outerRef,
      view: view,
      size: size
    }, rest), isOpen && withOverlay && /*#__PURE__*/React__default.default.createElement(Tour_styles.MaskContainer, {
      className: Tour_tokens.classes.mask,
      zIndex: zIndex
    }, _Mask || (_Mask = /*#__PURE__*/React__default.default.createElement(Tour_styles.Mask, null)), Boolean(highlightRect) && /*#__PURE__*/React__default.default.createElement(Tour_styles.Highlight, {
      overlayColor: overlayColor,
      borderRadius: highlightBorderRadius,
      style: {
        left: ((_highlightRect$left = highlightRect === null || highlightRect === void 0 ? void 0 : highlightRect.left) !== null && _highlightRect$left !== void 0 ? _highlightRect$left : 0) - highlightOffset,
        top: ((_highlightRect$top = highlightRect === null || highlightRect === void 0 ? void 0 : highlightRect.top) !== null && _highlightRect$top !== void 0 ? _highlightRect$top : 0) - highlightOffset,
        width: ((_highlightRect$width = highlightRect === null || highlightRect === void 0 ? void 0 : highlightRect.width) !== null && _highlightRect$width !== void 0 ? _highlightRect$width : 0) + highlightOffset * 2,
        height: ((_highlightRect$height = highlightRect === null || highlightRect === void 0 ? void 0 : highlightRect.height) !== null && _highlightRect$height !== void 0 ? _highlightRect$height : 0) + highlightOffset * 2
      }
    })), isOpen && Boolean(targetElement) && /*#__PURE__*/React__default.default.createElement(Tour_styles.TourPortal, {
      ref: refs.setFloating,
      style: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, floatingStyles), {}, {
        zIndex: Number(zIndex)
      }),
      "data-placement": calculatedPlacement
    }, renderStep && renderStep(active, steps.length, active === steps.length - 1, steps[active])));
    return /*#__PURE__*/ReactDOM.createPortal(tourContent, portalRef.current);
  });
};
var tourConfig = {
  name: 'Tour',
  tag: 'div',
  layout: tourRoot,
  base: '',
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    }
  },
  defaults: {
    view: 'default',
    size: 'm'
  }
};

exports.tourConfig = tourConfig;
exports.tourRoot = tourRoot;
//# sourceMappingURL=Tour.js.map
