"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const packageDpScpvTA = require("./dist-package-DpScpvTA.cjs");
const Micro = require("../effect/dist-esm-Micro.cjs");
const index = require("../@uploadthing/shared/dist-index.cjs");
const Function = require("../effect/dist-esm-Function.cjs");
const createDeferred = () => {
  let resolve;
  let reject;
  const ac = new AbortController();
  const promise = new Promise((res, rej) => {
    resolve = res;
    reject = rej;
  });
  return {
    promise,
    ac,
    resolve,
    reject
  };
};
const randomHexString = /* @__PURE__ */ (function() {
  const characters = "abcdef0123456789";
  const charactersLength = 16;
  return function(length) {
    let result = "";
    for (let i = 0; i < length; i++) result += characters.charAt(Math.floor(Math.random() * charactersLength));
    return result;
  };
})();
const generateTraceHeaders = () => {
  const traceId = randomHexString(32);
  const spanId = randomHexString(16);
  const sampled = "01";
  return {
    b3: `${traceId}-${spanId}-${sampled}`,
    traceparent: `00-${traceId}-${spanId}-${sampled}`
  };
};
const createAPIRequestUrl = (config) => {
  const url = new URL(config.url);
  const queryParams = new URLSearchParams(url.search);
  queryParams.set("actionType", config.actionType);
  queryParams.set("slug", config.slug);
  url.search = queryParams.toString();
  return url;
};
const createUTReporter = (cfg) => (type, payload) => Micro.gen(function* () {
  const url = createAPIRequestUrl({
    url: cfg.url,
    slug: cfg.endpoint,
    actionType: type
  });
  const headers = new Headers(yield* Micro.promise(async () => typeof cfg.headers === "function" ? await cfg.headers() : cfg.headers));
  if (cfg.package) headers.set("x-uploadthing-package", cfg.package);
  headers.set("x-uploadthing-version", packageDpScpvTA.version);
  headers.set("Content-Type", "application/json");
  headers.set("b3", cfg.traceHeaders.b3);
  headers.set("traceparent", cfg.traceHeaders.traceparent);
  const response = yield* index.fetchEff(url, {
    method: "POST",
    body: JSON.stringify(payload),
    headers
  }).pipe(
    Micro.andThen(index.parseResponseJson),
    /**
    * We don't _need_ to validate the response here, just cast it for now.
    * As of now, @effect/schema includes quite a few bytes we cut out by this...
    * We have "strong typing" on the backend that ensures the shape should match.
    */
    Micro.map(Function.unsafeCoerce),
    Micro.catchTag("FetchError", (e) => Micro.fail(new index.UploadThingError({
      code: "INTERNAL_CLIENT_ERROR",
      message: `Failed to report event "${type}" to UploadThing server`,
      cause: e
    }))),
    Micro.catchTag("BadRequestError", (e) => Micro.fail(new index.UploadThingError({
      code: index.getErrorTypeFromStatusCode(e.status),
      message: e.getMessage(),
      cause: e.json
    }))),
    Micro.catchTag("InvalidJson", (e) => Micro.fail(new index.UploadThingError({
      code: "INTERNAL_CLIENT_ERROR",
      message: "Failed to parse response from UploadThing server",
      cause: e
    })))
  );
  return response;
});
exports.createDeferred = createDeferred;
exports.createUTReporter = createUTReporter;
exports.generateTraceHeaders = generateTraceHeaders;
//# sourceMappingURL=dist-ut-reporter-Dlppchbx.cjs.map
