import { __name } from './chunk-IBAHCH6O.js';
import { format } from 'util';

// src/lib/duration.ts
var MS_UNITS = {
  ms: 1,
  millis: 1,
  millisecond: 1,
  milliseconds: 1,
  s: 1e3,
  sec: 1e3,
  secs: 1e3,
  second: 1e3,
  seconds: 1e3,
  m: 6e4,
  min: 6e4,
  mins: 6e4,
  minute: 6e4,
  minutes: 6e4,
  h: 36e5,
  hr: 36e5,
  hrs: 36e5,
  hour: 36e5,
  hours: 36e5,
  d: 864e5,
  day: 864e5,
  days: 864e5,
  w: 6048e5,
  wk: 6048e5,
  wks: 6048e5,
  week: 6048e5,
  weeks: 6048e5
};
var UNIT_TO_MS = {
  ms: 1,
  seconds: 1e3,
  minutes: 6e4,
  hours: 36e5,
  days: 864e5,
  weeks: 6048e5
};
function parseDuration(input) {
  let ms;
  if (typeof input === "number") {
    if (!Number.isFinite(input) || input < 0) {
      throw new Error(`Invalid duration: ${input} \u2014 must be a non-negative finite number`);
    }
    ms = input;
  } else {
    const trimmed = input.trim();
    if (!trimmed) throw new Error("Duration string cannot be empty");
    if (/^\d+(?:\.\d+)?$/.test(trimmed)) {
      ms = parseFloat(trimmed);
    } else {
      const match = trimmed.match(/^(\d+(?:\.\d+)?)\s*([a-z]+)$/i);
      if (!match) {
        throw new Error(
          `Invalid duration: "${input}" \u2014 expected a number with a unit suffix (e.g. "1h", "30m", "500ms", "2days")`
        );
      }
      const value = parseFloat(match[1]);
      const unit = match[2].toLowerCase();
      const multiplier = MS_UNITS[unit];
      if (!multiplier) {
        throw new Error(
          `Invalid duration unit: "${match[2]}" \u2014 valid units: ms, s, m, h, d, w (and long forms / plurals)`
        );
      }
      ms = value * multiplier;
    }
  }
  const rounded = Math.round(ms);
  if (ms > 0 && rounded === 0) {
    throw new Error(`Invalid duration: "${input}" \u2014 durations under 1ms are not supported`);
  }
  return rounded;
}
__name(parseDuration, "parseDuration");
function convertDurationFromMs(ms, unit) {
  return ms / UNIT_TO_MS[unit];
}
__name(convertDurationFromMs, "convertDurationFromMs");

// src/lib/cache/ttl-parser.ts
var TTL_FOREVER = Infinity;
function parseTtl(ttl) {
  if (typeof ttl === "string" && ttl.trim().toLowerCase() === "forever") {
    return TTL_FOREVER;
  }
  const ms = parseDuration(ttl);
  if (ms === 0) {
    throw new Error(
      'Cache TTL of 0 is ambiguous \u2014 use "forever" to cache until manually cleared, or false to disable caching'
    );
  }
  return ms;
}
__name(parseTtl, "parseTtl");
var prevTime = /* @__PURE__ */ new Map();
var colors = [
  "\x1B[36m",
  // cyan
  "\x1B[35m",
  // magenta
  "\x1B[34m",
  // blue
  "\x1B[33m",
  // yellow
  "\x1B[32m",
  // green
  "\x1B[31m",
  // red
  "\x1B[96m",
  // bright cyan
  "\x1B[95m",
  // bright magenta
  "\x1B[94m",
  // bright blue
  "\x1B[93m",
  // bright yellow
  "\x1B[92m",
  // bright green
  "\x1B[91m"
  // bright red
];
var resetCode = "\x1B[0m";
var grayCode = "\x1B[90m";
var selectColor = /* @__PURE__ */ __name((namespace) => {
  let hash = 0;
  for (let i = 0; i < namespace.length; i++) {
    hash = (hash << 5) - hash + namespace.charCodeAt(i);
    hash |= 0;
  }
  return colors[Math.abs(hash) % colors.length];
}, "selectColor");
var humanizeMs = /* @__PURE__ */ __name((ms) => {
  if (ms >= 1e3) {
    return `${(ms / 1e3).toFixed(1)}s`;
  }
  return `${Math.round(ms)}ms`;
}, "humanizeMs");
var isEnabled = /* @__PURE__ */ __name((namespace) => {
  const debugEnv = process.env.DEBUG || "";
  if (!debugEnv) return false;
  const patterns = debugEnv.split(",").map((p) => p.trim());
  let enabled = false;
  for (const pattern of patterns) {
    if (pattern.startsWith("-")) {
      const excludePattern = pattern.slice(1);
      if (excludePattern === namespace) return false;
      if (excludePattern.endsWith("*")) {
        const prefix = excludePattern.slice(0, -1);
        if (namespace.startsWith(prefix)) return false;
      }
      continue;
    }
    if (pattern === "*") {
      enabled = true;
    } else if (pattern === namespace) {
      enabled = true;
    } else if (pattern.endsWith("*")) {
      const prefix = pattern.slice(0, -1);
      if (namespace.startsWith(prefix)) {
        enabled = true;
      }
    }
  }
  return enabled;
}, "isEnabled");
function createDebug(namespace) {
  const enabled = isEnabled(namespace);
  if (!enabled) {
    return () => {
    };
  }
  const colorCode = selectColor(namespace);
  return (...args) => {
    const now = Date.now();
    const prev = prevTime.get(namespace);
    const diff = prev ? now - prev : 0;
    prevTime.set(namespace, now);
    const msg = format(...args);
    const diffStr = diff > 0 ? `${grayCode} +${humanizeMs(diff)}${resetCode}` : "";
    const output = `${colorCode}${namespace}${resetCode} ${msg}${diffStr}`;
    console.error(output);
  };
}
__name(createDebug, "createDebug");

// src/lib/cache/resolve-cache-ttl.ts
async function resolveCacheTtl(cacheTtlResolver) {
  if (!cacheTtlResolver) return void 0;
  const cacheTtl = await cacheTtlResolver.resolve();
  if (cacheTtl === void 0 || cacheTtl === null || cacheTtl === false || cacheTtl === "") {
    return void 0;
  }
  if (typeof cacheTtl !== "string" && typeof cacheTtl !== "number") {
    const err = new Error(`cacheTtl resolved to an invalid type (${typeof cacheTtl})`);
    err.tip = 'cacheTtl should resolve to a duration string like "1h", "forever" (until manually cleared), or false to disable caching';
    throw err;
  }
  parseTtl(cacheTtl);
  return cacheTtl;
}
__name(resolveCacheTtl, "resolveCacheTtl");

export { TTL_FOREVER, convertDurationFromMs, createDebug, parseDuration, parseTtl, resolveCacheTtl };
//# sourceMappingURL=chunk-QGM3LRUO.js.map
//# sourceMappingURL=chunk-QGM3LRUO.js.map