export class PixelioCartAbandonRecoveryMoreEventTracker {
    TRACKING_ACTION = "pixelio_track_event_data";

    constructor() {}

    track(eventType = "page_engagement", payload = {}, useBeacon = false) {
        if (typeof pixelio_ajax === "undefined") {
            return;
        }

        const data = new FormData();

        data.append("action", this.TRACKING_ACTION);
        data.append("nonce", pixelio_ajax.pixelio_ajax_nonce || "");
        data.append("event_type", eventType);
        data.append("product_id", payload.productId || pixelio_ajax.productId || 0);
        data.append("parent_product_id", payload.parentProductId || pixelio_ajax.parentProductId || 0);
        data.append("page_type", payload.pageType || pixelio_ajax.pageType || "page");
        data.append("visit_duration", payload.visitDuration || 0);
        data.append("scroll_percent", payload.scrollPercent || 0);

        if (pixelio_ajax.hasConsent === true) {
            data.append("page_url", payload.pageUrl || window.location.href);
        }

        if (useBeacon && navigator.sendBeacon) {
            navigator.sendBeacon(pixelio_ajax.pixelio_ajax_url, data);
            return;
        }

        fetch(pixelio_ajax.pixelio_ajax_url, {
            method: "POST",
            body: data,
            credentials: "same-origin",
            keepalive: true
        }).catch(() => {});
    }
}
