{"version":3,"file":"react.cjs","names":[],"sources":["../src/lib/core/NasaEarthdataControlReact.tsx","../src/lib/hooks/useNasaEarthdata.ts"],"sourcesContent":["import { useEffect, useRef } from \"react\";\nimport { NasaEarthdataControl } from \"./NasaEarthdataControl\";\nimport type { NasaEarthdataReactProps } from \"./types\";\n\n/**\n * React wrapper component for NasaEarthdataControl.\n *\n * This component manages the lifecycle of a NasaEarthdataControl instance,\n * adding it to the map on mount and removing it on unmount.\n *\n * @example\n * ```tsx\n * import { NasaEarthdataControlReact } from 'maplibre-gl-nasa-earthdata/react';\n *\n * function MyMap() {\n *   const [map, setMap] = useState<Map | null>(null);\n *\n *   return (\n *     <>\n *       <div ref={mapContainer} />\n *       {map && (\n *         <NasaEarthdataControlReact\n *           map={map}\n *           title=\"NASA Earthdata\"\n *           collapsed={false}\n *           onLayerAdd={(layer) => console.log('Added', layer.id)}\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 NasaEarthdataControlReact({\n  map,\n  onStateChange,\n  onLayerAdd,\n  onLayerRemove,\n  ...options\n}: NasaEarthdataReactProps): null {\n  const controlRef = useRef<NasaEarthdataControl | null>(null);\n\n  useEffect(() => {\n    if (!map) return;\n\n    // Create the control instance\n    const control = new NasaEarthdataControl(options);\n    controlRef.current = control;\n\n    // Register event handlers if provided\n    if (onStateChange) {\n      control.on(\"statechange\", (event) => {\n        onStateChange(event.state);\n      });\n    }\n    if (onLayerAdd) {\n      control.on(\"layeradd\", (event) => {\n        if (event.layer) {\n          onLayerAdd(event.layer);\n        }\n      });\n    }\n    if (onLayerRemove) {\n      control.on(\"layerremove\", (event) => {\n        if (event.layer) {\n          onLayerRemove(event.layer.id);\n        }\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  return null;\n}\n","import { useState, useCallback } from \"react\";\nimport type { NasaEarthdataState, AddedLayerState } from \"../core/types\";\n\n/**\n * Default initial state for the NASA Earthdata control\n */\nconst DEFAULT_STATE: NasaEarthdataState = {\n  collapsed: true,\n  panelWidth: 320,\n  query: \"\",\n  addedLayers: [],\n};\n\n/**\n * Custom hook for managing NASA Earthdata control state in React applications.\n *\n * This hook provides a simple way to track and update the state\n * of a NasaEarthdataControl from React components.\n *\n * @example\n * ```tsx\n * function MyComponent() {\n *   const { state, setState, setCollapsed, toggle } = useNasaEarthdata();\n *\n *   return (\n *     <div>\n *       <button onClick={toggle}>\n *         {state.collapsed ? 'Expand' : 'Collapse'}\n *       </button>\n *       <NasaEarthdataControlReact\n *         map={map}\n *         collapsed={state.collapsed}\n *         onStateChange={setState}\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 useNasaEarthdata(initialState?: Partial<NasaEarthdataState>) {\n  const [state, setState] = useState<NasaEarthdataState>({\n    ...DEFAULT_STATE,\n    ...initialState,\n  });\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 search query\n   */\n  const setQuery = useCallback((query: string) => {\n    setState((prev) => ({ ...prev, query }));\n  }, []);\n\n  /**\n   * Sets the list of added layers\n   */\n  const setAddedLayers = useCallback((addedLayers: AddedLayerState[]) => {\n    setState((prev) => ({ ...prev, addedLayers }));\n  }, []);\n\n  /**\n   * Resets the state to default values\n   */\n  const reset = useCallback(() => {\n    setState({ ...DEFAULT_STATE, ...initialState });\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    setQuery,\n    setAddedLayers,\n    reset,\n    toggle,\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,0BAA0B,EACxC,KACA,eACA,YACA,eACA,GAAG,WAC6B;CAChC,MAAM,cAAA,GAAA,MAAA,QAAiD,IAAI;CAE3D,CAAA,GAAA,MAAA,iBAAgB;EACd,IAAI,CAAC,KAAK;EAGV,MAAM,UAAU,IAAI,6BAAA,qBAAqB,OAAO;EAChD,WAAW,UAAU;EAGrB,IAAI,eACF,QAAQ,GAAG,gBAAgB,UAAU;GACnC,cAAc,MAAM,KAAK;EAC3B,CAAC;EAEH,IAAI,YACF,QAAQ,GAAG,aAAa,UAAU;GAChC,IAAI,MAAM,OACR,WAAW,MAAM,KAAK;EAE1B,CAAC;EAEH,IAAI,eACF,QAAQ,GAAG,gBAAgB,UAAU;GACnC,IAAI,MAAM,OACR,cAAc,MAAM,MAAM,EAAE;EAEhC,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,IA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