import { version } from "./dist-package-DpScpvTA.js";
import { gen, promise, andThen, map, catchTag, fail } from "../effect/dist-esm-Micro.js";
import { fetchEff, parseResponseJson, UploadThingError, getErrorTypeFromStatusCode } from "../@uploadthing/shared/dist-index.js";
import { unsafeCoerce } from "../effect/dist-esm-Function.js";
const createDeferred = () => {
  let resolve;
  let reject;
  const ac = new AbortController();
  const promise2 = new Promise((res, rej) => {
    resolve = res;
    reject = rej;
  });
  return {
    promise: promise2,
    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) => gen(function* () {
  const url = createAPIRequestUrl({
    url: cfg.url,
    slug: cfg.endpoint,
    actionType: type
  });
  const headers = new Headers(yield* 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", version);
  headers.set("Content-Type", "application/json");
  headers.set("b3", cfg.traceHeaders.b3);
  headers.set("traceparent", cfg.traceHeaders.traceparent);
  const response = yield* fetchEff(url, {
    method: "POST",
    body: JSON.stringify(payload),
    headers
  }).pipe(
    andThen(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.
    */
    map(unsafeCoerce),
    catchTag("FetchError", (e) => fail(new UploadThingError({
      code: "INTERNAL_CLIENT_ERROR",
      message: `Failed to report event "${type}" to UploadThing server`,
      cause: e
    }))),
    catchTag("BadRequestError", (e) => fail(new UploadThingError({
      code: getErrorTypeFromStatusCode(e.status),
      message: e.getMessage(),
      cause: e.json
    }))),
    catchTag("InvalidJson", (e) => fail(new UploadThingError({
      code: "INTERNAL_CLIENT_ERROR",
      message: "Failed to parse response from UploadThing server",
      cause: e
    })))
  );
  return response;
});
export {
  createDeferred,
  createUTReporter,
  generateTraceHeaders
};
//# sourceMappingURL=dist-ut-reporter-Dlppchbx.js.map
