{"version":3,"file":"use-tour-state.mjs","names":[],"sources":["../../../src/components/Tour/use-tour-state.ts"],"sourcesContent":["import { useEffect, useEffectEvent, useRef, useState } from 'react';\nimport {\n  useFocusReturn,\n  useHotkeys,\n  useUncontrolled,\n  useWindowEvent,\n  type HotkeyItem,\n} from '@mantine/hooks';\nimport { useDirection } from '../../core';\n\nexport interface UseTourStateInput {\n  active: boolean | undefined;\n  onClose: (() => void) | undefined;\n  step: number | undefined;\n  defaultStep: number | undefined;\n  onStepChange: ((step: number) => void) | undefined;\n  onStepOpen: (step: number) => void;\n  onStepClose: (step: number) => void;\n  mode: 'guided' | 'beacon';\n  stepsCount: number;\n  closeOnEscape: boolean | undefined;\n  withKeyboardNavigation: boolean | undefined;\n}\n\nexport function useTourState({\n  active,\n  onClose,\n  step,\n  defaultStep,\n  onStepChange,\n  onStepOpen,\n  onStepClose,\n  mode,\n  stepsCount,\n  closeOnEscape,\n  withKeyboardNavigation,\n}: UseTourStateInput) {\n  const { dir } = useDirection();\n  const targetRef = useRef<HTMLElement | null>(null);\n\n  const [currentStep, setCurrentStep, controlled] = useUncontrolled({\n    value: step,\n    defaultValue: defaultStep,\n    finalValue: 0,\n    onChange: onStepChange,\n  });\n\n  const [beaconOpenStep, setBeaconOpenStep] = useState<number | null>(null);\n\n  const resetUncontrolledStep = useEffectEvent(() => {\n    const initialStep = defaultStep ?? 0;\n    if (!controlled && currentStep !== initialStep) {\n      setCurrentStep(initialStep);\n    }\n  });\n\n  const wasActiveRef = useRef(!!active);\n\n  useEffect(() => {\n    if (wasActiveRef.current && !active) {\n      resetUncontrolledStep();\n    }\n    wasActiveRef.current = !!active;\n  }, [active]);\n\n  const showTooltip = !!active && (mode === 'guided' || beaconOpenStep !== null);\n  const displayStepIndex =\n    mode === 'beacon' && beaconOpenStep !== null ? beaconOpenStep : currentStep;\n  const openStep = showTooltip && displayStepIndex >= 0 ? displayStepIndex : null;\n\n  const reportedStepRef = useRef<number | null>(null);\n  const emitStepClose = useEffectEvent((index: number) => onStepClose(index));\n  const emitStepOpen = useEffectEvent((index: number) => onStepOpen(index));\n\n  useEffect(() => {\n    const previousStep = reportedStepRef.current;\n\n    if (previousStep === openStep) {\n      return;\n    }\n\n    reportedStepRef.current = openStep;\n\n    if (previousStep !== null) {\n      emitStepClose(previousStep);\n    }\n\n    if (openStep !== null) {\n      emitStepOpen(openStep);\n    }\n  }, [openStep]);\n\n  const close = () => {\n    const openedStep = reportedStepRef.current;\n\n    if (openedStep !== null) {\n      reportedStepRef.current = null;\n      onStepClose(openedStep);\n    }\n\n    setBeaconOpenStep(null);\n    onClose?.();\n  };\n\n  const setStep = (index: number) => {\n    if (mode === 'beacon' && beaconOpenStep !== null) {\n      setBeaconOpenStep(index);\n    }\n    setCurrentStep(index);\n  };\n\n  const openBeaconStep = (index: number) => {\n    setBeaconOpenStep(index);\n    setCurrentStep(index);\n  };\n\n  useFocusReturn({ opened: !!active, shouldReturnFocus: true });\n\n  const isInsideTarget = (event: KeyboardEvent) =>\n    !!targetRef.current && event.target instanceof Node && targetRef.current.contains(event.target);\n\n  const goToNextStep = (event: KeyboardEvent) => {\n    if (isInsideTarget(event)) {\n      return;\n    }\n\n    event.preventDefault();\n\n    if (currentStep < stepsCount - 1) {\n      setCurrentStep(currentStep + 1);\n    } else {\n      close();\n    }\n  };\n\n  const goToPreviousStep = (event: KeyboardEvent) => {\n    if (isInsideTarget(event)) {\n      return;\n    }\n\n    event.preventDefault();\n\n    if (currentStep > 0) {\n      setCurrentStep(currentStep - 1);\n    }\n  };\n\n  const arrowHotkeys: HotkeyItem[] = [];\n\n  if (active && mode === 'guided' && withKeyboardNavigation) {\n    arrowHotkeys.push([\n      dir === 'rtl' ? 'ArrowLeft' : 'ArrowRight',\n      goToNextStep,\n      { preventDefault: false },\n    ]);\n    arrowHotkeys.push([\n      dir === 'rtl' ? 'ArrowRight' : 'ArrowLeft',\n      goToPreviousStep,\n      { preventDefault: false },\n    ]);\n  }\n\n  useHotkeys(arrowHotkeys);\n  useWindowEvent(\n    'keydown',\n    (event) => {\n      if (\n        event.key === 'Escape' &&\n        showTooltip &&\n        closeOnEscape &&\n        !event.isComposing &&\n        (event.target as HTMLElement | null)?.getAttribute?.('data-mantine-stop-propagation') !==\n          'true'\n      ) {\n        close();\n      }\n    },\n    { capture: true }\n  );\n\n  return {\n    currentStep,\n    setStep,\n    beaconOpenStep,\n    openBeaconStep,\n    showTooltip,\n    displayStepIndex,\n    close,\n    targetRef,\n  };\n}\n"],"mappings":";;;;;AAwBA,SAAgB,aAAa,EAC3B,QACA,SACA,MACA,aACA,cACA,YACA,aACA,MACA,YACA,eACA,0BACoB;CACpB,MAAM,EAAE,QAAQ,aAAa;CAC7B,MAAM,YAAY,OAA2B,IAAI;CAEjD,MAAM,CAAC,aAAa,gBAAgB,cAAc,gBAAgB;EAChE,OAAO;EACP,cAAc;EACd,YAAY;EACZ,UAAU;CACZ,CAAC;CAED,MAAM,CAAC,gBAAgB,qBAAqB,SAAwB,IAAI;CAExE,MAAM,wBAAwB,qBAAqB;EACjD,MAAM,cAAc,eAAe;EACnC,IAAI,CAAC,cAAc,gBAAgB,aACjC,eAAe,WAAW;CAE9B,CAAC;CAED,MAAM,eAAe,OAAO,CAAC,CAAC,MAAM;CAEpC,gBAAgB;EACd,IAAI,aAAa,WAAW,CAAC,QAC3B,sBAAsB;EAExB,aAAa,UAAU,CAAC,CAAC;CAC3B,GAAG,CAAC,MAAM,CAAC;CAEX,MAAM,cAAc,CAAC,CAAC,WAAW,SAAS,YAAY,mBAAmB;CACzE,MAAM,mBACJ,SAAS,YAAY,mBAAmB,OAAO,iBAAiB;CAClE,MAAM,WAAW,eAAe,oBAAoB,IAAI,mBAAmB;CAE3E,MAAM,kBAAkB,OAAsB,IAAI;CAClD,MAAM,gBAAgB,gBAAgB,UAAkB,YAAY,KAAK,CAAC;CAC1E,MAAM,eAAe,gBAAgB,UAAkB,WAAW,KAAK,CAAC;CAExE,gBAAgB;EACd,MAAM,eAAe,gBAAgB;EAEr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