function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { usePopper } from 'react-popper';
import { BottomSheet, Button, Flexbox, Icon, Portal, Typography, useMedia } from '../..';
import { makeBackdropPath } from './utils';
import { useOnClickOutside, useWindowSize, useSafeLayoutEffect } from '../../common/Hooks';
import useScrollPosition from './hooks/useScrollPosition';
import BubbleArrow from './BubbleArrow';
import { OFFSET_AWAY_FROM_REFERENCE } from './Bubble/consts';
import { BACKDROP_PADDING } from './consts';
import { CloseButton, Content, FooterFlex, IconFlex, NavigationButtonsContainer, SVG, TitleWrapper, StyledBubble } from './CoachMarks.styled';
export var CoachMarks = function CoachMarks(_ref) {
  var barColor = _ref.barColor,
    _ref$bottomSheet = _ref.bottomSheet,
    bottomSheet = _ref$bottomSheet === void 0 ? false : _ref$bottomSheet,
    bottomSheetTitle = _ref.bottomSheetTitle,
    _ref$closeButton = _ref.closeButton,
    closeButton = _ref$closeButton === void 0 ? false : _ref$closeButton,
    _ref$closeOnClickOuts = _ref.closeOnClickOutside,
    closeOnClickOutside = _ref$closeOnClickOuts === void 0 ? true : _ref$closeOnClickOuts,
    _ref$closeText = _ref.closeText,
    closeText = _ref$closeText === void 0 ? 'Close' : _ref$closeText,
    _ref$doneText = _ref.doneText,
    doneText = _ref$doneText === void 0 ? 'Done' : _ref$doneText,
    _ref$feedbackWidgetMo = _ref.feedbackWidgetMode,
    feedbackWidgetMode = _ref$feedbackWidgetMo === void 0 ? false : _ref$feedbackWidgetMo,
    _ref$hideMultiStepInd = _ref.hideMultiStepIndicatorText,
    hideMultiStepIndicatorText = _ref$hideMultiStepInd === void 0 ? false : _ref$hideMultiStepInd,
    _ref$hidePreviousButt = _ref.hidePreviousButton,
    hidePreviousButton = _ref$hidePreviousButt === void 0 ? false : _ref$hidePreviousButt,
    _ref$multiStepIndicat = _ref.multiStepIndicatorText,
    multiStepIndicatorText = _ref$multiStepIndicat === void 0 ? 'of' : _ref$multiStepIndicat,
    _ref$nextText = _ref.nextText,
    nextTextFromProps = _ref$nextText === void 0 ? 'Next' : _ref$nextText,
    onClose = _ref.onClose,
    onDone = _ref.onDone,
    onNext = _ref.onNext,
    onPrev = _ref.onPrev,
    overrideStep = _ref.overrideStep,
    _ref$prevText = _ref.prevText,
    prevTextFromProps = _ref$prevText === void 0 ? 'Previous' : _ref$prevText,
    steps = _ref.steps;
  var _useState = useState(overrideStep !== null && overrideStep !== void 0 ? overrideStep : 0),
    _useState2 = _slicedToArray(_useState, 2),
    currentStep = _useState2[0],
    setCurrentStep = _useState2[1];
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    referenceElementRect = _useState4[0],
    setReferenceElementRect = _useState4[1];
  var _useState5 = useState(true),
    _useState6 = _slicedToArray(_useState5, 2),
    bottomSheetOpen = _useState6[0],
    setBottomSheetOpen = _useState6[1];
  useEffect(function () {
    if (overrideStep && overrideStep !== currentStep) {
      setCurrentStep(overrideStep);
    }
  }, [overrideStep, currentStep]);
  var _steps$currentStep = steps[currentStep],
    body = _steps$currentStep.body,
    content = _steps$currentStep.content,
    icon = _steps$currentStep.icon,
    _steps$currentStep$pl = _steps$currentStep.placement,
    placement = _steps$currentStep$pl === void 0 ? 'left' : _steps$currentStep$pl,
    title = _steps$currentStep.title,
    referenceElement = _steps$currentStep.referenceElement,
    isCircular = _steps$currentStep.isCircular,
    backdropPadding = _steps$currentStep.backdropPadding,
    px = _steps$currentStep.px,
    py = _steps$currentStep.py,
    _steps$currentStep$pr = _steps$currentStep.prevText,
    prevText = _steps$currentStep$pr === void 0 ? prevTextFromProps : _steps$currentStep$pr,
    _steps$currentStep$ne = _steps$currentStep.nextText,
    nextText = _steps$currentStep$ne === void 0 ? nextTextFromProps : _steps$currentStep$ne,
    _steps$currentStep$ne2 = _steps$currentStep.nextDisabled,
    nextDisabled = _steps$currentStep$ne2 === void 0 ? false : _steps$currentStep$ne2,
    _steps$currentStep$hi = _steps$currentStep.hideNextButton,
    hideNextButton = _steps$currentStep$hi === void 0 ? false : _steps$currentStep$hi,
    _steps$currentStep$hi2 = _steps$currentStep.hideDoneButton,
    hideDoneButton = _steps$currentStep$hi2 === void 0 ? false : _steps$currentStep$hi2;
  var _useState7 = useState(null),
    _useState8 = _slicedToArray(_useState7, 2),
    popperElement = _useState8[0],
    setPopperElement = _useState8[1];
  var _useState9 = useState(null),
    _useState10 = _slicedToArray(_useState9, 2),
    arrowElement = _useState10[0],
    setArrowElement = _useState10[1];
  var scrollPosition = useScrollPosition();
  var highlightBoxPadding = backdropPadding || BACKDROP_PADDING;
  var _usePopper = usePopper(referenceElement, popperElement, {
      placement: "".concat(placement, "-start"),
      modifiers: [{
        name: 'arrow',
        options: {
          element: arrowElement
        }
      }, {
        name: 'offset',
        options: {
          offset: [-highlightBoxPadding, OFFSET_AWAY_FROM_REFERENCE + Number(highlightBoxPadding)]
        }
      }]
    }),
    styles = _usePopper.styles,
    attributes = _usePopper.attributes;
  var internalCoachMarkRef = useRef(null);
  var windowSize = useWindowSize();
  var hasMultipleSteps = steps.length > 1;
  var hasPrevStep = currentStep > 0 && !hidePreviousButton;
  var hasNextStep = currentStep + 1 < steps.length;
  var path = referenceElementRect ? makeBackdropPath(referenceElementRect, Number(highlightBoxPadding), isCircular, px, py) : undefined;
  var isMobile = useMedia(function (t) {
    return t.media.lessThan(t.breakpoints.sm);
  });
  var shouldShowBottomSheet = bottomSheet && !feedbackWidgetMode && isMobile;
  useSafeLayoutEffect(function () {
    if (referenceElement) {
      setReferenceElementRect(referenceElement.getBoundingClientRect());
    }
  }, [referenceElement, windowSize, scrollPosition]);
  var handleStepBackwards = useCallback(function () {
    if (hasPrevStep) {
      setCurrentStep(currentStep - 1);
    }
    if (onPrev) {
      onPrev(currentStep);
    }
  }, [currentStep, hasPrevStep, onPrev]);
  var handleStepForward = useCallback(function () {
    if (hasNextStep) {
      setCurrentStep(currentStep + 1);
    }
    if (onNext) {
      onNext(currentStep);
    }
  }, [currentStep, hasNextStep, onNext]);
  var handleDone = useCallback(function () {
    if (onDone) {
      onDone();
    }
  }, [onDone]);
  var handleClose = function handleClose() {
    if (shouldShowBottomSheet) {
      setBottomSheetOpen(false);
      setTimeout(function () {
        if (typeof onClose === 'function') {
          onClose();
        }
      }, 1000);
    } else if (typeof onClose === 'function') {
      onClose();
    }
  };
  useOnClickOutside(internalCoachMarkRef, function () {
    if (closeOnClickOutside) {
      handleClose();
    }
  });
  if (typeof isMobile === 'undefined' || isMobile === null) {
    return null;
  }
  var getContent = React.createElement(Flexbox, {
    container: true,
    item: true,
    direction: "column",
    flex: "1",
    gap: 5,
    ref: internalCoachMarkRef
  }, React.createElement(Flexbox, {
    container: true,
    direction: "column",
    gap: 1
  }, icon && React.createElement(IconFlex, {
    item: true
  }, icon), title && React.createElement(Flexbox, {
    item: true
  }, React.createElement(TitleWrapper, {
    $hasIcon: Boolean(icon) || closeButton
  }, React.createElement(Typography, {
    as: "h2",
    type: "primary",
    weight: "bold"
  }, title))), content && React.createElement(Flexbox, {
    item: true
  }, React.createElement(Content, null, typeof content === 'string' ? React.createElement(Typography, {
    as: "p",
    type: "secondary",
    color: "inherit"
  }, content) : content))), React.createElement(FooterFlex, {
    container: true,
    item: true,
    alignItems: "baseline",
    gap: 5
  }, closeButton ? React.createElement(Flexbox, {
    item: true
  }, React.createElement(Button, {
    variant: "neutral",
    color: function color(t) {
      return t.color.link;
    },
    onClick: onClose
  }, closeText)) : hasMultipleSteps && !hideMultiStepIndicatorText && React.createElement(Flexbox, {
    item: true
  }, React.createElement(Typography, {
    type: "secondary",
    color: function color(t) {
      return t.color.bubbleSecondaryText;
    }
  }, "".concat(currentStep + 1, " ").concat(multiStepIndicatorText, " ").concat(steps.length))), React.createElement(NavigationButtonsContainer, {
    container: true,
    gap: 1,
    $hasSingleButton: !hasPrevStep
  }, hasPrevStep && React.createElement(Flexbox, {
    item: true,
    flex: "1 1 50%"
  }, React.createElement(Button, {
    variant: "secondary",
    onClick: handleStepBackwards,
    fullWidth: true
  }, prevText)), React.createElement(Flexbox, {
    item: true,
    flex: "1 1 50%"
  }, hasNextStep && !hideNextButton ? React.createElement(Button, {
    variant: "primary",
    onClick: handleStepForward,
    fullWidth: true,
    disabled: nextDisabled
  }, nextText) : !hideDoneButton && React.createElement(Button, {
    variant: "primary",
    onClick: handleDone,
    fullWidth: true
  }, doneText)))));
  if (shouldShowBottomSheet) {
    return React.createElement(BottomSheet, {
      closeOnClickOutside: true,
      onClose: handleClose,
      title: bottomSheetTitle,
      open: bottomSheetOpen
    }, body || getContent);
  }
  return React.createElement(Portal, null, React.createElement(StyledBubble, _extends({
    ref: setPopperElement,
    style: feedbackWidgetMode ? undefined : styles.popper
  }, attributes.popper, {
    barColor: barColor,
    feedbackWidgetMode: feedbackWidgetMode
  }), !feedbackWidgetMode && React.createElement(BubbleArrow, {
    ref: setArrowElement,
    style: styles.arrow,
    bubblePlacement: placement,
    bottomSheet: bottomSheet,
    noBorder: !!barColor
  }), body || getContent, !closeButton && React.createElement(CloseButton, {
    variant: "secondary",
    onClick: handleClose
  }, React.createElement(Icon.Cross16, null))), path && React.createElement(SVG, null, React.createElement("path", {
    d: path
  })));
};