(() => {
  // ../platforms/platform-shopify/dist/shopify-cookie-contract.js
  var COOKIE_NAME = "syntro_experiments";
  var COOKIE_VERSION = 1;
  var CART_ATTRIBUTE_KEY = {
    distinctId: "_syntro_distinct_id",
    telemetryHost: "_syntro_telemetry_host",
    telemetryKey: "_syntro_telemetry_key"
  };

  // ../platforms/platform-shopify/dist/ShopifyPixelBridge.js
  var MAX_AGE_SECONDS = 30 * 24 * 60 * 60;

  // src/shopify-pixel-entry.ts
  async function readCookie(browser) {
    try {
      const raw = await browser.cookie.get(COOKIE_NAME);
      if (!raw) return null;
      const parsed = JSON.parse(raw);
      if (!parsed || typeof parsed !== "object") return null;
      if (parsed.version !== COOKIE_VERSION) return null;
      if (typeof parsed.distinct_id !== "string" || parsed.distinct_id.length === 0) return null;
      if (typeof parsed.telemetry_host !== "string" || !parsed.telemetry_host.startsWith("https://"))
        return null;
      if (typeof parsed.telemetry_key !== "string" || parsed.telemetry_key.length === 0) return null;
      return parsed;
    } catch {
      return null;
    }
  }
  function readFromCartAttributes(attributes) {
    if (!attributes || attributes.length === 0) return null;
    const map = new Map(attributes.map((a) => [a.key, a.value]));
    const distinct_id = map.get(CART_ATTRIBUTE_KEY.distinctId);
    const telemetry_host = map.get(CART_ATTRIBUTE_KEY.telemetryHost);
    const telemetry_key = map.get(CART_ATTRIBUTE_KEY.telemetryKey);
    if (!distinct_id || !telemetry_host || !telemetry_key) return null;
    if (!telemetry_host.startsWith("https://")) return null;
    return { version: COOKIE_VERSION, distinct_id, telemetry_host, telemetry_key };
  }
  async function capture(cookie, eventName, properties) {
    try {
      const url = `${cookie.telemetry_host}/t/${cookie.telemetry_key}/e/`;
      await fetch(url, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        keepalive: true,
        // survive page unload on checkout_completed
        body: JSON.stringify({
          api_key: cookie.telemetry_key,
          // PostHog's capture body field is always `api_key`
          event: eventName,
          distinct_id: cookie.distinct_id,
          properties: {
            $lib: "syntrologie-shopify-pixel",
            $lib_version: "1.0.0",
            ...properties
          },
          timestamp: (/* @__PURE__ */ new Date()).toISOString()
        })
      });
    } catch {
    }
  }
  function register({
    analytics,
    browser
  }) {
    analytics.subscribe("checkout_completed", async (event) => {
      if (!event.data.checkout) return;
      const { checkout } = event.data;
      const identity = readFromCartAttributes(checkout.attributes) ?? await readCookie(browser);
      if (!identity) return;
      await capture(identity, "checkout_completed", {
        order_value: checkout.totalPrice.amount,
        currency: checkout.totalPrice.currencyCode,
        order_id: checkout.order?.id
      });
    });
    analytics.subscribe("product_added_to_cart", async (event) => {
      const cookie = await readCookie(browser);
      if (!cookie) return;
      const cartLine = event.data.cartLine;
      if (!cartLine) return;
      await capture(cookie, "product_added_to_cart", {
        product_id: cartLine.merchandise.product.id,
        variant_id: cartLine.merchandise.id,
        quantity: cartLine.quantity
      });
    });
  }
  if (typeof window !== "undefined") {
    window.__SyntroShopifyPixel = { register };
  }
})();
//# sourceMappingURL=shopify-pixel.js.map
