import { parseBooleanWithDefault } from "@open-mercato/shared/lib/boolean";
import { parseNumberWithDefault } from "@open-mercato/shared/lib/number";
import { parseCommaSeparatedList } from "@open-mercato/shared/lib/string";
const DEFAULT_SEARCH_MIN_TOKEN_LENGTH = 3;
const DEFAULT_SEARCH_MAX_FIELD_CHARS = 2e4;
const DEFAULT_SEARCH_MAX_TOKENS_PER_FIELD = 5e3;
const DEFAULT_SEARCH_MAX_TOKENS_PER_RECORD = 2e4;
const DEFAULT_BLOCKLIST = ["password", "token", "secret", "hash"];
const ENTITY_BLOCKLIST_SEPARATOR = "@";
function parseBoolean(raw, fallback) {
  return parseBooleanWithDefault(raw, fallback);
}
function parseNumber(raw, fallback, min = 1) {
  return parseNumberWithDefault(raw, fallback, { integer: true, min });
}
function resolveSearchTokenLimits(config) {
  const resolveLimit = (value, fallback) => {
    if (value === void 0) return fallback;
    if (!Number.isFinite(value) || value < 0) return fallback;
    return Math.trunc(value);
  };
  return {
    maxFieldChars: resolveLimit(config.maxFieldChars, DEFAULT_SEARCH_MAX_FIELD_CHARS),
    maxTokensPerField: resolveLimit(config.maxTokensPerField, DEFAULT_SEARCH_MAX_TOKENS_PER_FIELD),
    maxTokensPerRecord: resolveLimit(config.maxTokensPerRecord, DEFAULT_SEARCH_MAX_TOKENS_PER_RECORD)
  };
}
function parseHashAlgorithm(raw) {
  const value = (raw ?? "").trim().toLowerCase();
  if (value === "sha1") return "sha1";
  if (value === "md5") return "md5";
  return "sha256";
}
function parseFieldBlocklist(raw) {
  const global = [];
  const byEntity = /* @__PURE__ */ new Map();
  for (const rawEntry of parseCommaSeparatedList(raw)) {
    const entry = rawEntry.toLowerCase();
    const separatorIndex = entry.indexOf(ENTITY_BLOCKLIST_SEPARATOR);
    const entityType = separatorIndex >= 0 ? entry.slice(0, separatorIndex).trim() : "";
    const field = separatorIndex >= 0 ? entry.slice(separatorIndex + 1).trim() : entry;
    if (!field.length) continue;
    if (!entityType.length) {
      if (!global.includes(field)) global.push(field);
      continue;
    }
    const scoped = byEntity.get(entityType) ?? [];
    if (!scoped.includes(field)) scoped.push(field);
    byEntity.set(entityType, scoped);
  }
  for (const fallback of DEFAULT_BLOCKLIST) {
    if (!global.includes(fallback)) global.push(fallback);
  }
  const scopedBlocklist = /* @__PURE__ */ Object.create(null);
  for (const [entityType, fields] of byEntity) scopedBlocklist[entityType] = fields;
  return { global, byEntity: scopedBlocklist };
}
function resolveSearchConfig() {
  const blocklist = parseFieldBlocklist(process.env.OM_SEARCH_FIELD_BLOCKLIST);
  return {
    enabled: parseBoolean(process.env.OM_SEARCH_ENABLED, true),
    minTokenLength: resolveSearchMinTokenLength(),
    enablePartials: parseBoolean(process.env.OM_SEARCH_ENABLE_PARTIAL, true),
    hashAlgorithm: parseHashAlgorithm(process.env.OM_SEARCH_HASH_ALGO),
    storeRawTokens: parseBoolean(process.env.OM_SEARCH_STORE_RAW_TOKENS, false),
    useIlikeForNonEncryptedFields: parseBoolean(process.env.OM_SEARCH_USE_ILIKE_FOR_NON_ENCRYPTED_FIELDS, false),
    blocklistedFields: blocklist.global,
    entityBlocklistedFields: blocklist.byEntity,
    maxFieldChars: parseNumber(process.env.OM_SEARCH_MAX_FIELD_CHARS, DEFAULT_SEARCH_MAX_FIELD_CHARS, 0),
    maxTokensPerField: parseNumber(process.env.OM_SEARCH_MAX_TOKENS_PER_FIELD, DEFAULT_SEARCH_MAX_TOKENS_PER_FIELD, 0),
    maxTokensPerRecord: parseNumber(process.env.OM_SEARCH_MAX_TOKENS_PER_RECORD, DEFAULT_SEARCH_MAX_TOKENS_PER_RECORD, 0)
  };
}
function isSearchFieldBlocklisted(field, entityType, config) {
  const lower = field.toLowerCase();
  if (config.blocklistedFields.some((blocked) => lower.includes(blocked))) return true;
  if (!entityType) return false;
  const scoped = config.entityBlocklistedFields?.[entityType.trim().toLowerCase()];
  if (!Array.isArray(scoped) || !scoped.length) return false;
  return scoped.some((blocked) => lower.includes(blocked));
}
function resolveSearchMinTokenLength() {
  return parseNumber(process.env.OM_SEARCH_MIN_LEN, DEFAULT_SEARCH_MIN_TOKEN_LENGTH, 1);
}
export {
  DEFAULT_SEARCH_MAX_FIELD_CHARS,
  DEFAULT_SEARCH_MAX_TOKENS_PER_FIELD,
  DEFAULT_SEARCH_MAX_TOKENS_PER_RECORD,
  DEFAULT_SEARCH_MIN_TOKEN_LENGTH,
  isSearchFieldBlocklisted,
  resolveSearchConfig,
  resolveSearchMinTokenLength,
  resolveSearchTokenLimits
};
//# sourceMappingURL=config.js.map
