"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var http_exports = {};
__export(http_exports, {
  http: () => http
});
module.exports = __toCommonJS(http_exports);
var import_logLevel = require("../logLevel.js");
var import_fetch_retry = require("./fetch-retry.js");
var import_logrocket = require("./logrocket.js");
const http = (url) => {
  const logs = [];
  return {
    log: async (msg, logLevel) => {
      if (logLevel === import_logLevel.LogLevels.ERROR || logLevel === import_logLevel.LogLevels.FATAL)
        import_logrocket.logRocket.error(msg);
      const sendLogs = async (logMsg) => {
        if (window && window.navigator && navigator.sendBeacon) {
          try {
            return navigator.sendBeacon(url, JSON.stringify(logMsg));
          } catch (err) {
            console.error(err);
            return true;
          }
        }
        return (0, import_fetch_retry.fetchRetry)({
          url,
          options: {
            method: "POST",
            headers: {
              "Content-Type": "text/plain"
            },
            keepalive: true,
            body: JSON.stringify(logMsg)
          },
          retries: 3
        }).catch(console.error);
      };
      if (logLevel !== import_logLevel.LogLevels.ERROR && logLevel !== import_logLevel.LogLevels.WARN && logLevel !== import_logLevel.LogLevels.FATAL) {
        if (logs.length === 0) {
          window.addEventListener("beforeunload", () => {
            sendLogs(logs).catch(console.error);
          });
          window.addEventListener("unload", () => {
            sendLogs(logs).catch(console.error);
          });
          const handle = setInterval(() => {
            clearInterval(handle);
            if (logs.length === 0) return;
            sendLogs(logs).catch(console.error);
            logs.length = 0;
          }, 1e4);
        }
        logs.push(msg);
      } else {
        await sendLogs(msg);
      }
      return true;
    }
  };
};
