{"version":3,"file":"Gallery.mjs","names":[],"sources":["../../../../src/components/Gallery/Gallery.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useMemo, useState } from 'react';\n\nimport { GalleryContext } from './GalleryContext';\nimport { GalleryUI as DefaultGalleryUI } from './GalleryUI';\nimport { useComponentContext } from '../../context';\n\nimport type { GalleryContextValue, GalleryItem } from './GalleryContext';\n\nexport type GalleryProps = {\n  /** Whether clicking the empty gallery background requests close. Defaults to true. */\n  closeOnBackgroundClick?: boolean;\n  items: GalleryItem[];\n  /** Replaces the default UI and takes precedence over `ComponentContext.GalleryUI`. */\n  GalleryUI?: React.ComponentType;\n  /** Index of the item shown first. Defaults to 0. */\n  initialIndex?: number;\n  /** Called on mount and whenever the active index changes. */\n  onIndexChange?: (index: number) => void;\n  /** Called when the gallery requests to close. */\n  onRequestClose?: () => void;\n};\n\nexport const Gallery = ({\n  closeOnBackgroundClick = true,\n  GalleryUI,\n  initialIndex = 0,\n  items,\n  onIndexChange,\n  onRequestClose,\n}: GalleryProps) => {\n  const { GalleryUI: ContextGalleryUI } = useComponentContext();\n  const ResolvedGalleryUI = GalleryUI ?? ContextGalleryUI ?? DefaultGalleryUI;\n  const [currentIndex, setCurrentIndex] = useState(initialIndex);\n\n  const itemCount = items.length;\n\n  const goToIndex = useCallback(\n    (index: number) => {\n      if (index >= 0 && index < itemCount) {\n        setCurrentIndex(index);\n      }\n    },\n    [itemCount],\n  );\n\n  const goToNext = useCallback(() => {\n    setCurrentIndex((prev) => (prev < itemCount - 1 ? prev + 1 : prev));\n  }, [itemCount]);\n\n  const goToPrevious = useCallback(() => {\n    setCurrentIndex((prev) => (prev > 0 ? prev - 1 : prev));\n  }, []);\n\n  useEffect(() => {\n    onIndexChange?.(currentIndex);\n  }, [currentIndex, onIndexChange]);\n\n  const hasNext = currentIndex < itemCount - 1;\n  const hasPrevious = currentIndex > 0;\n  const currentItem = items[currentIndex];\n\n  const contextValue = useMemo<GalleryContextValue>(\n    () => ({\n      closeOnBackgroundClick,\n      currentIndex,\n      currentItem,\n      goToIndex,\n      goToNext,\n      goToPrevious,\n      hasNext,\n      hasPrevious,\n      itemCount,\n      items,\n      onRequestClose,\n    }),\n    [\n      closeOnBackgroundClick,\n      currentIndex,\n      currentItem,\n      goToIndex,\n      goToNext,\n      goToPrevious,\n      hasNext,\n      hasPrevious,\n      itemCount,\n      items,\n      onRequestClose,\n    ],\n  );\n\n  return (\n    <GalleryContext.Provider value={contextValue}>\n      <ResolvedGalleryUI />\n    </GalleryContext.Provider>\n  );\n};\n"],"mappings":";;;;;;AAsBA,IAAa,WAAW,EACtB,yBAAyB,MACzB,WAAA,aACA,eAAe,GACf,OACA,eACA,qBACkB;CAClB,MAAM,EAAE,WAAW,qBAAqB,oBAAoB;CAC5D,MAAM,oBAAoB,eAAa,oBAAoB;CAC3D,MAAM,CAAC,cAAc,mBAAmB,SAAS,YAAY;CAE7D,MAAM,YAAY,MAAM;CAExB,MAAM,YAAY,aACf,UAAkB;EACjB,IAAI,SAAS,KAAK,QAAQ,WACxB,gBAAgB,KAAK;CAEzB,GACA,CAAC,SAAS,CACZ;CAEA,MAAM,WAAW,kBAAkB;EACjC,iBAAiB,SAAU,OAAO,YAAY,IAAI,OAAO,IAAI,IAAK;CACpE,GAAG,CAAC,SAAS,CAAC;CAEd,MAAM,eAAe,kBAAkB;EACrC,iBAAiB,SAAU,OAAO,IAAI,OAAO,IAAI,IAAK;CACxD,GAAG,CAAC,CAAC;CAEL,gBAAgB;EACd,gBAAgB,YAAY;CAC9B,GAAG,CAAC,cAAc,aAAa,CAAC;CAEhC,MAAM,UAAU,eAAe,YAAY;CAC3C,MAAM,cAAc,eAAe;CACnC,MAAM,cAAc,MAAM;CAE1B,MAAM,eAAe,eACZ;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,IACA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CACF;CAEA,OACE,oBAAC,eAAe,UAAhB;EAAyB,OAAO;YAC9B,oBAAC,mBAAD,CAAoB,CAAA;CACG,CAAA;AAE7B"}