{"version":3,"file":"react.cjs","names":[],"sources":["../src/lib/core/OvertureMapsControlReact.tsx","../src/lib/hooks/useOvertureMapsState.ts"],"sourcesContent":["import { useEffect, useRef } from \"react\";\nimport { OvertureMapsControl } from \"./OvertureMapsControl\";\nimport type { OvertureMapsControlReactProps } from \"./types\";\n\n/**\n * React wrapper component for OvertureMapsControl.\n *\n * This component manages the lifecycle of an OvertureMapsControl instance,\n * adding it to the map on mount and removing it on unmount.\n *\n * @example\n * ```tsx\n * import { OvertureMapsControlReact } from 'maplibre-gl-overture-maps/react';\n *\n * function MyMap() {\n *   const [map, setMap] = useState<Map | null>(null);\n *\n *   return (\n *     <>\n *       <div ref={mapContainer} />\n *       {map && (\n *         <OvertureMapsControlReact\n *           map={map}\n *           collapsed={false}\n *           visibleThemes={['buildings', 'places']}\n *         />\n *       )}\n *     </>\n *   );\n * }\n * ```\n *\n * @param props - Component props including map instance and control options\n * @returns null - This component renders nothing directly\n */\nexport function OvertureMapsControlReact({\n  map,\n  onStateChange,\n  ...options\n}: OvertureMapsControlReactProps): null {\n  const controlRef = useRef<OvertureMapsControl | null>(null);\n\n  useEffect(() => {\n    if (!map) return;\n\n    // Create the control instance\n    const control = new OvertureMapsControl(options);\n    controlRef.current = control;\n\n    // Register state change handler if provided\n    if (onStateChange) {\n      control.on(\"statechange\", (event) => {\n        onStateChange(event.state);\n      });\n    }\n\n    // Add control to map\n    map.addControl(control, options.position || \"top-right\");\n\n    // Cleanup on unmount\n    return () => {\n      if (map.hasControl(control)) {\n        map.removeControl(control);\n      }\n      controlRef.current = null;\n    };\n  }, [map]);\n\n  // Update options when they change\n  useEffect(() => {\n    if (controlRef.current) {\n      // Handle collapsed state changes\n      const currentState = controlRef.current.getState();\n      if (\n        options.collapsed !== undefined &&\n        options.collapsed !== currentState.collapsed\n      ) {\n        if (options.collapsed) {\n          controlRef.current.collapse();\n        } else {\n          controlRef.current.expand();\n        }\n      }\n    }\n  }, [options.collapsed]);\n\n  // Sync pinned release changes\n  useEffect(() => {\n    if (controlRef.current && options.release) {\n      controlRef.current.setRelease(options.release);\n    }\n  }, [options.release]);\n\n  return null;\n}\n","import { useState, useCallback } from 'react';\nimport type { OvertureMapsState, OvertureThemeState, OvertureLayerState } from '../core/types';\nimport { THEMES, THEME_IDS, defaultSizeForGeometry } from '../core/themes';\nimport type { OvertureTheme } from '../core/themes';\n\n/**\n * Builds the default per-theme, per-layer state.\n *\n * @returns Per-theme state with each source layer's defaults\n */\nfunction defaultThemes(): Record<OvertureTheme, OvertureThemeState> {\n  const themes = {} as Record<OvertureTheme, OvertureThemeState>;\n  for (const theme of THEME_IDS) {\n    const def = THEMES[theme];\n    const visible = ['buildings', 'transportation', 'places'].includes(theme);\n    const layers = {} as Record<string, OvertureLayerState>;\n    for (const layer of def.layers) {\n      layers[layer.sourceLayer] = {\n        visible,\n        opacity: 0.8,\n        color: def.color,\n        size: defaultSizeForGeometry(layer.geometry),\n      };\n    }\n    themes[theme] = { expanded: false, layers };\n  }\n  return themes;\n}\n\n/**\n * Custom hook for managing Overture Maps control state in React applications.\n *\n * This hook provides a simple way to track and update the state\n * of an OvertureMapsControl from React components.\n *\n * @example\n * ```tsx\n * function MyComponent() {\n *   const { state, setState, toggle } = useOvertureMapsState();\n *\n *   return (\n *     <div>\n *       <button onClick={toggle}>\n *         {state.collapsed ? 'Expand' : 'Collapse'}\n *       </button>\n *       <OvertureMapsControlReact\n *         map={map}\n *         collapsed={state.collapsed}\n *         onStateChange={(newState) => setState(newState)}\n *       />\n *     </div>\n *   );\n * }\n * ```\n *\n * @param initialState - Optional initial state values\n * @returns Object containing state and update functions\n */\nexport function useOvertureMapsState(initialState?: Partial<OvertureMapsState>) {\n  const buildDefault = (): OvertureMapsState => ({\n    collapsed: true,\n    panelWidth: 300,\n    release: '',\n    releases: [],\n    themes: defaultThemes(),\n    inspect: true,\n    error: null,\n    ...initialState,\n  });\n\n  const [state, setState] = useState<OvertureMapsState>(buildDefault);\n\n  /**\n   * Sets the collapsed state\n   */\n  const setCollapsed = useCallback((collapsed: boolean) => {\n    setState((prev) => ({ ...prev, collapsed }));\n  }, []);\n\n  /**\n   * Sets the panel width\n   */\n  const setPanelWidth = useCallback((panelWidth: number) => {\n    setState((prev) => ({ ...prev, panelWidth }));\n  }, []);\n\n  /**\n   * Sets the active release\n   */\n  const setRelease = useCallback((release: string) => {\n    setState((prev) => ({ ...prev, release }));\n  }, []);\n\n  /**\n   * Maps every source layer of a theme through an updater\n   */\n  const updateThemeLayers = useCallback(\n    (theme: OvertureTheme, update: (layer: OvertureLayerState) => OvertureLayerState) => {\n      setState((prev) => {\n        const themeState = prev.themes[theme];\n        const layers = {} as Record<string, OvertureLayerState>;\n        for (const key of Object.keys(themeState.layers)) {\n          layers[key] = update(themeState.layers[key]);\n        }\n        return { ...prev, themes: { ...prev.themes, [theme]: { ...themeState, layers } } };\n      });\n    },\n    []\n  );\n\n  /**\n   * Sets the visibility of every layer in a theme\n   */\n  const setThemeVisible = useCallback(\n    (theme: OvertureTheme, visible: boolean) => {\n      updateThemeLayers(theme, (layer) => ({ ...layer, visible }));\n    },\n    [updateThemeLayers]\n  );\n\n  /**\n   * Sets the opacity of every layer in a theme\n   */\n  const setThemeOpacity = useCallback(\n    (theme: OvertureTheme, opacity: number) => {\n      updateThemeLayers(theme, (layer) => ({ ...layer, opacity }));\n    },\n    [updateThemeLayers]\n  );\n\n  /**\n   * Updates a single source layer's state\n   */\n  const setLayer = useCallback(\n    (theme: OvertureTheme, sourceLayer: string, patch: Partial<OvertureLayerState>) => {\n      setState((prev) => {\n        const themeState = prev.themes[theme];\n        return {\n          ...prev,\n          themes: {\n            ...prev.themes,\n            [theme]: {\n              ...themeState,\n              layers: {\n                ...themeState.layers,\n                [sourceLayer]: { ...themeState.layers[sourceLayer], ...patch },\n              },\n            },\n          },\n        };\n      });\n    },\n    []\n  );\n\n  /**\n   * Enables or disables the feature inspection picker\n   */\n  const setInspect = useCallback((inspect: boolean) => {\n    setState((prev) => ({ ...prev, inspect }));\n  }, []);\n\n  /**\n   * Resets the state to default values\n   */\n  const reset = useCallback(() => {\n    setState(buildDefault());\n  }, [initialState]);\n\n  /**\n   * Toggles the collapsed state\n   */\n  const toggle = useCallback(() => {\n    setState((prev) => ({ ...prev, collapsed: !prev.collapsed }));\n  }, []);\n\n  return {\n    state,\n    setState,\n    setCollapsed,\n    setPanelWidth,\n    setRelease,\n    setThemeVisible,\n    setThemeOpacity,\n    setLayer,\n    setInspect,\n    reset,\n    toggle,\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,yBAAyB,EACvC,KACA,eACA,GAAG,WACmC;CACtC,MAAM,cAAA,GAAA,MAAA,QAAgD,IAAI;CAE1D,CAAA,GAAA,MAAA,iBAAgB;EACd,IAAI,CAAC,KAAK;EAGV,MAAM,UAAU,IAAI,4BAAA,oBAAoB,OAAO;EAC/C,WAAW,UAAU;EAGrB,IAAI,eACF,QAAQ,GAAG,gBAAgB,UAAU;GACnC,cAAc,MAAM,KAAK;EAC3B,CAAC;EAIH,IAAI,WAAW,SAAS,QAAQ,YAAY,WAAW;EAGvD,aAAa;GACX,IAAI,IAAI,WAAW,OAAO,GACxB,IAAI,cAAc,OAAO;GAE3B,WAAW,UAAU;EACvB;CACF,GAAG,CAAC,GAAG,CAAC;CAGR,CAAA,GAAA,MAAA,iBAAgB;EACd,IAAI,WAAW,SAAS;GAEtB,MAAM,eAAe,WAAW,QAAQ,SAAS;GACjD,IACE,QAAQ,cAAc,KAAA,KACtB,QAAQ,cAAc,aAAa,WAEnC,IAAI,QAAQ,WACV,WAAW,QAAQ,SAAS;QAE5B,WAAW,QAAQ,OAAO;EAGhC;CACF,GAAG,CAAC,QAAQ,SAAS,CAAC;CAGtB,CAAA,GAAA,MAAA,iBAAgB;EACd,IAAI,WAAW,WAAW,QAAQ,SAChC,WAAW,QAAQ,WAAW,QAAQ,OAAO;CAEjD,GAAG,CAAC,QAAQ,OAAO,CAAC;CAEpB,O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