"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CoachMarks = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactPopper = require("react-popper");
var _ = require("../..");
var _utils = require("./utils");
var _Hooks = require("../../common/Hooks");
var _useScrollPosition = _interopRequireDefault(require("./hooks/useScrollPosition"));
var _BubbleArrow = _interopRequireDefault(require("./BubbleArrow"));
var _consts = require("./Bubble/consts");
var _consts2 = require("./consts");
var _CoachMarks = require("./CoachMarks.styled");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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; }
var CoachMarks = exports.CoachMarks = function CoachMarks(_ref) {
  var steps = _ref.steps,
    onClose = _ref.onClose,
    onDone = _ref.onDone,
    onNext = _ref.onNext,
    onPrev = _ref.onPrev,
    _ref$prevText = _ref.prevText,
    prevTextFromProps = _ref$prevText === void 0 ? 'Previous' : _ref$prevText,
    _ref$nextText = _ref.nextText,
    nextTextFromProps = _ref$nextText === void 0 ? 'Next' : _ref$nextText,
    _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$multiStepIndicat = _ref.multiStepIndicatorText,
    multiStepIndicatorText = _ref$multiStepIndicat === void 0 ? 'of' : _ref$multiStepIndicat,
    _ref$closeOnClickOuts = _ref.closeOnClickOutside,
    closeOnClickOutside = _ref$closeOnClickOuts === void 0 ? true : _ref$closeOnClickOuts,
    barColor = _ref.barColor,
    _ref$bottomSheet = _ref.bottomSheet,
    bottomSheet = _ref$bottomSheet === void 0 ? false : _ref$bottomSheet,
    _ref$closeButton = _ref.closeButton,
    closeButton = _ref$closeButton === void 0 ? false : _ref$closeButton,
    _ref$hidePreviousButt = _ref.hidePreviousButton,
    hidePreviousButton = _ref$hidePreviousButt === void 0 ? false : _ref$hidePreviousButt,
    _ref$feedbackWidgetMo = _ref.feedbackWidgetMode,
    feedbackWidgetMode = _ref$feedbackWidgetMo === void 0 ? false : _ref$feedbackWidgetMo,
    _ref$hideMultiStepInd = _ref.hideMultiStepIndicatorText,
    hideMultiStepIndicatorText = _ref$hideMultiStepInd === void 0 ? false : _ref$hideMultiStepInd,
    overrideStep = _ref.overrideStep;
  var _useState = (0, _react.useState)(overrideStep !== null && overrideStep !== void 0 ? overrideStep : 0),
    _useState2 = _slicedToArray(_useState, 2),
    currentStep = _useState2[0],
    setCurrentStep = _useState2[1];
  var _useState3 = (0, _react.useState)(null),
    _useState4 = _slicedToArray(_useState3, 2),
    referenceElementRect = _useState4[0],
    setReferenceElementRect = _useState4[1];
  (0, _react.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 _useState5 = (0, _react.useState)(null),
    _useState6 = _slicedToArray(_useState5, 2),
    popperElement = _useState6[0],
    setPopperElement = _useState6[1];
  var _useState7 = (0, _react.useState)(null),
    _useState8 = _slicedToArray(_useState7, 2),
    arrowElement = _useState8[0],
    setArrowElement = _useState8[1];
  var scrollPosition = (0, _useScrollPosition.default)();
  var highlightBoxPadding = backdropPadding || _consts2.BACKDROP_PADDING;
  var _usePopper = (0, _reactPopper.usePopper)(referenceElement, popperElement, {
      placement: "".concat(placement, "-start"),
      modifiers: [{
        name: 'arrow',
        options: {
          element: arrowElement
        }
      }, {
        name: 'offset',
        options: {
          offset: [-highlightBoxPadding, _consts.OFFSET_AWAY_FROM_REFERENCE + Number(highlightBoxPadding)]
        }
      }]
    }),
    styles = _usePopper.styles,
    attributes = _usePopper.attributes;
  var internalCoachMarkRef = (0, _react.useRef)(null);
  var windowSize = (0, _Hooks.useWindowSize)();
  var hasMultipleSteps = steps.length > 1;
  var hasPrevStep = currentStep > 0 && !hidePreviousButton;
  var hasNextStep = currentStep + 1 < steps.length;
  var path = referenceElementRect ? (0, _utils.makeBackdropPath)(referenceElementRect, Number(highlightBoxPadding), isCircular, px, py) : undefined;
  (0, _Hooks.useSafeLayoutEffect)(function () {
    if (referenceElement) {
      setReferenceElementRect(referenceElement.getBoundingClientRect());
    }
  }, [referenceElement, windowSize, scrollPosition]);
  var handleStepBackwards = (0, _react.useCallback)(function () {
    if (hasPrevStep) {
      setCurrentStep(currentStep - 1);
    }
    if (onPrev) {
      onPrev(currentStep);
    }
  }, [currentStep, hasPrevStep, onPrev]);
  var handleStepForward = (0, _react.useCallback)(function () {
    if (hasNextStep) {
      setCurrentStep(currentStep + 1);
    }
    if (onNext) {
      onNext(currentStep);
    }
  }, [currentStep, hasNextStep, onNext]);
  var handleDone = (0, _react.useCallback)(function () {
    if (onDone) {
      onDone();
    }
  }, [onDone]);
  var handleClose = function handleClose() {
    if (onClose) {
      onClose();
    }
  };
  (0, _Hooks.useOnClickOutside)(internalCoachMarkRef, function () {
    if (closeOnClickOutside) {
      handleClose();
    }
  });
  return referenceElementRect || feedbackWidgetMode ? _react.default.createElement(_.Portal, null, _react.default.createElement(_CoachMarks.StyledBubble, _extends({
    ref: setPopperElement,
    style: feedbackWidgetMode ? undefined : styles.popper
  }, attributes.popper, {
    barColor: barColor,
    bottomSheet: bottomSheet,
    feedbackWidgetMode: feedbackWidgetMode
  }), !feedbackWidgetMode && _react.default.createElement(_BubbleArrow.default, {
    ref: setArrowElement,
    style: styles.arrow,
    bubblePlacement: placement,
    bottomSheet: bottomSheet,
    noBorder: !!barColor
  }), _react.default.createElement(_.Flexbox, {
    container: true,
    item: true,
    direction: "column",
    flex: "1",
    gap: 5,
    ref: internalCoachMarkRef
  }, body || _react.default.createElement(_.Flexbox, {
    container: true,
    direction: "column",
    gap: 1
  }, icon && _react.default.createElement(_CoachMarks.IconFlex, {
    item: true
  }, icon), title && _react.default.createElement(_.Flexbox, {
    item: true
  }, _react.default.createElement(_CoachMarks.TitleWrapper, {
    $hasIcon: Boolean(icon) || closeButton
  }, _react.default.createElement(_.Typography, {
    as: "h2",
    type: "primary",
    weight: "bold"
  }, title))), content && _react.default.createElement(_.Flexbox, {
    item: true
  }, _react.default.createElement(_CoachMarks.Content, null, typeof content === 'string' ? _react.default.createElement(_.Typography, {
    as: "p",
    type: "secondary",
    color: "inherit"
  }, content) : content))), _react.default.createElement(_CoachMarks.FooterFlex, {
    container: true,
    item: true,
    alignItems: "baseline",
    gap: 5
  }, closeButton ? _react.default.createElement(_.Flexbox, {
    item: true
  }, _react.default.createElement(_.Button, {
    variant: "neutral",
    color: function color(t) {
      return t.color.link;
    },
    onClick: onClose
  }, closeText)) : hasMultipleSteps && !hideMultiStepIndicatorText && _react.default.createElement(_.Flexbox, {
    item: true
  }, _react.default.createElement(_.Typography, {
    type: "secondary",
    color: function color(t) {
      return t.color.bubbleSecondaryText;
    }
  }, "".concat(currentStep + 1, " ").concat(multiStepIndicatorText, " ").concat(steps.length))), _react.default.createElement(_CoachMarks.NavigationButtonsContainer, {
    container: true,
    gap: 1,
    $hasSingleButton: !hasPrevStep
  }, hasPrevStep && _react.default.createElement(_.Flexbox, {
    item: true,
    flex: "1 1 50%"
  }, _react.default.createElement(_.Button, {
    variant: "secondary",
    onClick: handleStepBackwards,
    fullWidth: true
  }, prevText)), _react.default.createElement(_.Flexbox, {
    item: true,
    flex: "1 1 50%"
  }, hasNextStep && !hideNextButton ? _react.default.createElement(_.Button, {
    variant: "primary",
    onClick: handleStepForward,
    fullWidth: true,
    disabled: nextDisabled
  }, nextText) : !hideDoneButton && _react.default.createElement(_.Button, {
    variant: "primary",
    onClick: handleDone,
    fullWidth: true
  }, doneText))))), !closeButton && _react.default.createElement(_CoachMarks.CloseButton, {
    variant: "neutral",
    onClick: handleClose
  }, _react.default.createElement(_.Icon.Cross16, null))), path && _react.default.createElement(_CoachMarks.SVG, null, _react.default.createElement("path", {
    d: path
  }))) : null;
};