"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TourProvider = void 0;
var _react = _interopRequireWildcard(require("react"));
var _TourOverlay = require("../../components/tour/TourOverlay");
var _tour = require("../../types/tour");
var _helpers = require("../../utils/helpers");
var _tourContext = require("./tour-context");
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; }
const TourProvider = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
  const {
    children,
    onBackdropPress,
    overlayColor = 'black',
    overlayOpacity = 0.45,
    homeTourSteps,
    credentialsTourSteps,
    credentialOfferTourSteps,
    proofRequestTourSteps,
    nativeDriver = false
  } = props;
  const [currentTour, setCurrentTour] = (0, _react.useState)(_tour.TourID.HomeTour);
  const [currentStep, setCurrentStep] = (0, _react.useState)();
  const [spot, setSpot] = (0, _react.useState)(_tourContext.ORIGIN_SPOT);
  const renderStep = (0, _react.useCallback)(index => {
    if (currentTour === _tour.TourID.HomeTour && homeTourSteps[index] !== undefined || currentTour === _tour.TourID.CredentialsTour && credentialsTourSteps[index] !== undefined || currentTour === _tour.TourID.CredentialOfferTour && credentialOfferTourSteps[index] !== undefined || currentTour === _tour.TourID.ProofRequestTour && proofRequestTourSteps[index] !== undefined) {
      setCurrentStep(index);
    }
  }, [currentTour, homeTourSteps, credentialsTourSteps, credentialOfferTourSteps, proofRequestTourSteps]);
  const changeSpot = (0, _react.useCallback)(newSpot => {
    setSpot(newSpot);
  }, []);
  const start = (0, _react.useCallback)(tourId => {
    setCurrentTour(tourId);
    renderStep(0);
  }, [renderStep]);
  const stop = (0, _react.useCallback)(() => {
    setCurrentStep(undefined);
    setSpot(_tourContext.ORIGIN_SPOT);
  }, []);
  const next = (0, _react.useCallback)(() => {
    let steps = homeTourSteps;
    if (currentTour === _tour.TourID.CredentialsTour) {
      steps = credentialsTourSteps;
    } else if (currentTour === _tour.TourID.CredentialOfferTour) {
      steps = credentialOfferTourSteps;
    } else if (currentTour === _tour.TourID.ProofRequestTour) {
      steps = proofRequestTourSteps;
    }
    if (currentStep !== undefined) {
      currentStep === steps.length - 1 ? stop() : renderStep(currentStep + 1);
    }
  }, [stop, renderStep, currentStep, currentTour, homeTourSteps, credentialsTourSteps, credentialOfferTourSteps, proofRequestTourSteps]);

  // works the same regardless of which tour is on
  const previous = (0, _react.useCallback)(() => {
    if (currentStep !== undefined && currentStep > 0) {
      renderStep(currentStep - 1);
    }
  }, [renderStep, currentStep]);
  const tourStep = (0, _react.useMemo)(() => {
    let stepToRender = undefined;
    let steps = homeTourSteps;
    if (currentTour === _tour.TourID.CredentialsTour) {
      steps = credentialsTourSteps;
    } else if (currentTour === _tour.TourID.CredentialOfferTour) {
      steps = credentialOfferTourSteps;
    } else if (currentTour === _tour.TourID.ProofRequestTour) {
      steps = proofRequestTourSteps;
    }
    stepToRender = currentStep !== undefined ? steps[currentStep] : undefined;
    return stepToRender ?? {
      render: () => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null)
    };
  }, [homeTourSteps, credentialsTourSteps, credentialOfferTourSteps, proofRequestTourSteps, currentStep]);
  const tour = (0, _react.useMemo)(() => ({
    changeSpot,
    currentTour,
    currentStep,
    next,
    previous,
    spot,
    start,
    stop,
    homeTourSteps,
    credentialsTourSteps,
    credentialOfferTourSteps,
    proofRequestTourSteps
  }), [changeSpot, currentTour, currentStep, next, previous, spot, start, stop, homeTourSteps, credentialsTourSteps, credentialOfferTourSteps, proofRequestTourSteps]);
  (0, _react.useImperativeHandle)(ref, () => ({
    currentTour,
    currentStep,
    changeSpot,
    next,
    previous,
    start,
    stop
  }));
  return /*#__PURE__*/_react.default.createElement(_tourContext.TourContext.Provider, {
    value: tour
  }, (0, _helpers.isChildFunction)(children) ? /*#__PURE__*/_react.default.createElement(_tourContext.TourContext.Consumer, null, children) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children), /*#__PURE__*/_react.default.createElement(_TourOverlay.TourOverlay, {
    color: overlayColor,
    currentStep: currentStep,
    currentTour: currentTour,
    changeSpot: changeSpot,
    backdropOpacity: overlayOpacity,
    onBackdropPress: onBackdropPress,
    spot: spot,
    tourStep: tourStep,
    nativeDriver: nativeDriver
  }));
});
exports.TourProvider = TourProvider;
//# sourceMappingURL=tour-provider.js.map