{"version":3,"file":"MapItems.mjs","sources":[""],"sourcesContent":["import { useYMaps } from \"@pbe/react-yandex-maps\";\r\nimport type {\r\n  FC, ReactNode, PropsWithChildren,\r\n} from \"react\";\r\nimport {\r\n  memo,\r\n  useContext,\r\n  useEffect,\r\n  useMemo,\r\n  useRef,\r\n} from \"react\";\r\nimport type { Clusterer } from \"yandex-maps\";\r\nimport { MapClusterer } from \"./MapClusterer\";\r\nimport type { TMapModulesProps } from \"./MapModules\";\r\nimport { MapModules } from \"./MapModules\";\r\nimport { MapPanel } from \"./MapPanel\";\r\nimport { MapPlacemark } from \"./MapPlacemark\";\r\nimport { mapDefaults } from \"../config\";\r\nimport type {\r\n  IMap, TCluster, TMapContext, TPoint,\r\n} from \"../config/types\";\r\nimport { MapContext } from \"../context\";\r\nimport { getActivePointsCount } from \"../lib/clusterer\";\r\n\r\nexport type TMapItemsProps = Pick<IMap, \"clusters\"> & PropsWithChildren<{\r\n  onReady?: (clusters: Clusterer[]) => void;\r\n}>;\r\n\r\nconst MemoModules = memo((props: TMapModulesProps): ReactNode => <MapModules {...props} />);\r\nMemoModules.displayName = \"MapModules\";\r\n\r\nconst hasPointPaneData = (point: TPoint): boolean => {\r\n  return !!(point?.meta?.pane?.title || point?.meta?.pane?.description);\r\n};\r\n\r\n/**\r\n * Внутренний компонент содержимого карт\r\n * @returns {ReactNode}\r\n */\r\nconst MapItems: FC<TMapItemsProps> = ({\r\n  children,\r\n  clusters = [],\r\n  onReady,\r\n}: TMapItemsProps): ReactNode => {\r\n\r\n  const {\r\n    panelSlot, modules, api,\r\n  } = useContext<TMapContext>(MapContext);\r\n\r\n  const ymaps = useYMaps(api?.load ?? mapDefaults.api!.load);\r\n\r\n  const clustersRef = useRef<Clusterer[]>([]);\r\n\r\n  const activePointsCountRef = useRef<number>(getActivePointsCount(clusters));\r\n\r\n  const isPanelEnabled = useMemo(() => {\r\n    if (panelSlot) {\r\n      return true;\r\n    }\r\n    return clusters.some((cluster) => cluster.points.some((point) => hasPointPaneData(point)));\r\n  }, [ panelSlot, clusters ]);\r\n\r\n  useEffect(() => {\r\n    clustersRef.current = [];\r\n    activePointsCountRef.current = getActivePointsCount(clusters);\r\n  }, [ clusters ]);\r\n\r\n  const onClusterersReady = (instance: Clusterer) => {\r\n    clustersRef.current.push(instance);\r\n    if (clustersRef.current.length === clusters?.length && typeof onReady === \"function\") {\r\n      onReady(clustersRef.current);\r\n    }\r\n  };\r\n\r\n  return ymaps && (\r\n    <>\r\n      {clusters.map((cluster: TCluster, index) => {\r\n        const { id, points } = cluster;\r\n        const clusterKey = `clusterer_${id}_${points.map((point) => `${point.id}_${point.coords.join(\"_\")}`).join(\"|\")}_${index}`;\r\n        return (\r\n          <MapClusterer onReady={onClusterersReady} cluster={cluster} key={clusterKey}>\r\n            {points.map((props: TPoint, index) => {\r\n              const pointKey = `placemark_${id}_${props.id}_${props.coords.join(\"_\")}_${index}`;\r\n              return (\r\n                <MapPlacemark cluster={cluster} activePointsCountRef={activePointsCountRef} isPanel={!!panelSlot || hasPointPaneData(props)} point={props} key={pointKey} />\r\n              );\r\n            })}\r\n          </MapClusterer>\r\n        );\r\n      })}\r\n      <MemoModules ymaps={ymaps} modules={modules} />\r\n      {isPanelEnabled && <MapPanel>{panelSlot}</MapPanel>}\r\n      {children}\r\n    </>\r\n  );\r\n\r\n};\r\n\r\nexport {\r\n  MapItems,\r\n};\r\n"],"names":["MemoModules","memo","props","_jsx","MapModules","displayName","hasPointPaneData","point","meta","pane","title","description","MapItems","children","clusters","onReady","panelSlot","modules","api","useContext","MapContext","ymaps","useYMaps","load","mapDefaults","clustersRef","useRef","activePointsCountRef","getActivePointsCount","isPanelEnabled","useMemo","some","cluster","points","useEffect","current","onClusterersReady","instance","push","length","_jsxs","_Fragment","map","index","id","clusterKey","coords","join","MapClusterer","pointKey","MapPlacemark","isPanel","MapPanel"],"mappings":"ydA4BA,MAAMA,YAAcC,KAAMC,OAAuCC,IAACC,WAAU,IAAKF,SACjFF,YAAYK,YAAc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