{"version":3,"file":"use-cascader-safe-area.cjs","names":["useDirection"],"sources":["../../../src/components/Cascader/use-cascader-safe-area.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { safePolygon, type HandleCloseContext } from '@floating-ui/react';\nimport { useDirection } from '../../core';\nimport type { CascaderOption, CascaderSafeAreaPolygonOptions } from './Cascader';\n\nexport interface CascaderSafeAreaArmInput {\n  level: number;\n  reference: HTMLElement;\n  floating: HTMLElement;\n  clientX: number;\n  clientY: number;\n}\n\nexport interface UseCascaderSafeAreaInput {\n  safeAreaPolygon: boolean | CascaderSafeAreaPolygonOptions | undefined;\n  onFlush: (level: number, option: CascaderOption) => void;\n}\n\nexport function useCascaderSafeArea({ safeAreaPolygon, onFlush }: UseCascaderSafeAreaInput) {\n  const { dir } = useDirection();\n  const enabled = Boolean(safeAreaPolygon);\n\n  const optionsRef = useRef<CascaderSafeAreaPolygonOptions | undefined>(undefined);\n  optionsRef.current = typeof safeAreaPolygon === 'object' ? safeAreaPolygon : undefined;\n\n  const dirRef = useRef(dir);\n  dirRef.current = dir;\n\n  const onFlushRef = useRef(onFlush);\n  onFlushRef.current = onFlush;\n\n  const blockedLevelRef = useRef<number | null>(null);\n  const pendingRef = useRef<{ level: number; option: CascaderOption } | null>(null);\n  const cleanupRef = useRef<() => void>(() => {});\n  const generationRef = useRef(0);\n\n  const disarm = useCallback(() => {\n    generationRef.current += 1;\n    cleanupRef.current();\n    cleanupRef.current = () => {};\n    blockedLevelRef.current = null;\n    pendingRef.current = null;\n  }, []);\n\n  const arm = useCallback(\n    ({ level, reference, floating, clientX, clientY }: CascaderSafeAreaArmInput) => {\n      disarm();\n      const generation = generationRef.current;\n      const ownerDocument = floating.ownerDocument;\n\n      const handleMouseMove = safePolygon(optionsRef.current)({\n        x: clientX,\n        y: clientY,\n        placement: dirRef.current === 'rtl' ? 'left-start' : 'right-start',\n        elements: { domReference: reference, floating },\n        onClose: () => {\n          if (generationRef.current !== generation) {\n            return;\n          }\n\n          const pending = pendingRef.current;\n          pendingRef.current = null;\n          disarm();\n\n          if (pending) {\n            onFlushRef.current(pending.level, pending.option);\n          }\n        },\n      } as unknown as HandleCloseContext);\n\n      blockedLevelRef.current = level;\n      ownerDocument.addEventListener('mousemove', handleMouseMove);\n      cleanupRef.current = () => ownerDocument.removeEventListener('mousemove', handleMouseMove);\n    },\n    [disarm]\n  );\n\n  useEffect(() => disarm, [disarm]);\n\n  useEffect(() => {\n    if (!enabled) {\n      disarm();\n    }\n  }, [enabled, disarm]);\n\n  const isBlocked = (level: number) =>\n    blockedLevelRef.current !== null && level <= blockedLevelRef.current;\n\n  const setPending = (level: number, option: CascaderOption) => {\n    pendingRef.current = { level, option };\n  };\n\n  const clearPending = (level: number, option: CascaderOption) => {\n    if (pendingRef.current?.level === level && pendingRef.current?.option.value === option.value) {\n      pendingRef.current = null;\n    }\n  };\n\n  return { enabled, arm, disarm, isBlocked, setPending, clearPending };\n}\n"],"mappings":";;;;;AAkBA,SAAgB,oBAAoB,EAAE,iBAAiB,WAAqC;CAC1F,MAAM,EAAE,QAAQA,0BAAAA,aAAa;CAC7B,MAAM,UAAU,QAAQ,eAAe;CAEvC,MAAM,cAAA,GAAA,MAAA,OAAA,CAAgE,KAAA,CAAS;CAC/E,WAAW,UAAU,OAAO,oBAAoB,WAAW,kBAAkB,KAAA;CAE7E,MAAM,UAAA,GAAA,MAAA,OAAA,CAAgB,GAAG;CACzB,OAAO,UAAU;CAEjB,MAAM,cAAA,GAAA,MAAA,OAAA,CAAoB,OAAO;CACjC,WAAW,UAAU;CAErB,MAAM,mBAAA,GAAA,MAAA,OAAA,CAAwC,IAAI;CAClD,MAAM,cAAA,GAAA,MAAA,OAAA,CAAsE,IAAI;CAChF,MAAM,cAAA,GAAA,MAAA,OAAA,OAAsC,CAAC,CAAC;CAC9C,MAAM,iBAAA,GAAA,MAAA,OAAA,CAAuB,CAAC;CAE9B,MAAM,UAAA,GAAA,MAAA,YAAA,OAA2B;EAC/B,cAAc,WAA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