var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { schema } from "./validator";
export const trackingFn = (requests) => (params) => __awaiter(void 0, void 0, void 0, function* () {
    const validated = yield schema.validate(params);
    const { event_type: eventType, isTestEvent, event_version: eventVersion, meta, attributes, } = validated;
    try {
        return yield requests.post("/record", {
            params: {
                event_type: eventType,
                event_version: eventVersion,
                isTestEvent,
                meta,
                attributes,
            },
        });
    }
    catch (err) {
        if (err instanceof TypeError &&
            (err.message.toLowerCase().includes("networkerror") ||
                err.message.toLowerCase().includes("load failed") ||
                err.message.toLowerCase().includes("failed to fetch"))) {
            // This likely happens when an adblocker blocks the tracking request, drop the error to prevent
            // Sentry pollution.
            return undefined;
        }
        if ((err instanceof DOMException &&
            err.message.toLowerCase().includes("aborted")) ||
            (err instanceof TypeError &&
                err.message.toLowerCase().includes("cancelled")) ||
            (err instanceof TypeError &&
                err.message.toLowerCase().includes("annulé"))) {
            // This likely happens when a requests times out or is cancelled on the client, drop the error to prevent
            // Sentry pollution.
            return undefined;
        }
        throw err;
    }
});
//# sourceMappingURL=index.js.map