const MAX_IDS_PER_REQUEST = 200;
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function isUuid(value) {
  return typeof value === "string" && UUID_REGEX.test(value);
}
function normalizeIdList(values) {
  if (values.length === 0) return values;
  const deduped = /* @__PURE__ */ new Set();
  for (const value of values) {
    const trimmed = value.trim();
    if (!trimmed || !isUuid(trimmed)) continue;
    deduped.add(trimmed);
  }
  return Array.from(deduped);
}
function readExistingIds(filter) {
  if (typeof filter === "string") {
    return isUuid(filter) ? [filter] : null;
  }
  if (Array.isArray(filter)) {
    return normalizeIdList(
      filter.filter((value) => typeof value === "string")
    );
  }
  if (!filter || typeof filter !== "object") return null;
  const operators = filter;
  if (isUuid(operators.$eq)) return [operators.$eq];
  if (Array.isArray(operators.$in)) {
    return normalizeIdList(
      operators.$in.filter((value) => typeof value === "string")
    );
  }
  return null;
}
function parseIdsParam(raw, maxIds = MAX_IDS_PER_REQUEST) {
  if (typeof raw !== "string" || raw.trim().length === 0) return [];
  const safeMax = Number.isFinite(maxIds) && maxIds > 0 ? Math.floor(maxIds) : MAX_IDS_PER_REQUEST;
  const parsed = normalizeIdList(raw.split(","));
  return parsed.slice(0, safeMax);
}
function isIdsParamProvided(raw) {
  return typeof raw === "string" && raw.trim().length > 0;
}
function mergeIdFilter(existingFilters, parsedIds, options) {
  if (parsedIds.length === 0) {
    if (options?.idsParamProvided) {
      return { ...existingFilters, id: { $in: [] } };
    }
    return existingFilters;
  }
  const existingFilter = existingFilters.id;
  const existingIds = readExistingIds(existingFilter);
  if (!existingIds) {
    if (existingFilter === void 0 || existingFilter === null) {
      return { ...existingFilters, id: { $in: parsedIds } };
    }
    return existingFilters;
  }
  const allowed = new Set(parsedIds);
  const intersection = existingIds.filter((id) => allowed.has(id));
  return {
    ...existingFilters,
    id: { $in: intersection }
  };
}
export {
  MAX_IDS_PER_REQUEST,
  isIdsParamProvided,
  mergeIdFilter,
  parseIdsParam
};
//# sourceMappingURL=ids.js.map
