"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TourContextProvider = exports.TourContext = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _reactDom = require("react-dom");
var _tour = _interopRequireDefault(require("./tour"));
var _advancedTour = require("./advanced-tour");
var _helpers = require("./helpers");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const defaultState = {
  stepId: 0,
  advancedTourStepId: 0,
  showTour: false,
  advancedTourEnabled: false,
  setShowTour: () => {},
  setStepId: () => {},
  setStep: () => {},
  setTourId: () => {},
  setAdvancedTourEnabled: () => {},
  setAdvancedTourStepId: () => {},
  setAdvancedTourId: () => {},
  setShowAdvancedTour: () => {},
  setIsHighlighted: () => {}
};
const TourContext = exports.TourContext = /*#__PURE__*/_react.default.createContext(defaultState);
const TourContextProvider = ({
  children,
  toursSettings,
  a11yProps
}) => {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const a11yPropsWithDefault = a11yProps ?? {
    tourAnchorButtonAriaLabel: t('Advanced tour anchor button'),
    nextTourButtonAriaLabel: t('Next tour page'),
    previousTourButtonAriaLabel: t('Previous tour page')
  };
  const [stepId, setStepId] = (0, _react.useState)(0);
  const [showTour, setShowTour] = (0, _react.useState)(true);
  const [step, setStep] = (0, _react.useState)();
  const [tourId, setTourId] = (0, _react.useState)('');
  const [advancedTourId, setAdvancedTourId] = (0, _react.useState)('');
  const [advancedTourStep, setAdvancedTourStep] = (0, _react.useState)();
  const [advancedTourStepId, setAdvancedTourStepId] = (0, _react.useState)(0);
  const [showAdvancedTour, setShowAdvancedTour] = (0, _react.useState)(false);
  const [advancedTourEnabled, setAdvancedTourEnabled] = (0, _react.useState)(false);
  const [isHighlighted, setIsHighlighted] = (0, _react.useState)(false);
  const advancedTourAnchor = (0, _react.useRef)(null);
  const {
    tour: {
      steps,
      onCompleteHandler,
      onSkipHandler
    },
    advancedTour: {
      steps: advancedSteps,
      onSkipHandler: onAdvancedSkipHandler,
      tourClassName
    }
  } = (0, _helpers.getTourDataById)(tourId, advancedTourId, toursSettings);
  (0, _react.useEffect)(() => {
    setStep(steps[stepId]);
    setAdvancedTourStep(advancedSteps[advancedTourStepId]);
  }, [steps, stepId, advancedTourStepId, advancedSteps]);
  const value = (0, _react.useMemo)(() => ({
    stepId,
    advancedSteps,
    showTour,
    setShowTour,
    setStep,
    setStepId,
    setTourId,
    advancedTourEnabled,
    showAdvancedTour,
    advancedTourStepId,
    setAdvancedTourEnabled,
    setAdvancedTourStepId,
    setAdvancedTourId,
    setShowAdvancedTour,
    isHighlighted,
    setIsHighlighted
  }), [stepId, showTour, tourId, advancedTourStepId, advancedTourId, advancedTourEnabled]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(TourContext.Provider, {
    value: value,
    children: [children, /*#__PURE__*/(0, _reactDom.createPortal)(/*#__PURE__*/(0, _jsxRuntime.jsx)(_advancedTour.AdvancedTourAnchor, {
      ref: advancedTourAnchor,
      onClickHandler: () => setShowAdvancedTour(!showAdvancedTour),
      isVisible: advancedTourEnabled,
      isHighlighted: isHighlighted,
      placement: advancedSteps[advancedTourStepId]?.placement,
      ariaLabel: a11yPropsWithDefault.tourAnchorButtonAriaLabel
    }), document.body), advancedTourStep && advancedTourEnabled && showAdvancedTour && /*#__PURE__*/(0, _jsxRuntime.jsx)(_advancedTour.AdvancedTour, {
      step: advancedTourStep,
      stepNumber: advancedTourStepId + 1,
      totalSteps: advancedSteps?.length,
      setNextStep: () => setAdvancedTourStepId(advancedTourStepId + 1),
      setPrevStep: () => setAdvancedTourStepId(advancedTourStepId - 1),
      onSkipHandler: () => {
        setIsHighlighted(false);
        setShowAdvancedTour(false);
        onAdvancedSkipHandler(advancedTourStepId);
      },
      anchorRef: advancedTourAnchor.current,
      showTour: showAdvancedTour,
      className: (0, _classnames.default)(tourClassName, 'usecase-overview-tour'),
      a11yProps: a11yPropsWithDefault
    }), step && showTour && /*#__PURE__*/(0, _jsxRuntime.jsx)(_tour.default, {
      step: step,
      stepNumber: stepId + 1,
      totalSteps: steps?.length,
      setNextStep: () => setStepId(stepId + 1),
      setPrevStep: () => setStepId(stepId - 1),
      onCompleteHandler: () => {
        setShowTour(false);
        onCompleteHandler();
      },
      onSkipHandler: stepNumber => {
        setShowTour(false);
        onSkipHandler(stepNumber);
      },
      showTour: showTour,
      className: "usecase-overview-tour"
    })]
  });
};
exports.TourContextProvider = TourContextProvider;