var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
var __accessCheck = (obj, member, msg) => {
  if (!member.has(obj))
    throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
  __accessCheck(obj, member, "read from private field");
  return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
  if (member.has(obj))
    throw TypeError("Cannot add the same private member more than once");
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
  __accessCheck(obj, member, "write to private field");
  setter ? setter.call(obj, value) : member.set(obj, value);
  return value;
};
var __privateMethod = (obj, member, method) => {
  __accessCheck(obj, member, "access private method");
  return method;
};
(function() {
  "use strict";
  var _endpoint, _startTimestamp, _initialized, _cookieName, _cookieExpDays, _debug, _getCookie, getCookie_fn, _setCookie, setCookie_fn, _sendXhr, sendXhr_fn, _sendBeacon, sendBeacon_fn, _addListeners, addListeners_fn;
  class PlugAndPayAnalytics {
    constructor() {
      __privateAdd(this, _getCookie);
      __privateAdd(this, _setCookie);
      __privateAdd(this, _sendXhr);
      __privateAdd(this, _sendBeacon);
      __privateAdd(this, _addListeners);
      __privateAdd(this, _endpoint, "https://api.plugandpay.nl/v2/statistics/activities");
      __privateAdd(this, _startTimestamp, Date.now());
      __privateAdd(this, _initialized, false);
      __privateAdd(this, _cookieName, "ppa_visitor_id");
      __privateAdd(this, _cookieExpDays, 3650);
      __privateAdd(this, _debug, false);
      __publicField(this, "data", {});
      if (this.init()) {
        this.trackActivity();
      }
    }
    get isInitialized() {
      return __privateGet(this, _initialized);
    }
    init() {
      __privateSet(this, _debug, __privateMethod(this, _getCookie, getCookie_fn).call(this, "ppa_debug") === "1");
      __privateGet(this, _debug) && console.warn("PPA Debug mode enabled");
      if (__privateGet(this, _initialized))
        return true;
      const cookie = __privateMethod(this, _getCookie, getCookie_fn).call(this, __privateGet(this, _cookieName));
      if (!cookie) {
        this.data.visitor_id = Math.random().toString(36).substring(2, 15) + Date.now();
        __privateMethod(this, _setCookie, setCookie_fn).call(this, __privateGet(this, _cookieName), this.data.visitor_id, __privateGet(this, _cookieExpDays));
      } else {
        this.data.visitor_id = cookie;
      }
      if (!window.analyticsData || !window.analyticsData.tenant_id || !window.analyticsData.viewable_type) {
        __privateGet(this, _debug) && console.error("Missing required data");
        return false;
      }
      this.data.user_agent = window.navigator.userAgent;
      this.data.page_url = window.analyticsData.page_url || window.location.pathname;
      this.data.tenant_id = window.analyticsData.tenant_id || null;
      this.data.viewable_type = window.analyticsData.viewable_type || null;
      this.data.checkout_id = window.analyticsData.checkout_id || null;
      this.data.form_id = window.analyticsData.form_id || null;
      this.data.order_id = window.analyticsData.order_id || null;
      this.data.popup_id = window.analyticsData.popup_id || null;
      this.data.product_id = window.analyticsData.product_id || null;
      this.data.landing_page_id = window.analyticsData.landing_page_id || null;
      this.data.scroll_percentage = null;
      this.data.time_on_page = null;
      __privateMethod(this, _addListeners, addListeners_fn).call(this);
      __privateSet(this, _initialized, true);
      return true;
    }
    setDebug(debug) {
      __privateMethod(this, _setCookie, setCookie_fn).call(this, "ppa_debug", debug ? "1" : "0", 1);
    }
    trackActivity() {
      const payload = [];
      payload.push(__spreadProps(__spreadValues({}, this.data), {
        type: "viewed"
      }));
      if (window.analyticsData.viewable_type === "checkout" && window.analyticsData.order_bumps) {
        window.analyticsData.order_bumps.forEach((ob) => {
          payload.push(__spreadProps(__spreadValues({}, this.data), {
            type: "viewed",
            viewable_type: "order_bump",
            product_id: ob.id
          }));
        });
      }
      this.send(payload);
    }
    send(data) {
      if (!__privateGet(this, _initialized)) {
        __privateGet(this, _debug) && console.error("PPA: Analytics not initialized, cannot send data.");
        return;
      }
      const utmData = this.getUTMParams();
      const finalData = data.map((d) => __spreadValues(__spreadValues({}, d), utmData));
      finalData.forEach((d) => {
        if (__privateGet(this, _debug)) {
          d.debug = true;
        }
      });
      if ("sendBeacon" in navigator) {
        return __privateMethod(this, _sendBeacon, sendBeacon_fn).call(this, finalData);
      }
      return __privateMethod(this, _sendXhr, sendXhr_fn).call(this, finalData);
    }
    getTimeOnPage() {
      return null;
    }
    getUTMParams() {
      const urlParams = new URLSearchParams(window.location.search);
      const utm_source = urlParams.get("utm_source");
      const utm_medium = urlParams.get("utm_medium");
      const utm_campaign = urlParams.get("utm_campaign");
      const utm_term = urlParams.get("utm_term");
      const utm_content = urlParams.get("utm_content");
      return {
        utm_source,
        utm_medium,
        utm_campaign,
        utm_term,
        utm_content
      };
    }
    getScrollPercentage() {
      const h = document.documentElement;
      const b = document.body;
      const st = "scrollTop";
      const sh = "scrollHeight";
      return Math.ceil(
        (h[st] || b[st]) / ((h[sh] || b[sh]) - h.clientHeight) * 100
      );
    }
  }
  _endpoint = new WeakMap();
  _startTimestamp = new WeakMap();
  _initialized = new WeakMap();
  _cookieName = new WeakMap();
  _cookieExpDays = new WeakMap();
  _debug = new WeakMap();
  _getCookie = new WeakSet();
  getCookie_fn = function(cookieName) {
    let name = cookieName + "=";
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(";");
    for (let i = 0; i < ca.length; i++) {
      let c = ca[i];
      while (c.charAt(0) == " ") {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  };
  _setCookie = new WeakSet();
  setCookie_fn = function(cookieName, cookieValue, expirationDays) {
    var d = /* @__PURE__ */ new Date();
    d.setTime(d.getTime() + expirationDays * 24 * 60 * 60 * 1e3);
    var expires = "expires=" + d.toUTCString();
    document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/";
  };
  _sendXhr = new WeakSet();
  sendXhr_fn = function(data) {
    const self = this;
    const xhr = new XMLHttpRequest();
    xhr.open("POST", __privateGet(this, _endpoint), true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.setRequestHeader("Accept", "application/json");
    xhr.onreadystatechange = function() {
      if (xhr.readyState === XMLHttpRequest.DONE) {
        if (xhr.status === 200) {
          __privateGet(self, _debug) && console.log("PPA: Data sent");
          __privateGet(self, _debug) && console.table(data);
        } else {
          __privateGet(self, _debug) && console.error("PPA: Error sending data", data);
        }
      }
    };
    __privateGet(this, _debug) && console.table(data);
    return xhr.send(JSON.stringify(data));
  };
  _sendBeacon = new WeakSet();
  sendBeacon_fn = function(data) {
    __privateGet(this, _debug) && console.table(data);
    let blob = new Blob([JSON.stringify(data)], { type: "application/json" });
    return navigator.sendBeacon(__privateGet(this, _endpoint), blob);
  };
  _addListeners = new WeakSet();
  addListeners_fn = function() {
    window.addEventListener("beforeunload", (_event) => {
      this.send([
        __spreadProps(__spreadValues({}, this.data), {
          type: "viewed",
          time_on_page: this.getTimeOnPage(),
          scroll_percentage: this.getScrollPercentage()
        })
      ]);
    });
  };
  if (!window.ppAnalytics) {
    window.ppAnalytics = new PlugAndPayAnalytics();
  }
})();
