import {
  getWebGLOverlayViewClass
} from "./chunk-PHJ4EBI4.js";
import "./chunk-SGIVSRN5.js";
import {
  useAsyncEffect_default,
  useMemoObjectDeep_default
} from "./chunk-NMUGRJT3.js";
import {
  StateContextProvider_default,
  createStateContext_default,
  useMemoObjectShallow_default
} from "./chunk-W3FLVN5D.js";
import "./chunk-JSBRDJBE.js";

// src/js-tools/react-google/GoogleApiProvider.tsx
import { Loader } from "@googlemaps/js-api-loader";
import { useMemo, useState } from "react";

// src/js-tools/react-google/contexts.ts
import { createContext } from "react";
var GoogleApiContext = createContext({
  loader: null
});
var GoogleMapsContext = createStateContext_default();
var WebGLOverlayViewContext = createStateContext_default();

// src/js-tools/react-google/GoogleApiProvider.tsx
import { jsx } from "react/jsx-runtime";
function GoogleApiProvider({
  options,
  children
}) {
  options = useMemoObjectDeep_default(options);
  const [loader, setLoader] = useState(null);
  useAsyncEffect_default(
    () => async () => {
      const loader2 = new Loader(options);
      await loader2.importLibrary("core");
      setLoader(loader2);
      return async () => {
        loader2.deleteScript();
        setLoader(null);
      };
    },
    [options]
  );
  return useMemo(
    () => /* @__PURE__ */ jsx(
      GoogleApiContext.Provider,
      {
        value: {
          loader
        },
        children
      }
    ),
    [children, loader]
  );
}
var GoogleApiProvider_default = GoogleApiProvider;

// src/js-tools/react-google/GoogleMaps.tsx
import {
  useMemo as useMemo2,
  useRef,
  useState as useState2
} from "react";

// src/js-tools/react-google/useGapiLoader.ts
import { useContext } from "react";
function useGapiLoader() {
  const { loader } = useContext(GoogleApiContext);
  return loader;
}
var useGapiLoader_default = useGapiLoader;

// src/js-tools/react-google/GoogleMaps.tsx
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
function GoogleMaps({
  divRef,
  options,
  children,
  ...props
}) {
  const [map, setMap] = useState2(null);
  const mapDivRef = useRef(null);
  options = useMemoObjectDeep_default(options);
  props = useMemoObjectShallow_default(props);
  const loader = useGapiLoader_default();
  useAsyncEffect_default(
    () => async () => {
      const div = mapDivRef.current;
      if (div && loader) {
        const { Map } = await loader.importLibrary("maps");
        const map2 = new Map(div, { ...options });
        setMap(map2);
        return async () => {
          setMap(null);
        };
      }
    },
    [loader, options]
  );
  return useMemo2(
    () => /* @__PURE__ */ jsx2(
      StateContextProvider_default,
      {
        contexts: [GoogleMapsContext],
        values: [{ map }],
        children: /* @__PURE__ */ jsxs("div", { ref: divRef, ...props, children: [
          /* @__PURE__ */ jsx2(
            "div",
            {
              ref: mapDivRef,
              style: {
                position: "relative",
                width: "100%",
                height: "100%"
              }
            }
          ),
          children
        ] })
      }
    ),
    [divRef, props, children, map]
  );
}
var GoogleMaps_default = GoogleMaps;

// src/js-tools/react-google/WebGLOverlayView.tsx
import { useMemo as useMemo3, useState as useState3 } from "react";

// src/js-tools/react-google/useMap.ts
import { useContext as useContext2 } from "react";
function useMap() {
  return useContext2(GoogleMapsContext)[0].map;
}
var useMap_default = useMap;

// src/js-tools/react-google/WebGLOverlayView.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
function WebGLOverlayView({
  anchor,
  children
}) {
  const [overlayView, setOverlayView] = useState3(
    null
  );
  const loader = useGapiLoader_default();
  const map = useMap_default();
  anchor = useMemoObjectDeep_default(anchor);
  useAsyncEffect_default(
    () => async () => {
      if (map && loader) {
        const { WebGLOverlayView: BaseOverlayView } = await loader.importLibrary("maps");
        const WebGLOverlayView2 = getWebGLOverlayViewClass(BaseOverlayView);
        const overlayView2 = new WebGLOverlayView2(anchor);
        overlayView2.setMap(map);
        setOverlayView(overlayView2);
        return async () => {
          setOverlayView(null);
          overlayView2.setMap(null);
          overlayView2.dispose();
        };
      }
    },
    [loader, map, anchor]
  );
  return useMemo3(
    () => /* @__PURE__ */ jsx3(
      StateContextProvider_default,
      {
        contexts: [WebGLOverlayViewContext],
        values: [
          {
            overlayView
          }
        ],
        children
      }
    ),
    [children, overlayView]
  );
}
var WebGLOverlayView_default = WebGLOverlayView;

// src/js-tools/react-google/useWebGLOverlayView.ts
import { useContext as useContext3 } from "react";
function useWebGLOverlayView() {
  return useContext3(WebGLOverlayViewContext)[0].overlayView;
}
var useWebGLOverlayView_default = useWebGLOverlayView;
export {
  GoogleApiProvider_default as GoogleApiProvider,
  GoogleMaps_default as GoogleMaps,
  WebGLOverlayView_default as WebGLOverlayView,
  useGapiLoader_default as useGapiLoader,
  useMap_default as useMap,
  useWebGLOverlayView_default as useWebGLOverlayView
};
