import { z } from "zod";
import { startSpan, captureException } from "@sentry/nextjs";
import { l as logger } from "./index-Cw1zbw54.js";
const intervalLimitsType = z.object({
  PER_DAY: z.number().optional(),
  PER_WEEK: z.number().optional(),
  PER_MONTH: z.number().optional(),
  PER_YEAR: z.number().optional()
}).nullable();
const SENSITIVE_FIELDS = [
  "accessToken",
  "api_key",
  "apiKey",
  "auth",
  "authorization",
  "client_id",
  "client_secret",
  "credential",
  "hash",
  "key",
  "password",
  "refreshToken",
  "secret",
  "token"
];
const SENSITIVE_FIELDS_SET = new Set(SENSITIVE_FIELDS);
function createReplacer() {
  const seen = /* @__PURE__ */ new WeakSet();
  return function replacer(key, value) {
    if (value && typeof value === "object") {
      if (seen.has(value)) {
        return "[Circular]";
      }
      seen.add(value);
    }
    if (SENSITIVE_FIELDS_SET.has(key)) {
      return "[REDACTED]";
    }
    if (value instanceof Error) {
      const errorObj = {
        name: value.name,
        message: value.message
      };
      if (value.stack) {
        errorObj.stack = value.stack;
      }
      if ("cause" in value && value.cause !== void 0) {
        errorObj.cause = value.cause;
      }
      return errorObj;
    }
    return value;
  };
}
function redactSensitiveData(data) {
  if (!data || typeof data !== "object")
    return data;
  try {
    const replacer = createReplacer();
    const jsonString = JSON.stringify(data, replacer);
    return JSON.parse(jsonString);
  } catch (error) {
    logger.error("Failed to redact sensitive data", error);
    return { error: "Redaction failed" };
  }
}
function isPromise(value) {
  return value instanceof Promise;
}
function createErrorHandler(name, args) {
  return (error) => {
    const context = {
      functionName: name,
      args: redactSensitiveData(args)
    };
    if (error instanceof Error) {
      error.context = context;
    }
    captureException(error, {
      extra: context,
      tags: { functionName: name }
    });
    throw error;
  };
}
function withReporting(func, name) {
  if (!name?.trim()) {
    throw new Error("withReporting requires a non-empty name parameter");
  }
  if (!process.env.NEXT_PUBLIC_SENTRY_DSN || !process.env.SENTRY_TRACES_SAMPLE_RATE) {
    return func;
  }
  return (...args) => {
    return startSpan({ name }, () => {
      const onError = createErrorHandler(name, args);
      try {
        const result = func(...args);
        if (isPromise(result)) {
          return result.catch(onError);
        }
        return result;
      } catch (error) {
        onError(error);
        throw error;
      }
    });
  };
}
export {
  intervalLimitsType as i,
  withReporting as w
};
