// @bun
// src/logs/filter-tokens.ts
var VALID_LEVEL_KEYWORDS = "error, warning, info";
var VALID_KV_KEYS = "since, limit";
var DURATION_HINT = "Use number + unit: 30s, 5m, 1h, 2d, 1w";
var POSITIVE_INT_HINT = "Must be a positive integer";
var DURATION_UNITS = {
  s: 1000,
  m: 60000,
  h: 3600000,
  d: 86400000,
  w: 604800000
};
function parseDuration(s) {
  const match = s.match(/^(\d+(?:\.\d+)?)\s*([smhdw])$/);
  if (!match)
    return null;
  const value = parseFloat(match[1]);
  const unit = match[2];
  const multiplier = DURATION_UNITS[unit];
  if (!multiplier || value <= 0)
    return null;
  return Math.round(value * multiplier);
}
function parseFilterTokens(tokens) {
  const opts = {};
  const errors = [];
  for (const token of tokens) {
    const lower = token.toLowerCase();
    if (lower === "error" || lower === "errors") {
      opts.level = "error";
      continue;
    }
    if (lower === "warning" || lower === "warnings" || lower === "warn") {
      opts.level = "warning";
      continue;
    }
    if (lower === "info") {
      opts.level = "info";
      continue;
    }
    const eqIdx = token.indexOf("=");
    if (eqIdx === -1) {
      errors.push({
        token,
        message: `Unknown filter \`${token}\``,
        suggestion: `Valid filters: ${VALID_LEVEL_KEYWORDS}, since=, limit=`
      });
      continue;
    }
    const key = token.slice(0, eqIdx).toLowerCase();
    const val = token.slice(eqIdx + 1);
    if (key === "since") {
      const ms = parseDuration(val);
      if (ms !== null) {
        opts.since = Date.now() - ms;
      } else {
        errors.push({
          token,
          message: `Invalid duration \`${val}\` for \`since\``,
          suggestion: DURATION_HINT
        });
      }
      continue;
    }
    if (key === "limit") {
      const n = parseInt(val, 10);
      if (!isNaN(n) && n > 0) {
        opts.limit = n;
      } else {
        errors.push({
          token,
          message: `Invalid value \`${val}\` for \`limit\``,
          suggestion: POSITIVE_INT_HINT
        });
      }
      continue;
    }
    errors.push({
      token,
      message: `Unknown filter key \`${key}\``,
      suggestion: `Valid keys: ${VALID_KV_KEYS}`
    });
  }
  return { options: opts, errors };
}

export { parseDuration, parseFilterTokens };
