(function () {

  console.log("VendorGet injected bridge loaded:", window.location.href);

  if (typeof window.__tcfapi !== "function") {
    console.log("VendorGet: __tcfapi not found");
    return;
  }

  console.log("VendorGet: __tcfapi found");

  function emitToContentScript(eventName, payload) {
    window.dispatchEvent(new CustomEvent("VendorGetFromPage", {
      detail: {
        eventName: eventName,
        payload: payload
      }
    }));
  }

  function cloneSerializable(value, seen) {
    if (value === undefined) {
      return null;
    }

    if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
      return value;
    }

    if (typeof value === "function" || typeof value === "symbol") {
      return undefined;
    }

    if (typeof value !== "object") {
      return undefined;
    }

    const seenValues = seen || new WeakSet();

    if (seenValues.has(value)) {
      return "[Circular]";
    }

    seenValues.add(value);

    if (Array.isArray(value)) {
      return value.map(function (item) {
        const clonedItem = cloneSerializable(item, seenValues);
        return clonedItem === undefined ? null : clonedItem;
      });
    }

    if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) {
      return undefined;
    }

    const result = {};

    Object.keys(value).forEach(function (key) {
      let propertyValue;

      try {
        propertyValue = value[key];
      } catch (error) {
        return;
      }

      const clonedValue = cloneSerializable(propertyValue, seenValues);

      if (clonedValue !== undefined) {
        result[key] = clonedValue;
      }
    });

    return result;
  }

  window.__tcfapi("ping", 2, function (pingData, pingSuccess) {

    console.log("VendorGet __tcfapi ping:", {
      success: pingSuccess,
      data: pingData
    });

    emitToContentScript("tcf_ping", {
      success: pingSuccess,
      data: pingData
    });

  });

  window.__tcfapi("addEventListener", 2, function (tcData, success) {

    if (!success || !tcData) {
      return;
    }

    console.log("VendorGet raw event:", tcData);

    if (tcData.eventStatus === "useractioncomplete") {

      const capture = {
        url: window.location.href,
        origin: window.location.origin,

        timestampUtc: new Date().toISOString(),

        cmpId: tcData.cmpId,
        cmpVersion: tcData.cmpVersion,

        gdprApplies: tcData.gdprApplies,

        tcfPolicyVersion: tcData.tcfPolicyVersion,
        vendorListVersion: tcData.vendorListVersion,

        tcString: tcData.tcString,

        eventStatus: tcData.eventStatus,
        cmpStatus: tcData.cmpStatus,

        isServiceSpecific: tcData.isServiceSpecific,
        useNonStandardTexts: tcData.useNonStandardTexts,

        publisherCC: tcData.publisherCC,
        purposeOneTreatment: tcData.purposeOneTreatment,

        purpose: tcData.purpose,
        vendor: tcData.vendor,

        specialFeatureOptins: tcData.specialFeatureOptins,

        publisher: tcData.publisher,

        addtlConsent: tcData.addtlConsent,

        rawTcData: cloneSerializable(tcData)
      };

      console.log("VendorGet CONSENT CAPTURE:", capture);

      emitToContentScript("consent_capture", capture);
    }

  });

})();
