import { withQuery } from "ufo";
import { useRegistryScript } from "../utils.js";
import { object, string } from "#nuxt-scripts-validator";
export const GoogleAnalyticsOptions = object({
  /**
   * The Google Analytics ID.
   */
  id: string()
});
export function useScriptGoogleAnalytics(_options) {
  return useRegistryScript("googleAnalytics", (options) => ({
    scriptInput: {
      src: withQuery("https://www.googletagmanager.com/gtag/js", {
        id: options?.id
      })
    },
    schema: import.meta.dev ? GoogleAnalyticsOptions : void 0,
    scriptOptions: {
      use() {
        return { dataLayer: window.dataLayer, gtag: window.gtag };
      },
      // allow dataLayer to be accessed on the server
      stub: import.meta.client ? void 0 : ({ fn }) => {
        return fn === "dataLayer" ? [] : void 0;
      }
    },
    clientInit: import.meta.server ? void 0 : () => {
      const w = window;
      w.dataLayer = w.dataLayer || [];
      const gtag = function() {
        w.dataLayer.push(arguments);
      };
      gtag("js", /* @__PURE__ */ new Date());
      gtag("config", options?.id);
      w.gtag = gtag;
    }
  }), _options);
}
