const AGGREGATE_SEARCH_FIELD = "search_text";
function normalizeScopeValue(value) {
  if (value === void 0 || value === null || value === "") return null;
  return value;
}
function isScopedValueVisible(scopeValue, fieldValue) {
  if (scopeValue === null) return fieldValue === null;
  return fieldValue === null || fieldValue === scopeValue;
}
function normalizeValue(value) {
  if (value === void 0) return null;
  return value;
}
function collectAggregateSearchValues(field, value) {
  const lower = field.toLowerCase();
  if (lower === AGGREGATE_SEARCH_FIELD || lower === "id" || lower.endsWith("_id") || lower.endsWith(".id") || lower.endsWith("_at") || ["created_at", "updated_at", "deleted_at", "tenant_id", "organization_id"].includes(lower)) {
    return [];
  }
  if (typeof value === "string") {
    const trimmed = value.trim();
    return trimmed.length > 0 ? [trimmed] : [];
  }
  if (Array.isArray(value)) {
    return value.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter((entry) => entry.length > 0);
  }
  return [];
}
function attachAggregateSearchField(doc) {
  const parts = [];
  const seen = /* @__PURE__ */ new Set();
  for (const [field, value] of Object.entries(doc)) {
    const values = collectAggregateSearchValues(field, value);
    for (const entry of values) {
      const key = entry.toLowerCase();
      if (seen.has(key)) continue;
      seen.add(key);
      parts.push(entry);
    }
  }
  if (parts.length > 0) {
    doc[AGGREGATE_SEARCH_FIELD] = parts.join("\n");
  }
  return doc;
}
function buildIndexDocument(baseRow, customFieldValues = [], scope = {}) {
  const doc = {};
  for (const [key, value] of Object.entries(baseRow)) {
    doc[key] = value;
  }
  const scopeOrg = normalizeScopeValue(scope.organizationId ?? null);
  const scopeTenant = normalizeScopeValue(scope.tenantId ?? null);
  const grouped = /* @__PURE__ */ new Map();
  for (const field of customFieldValues) {
    const org = normalizeScopeValue(field.organizationId ?? null);
    const tenant = normalizeScopeValue(field.tenantId ?? null);
    if (!isScopedValueVisible(scopeOrg, org)) continue;
    if (!isScopedValueVisible(scopeTenant, tenant)) continue;
    const bucketKey = `cf:${field.key}`;
    let bucket = grouped.get(bucketKey);
    if (!bucket) {
      bucket = [];
      grouped.set(bucketKey, bucket);
    }
    const { value } = field;
    if (Array.isArray(value)) {
      for (const entry of value) bucket.push(normalizeValue(entry));
    } else {
      bucket.push(normalizeValue(value));
    }
  }
  for (const [key, values] of grouped.entries()) {
    if (values.length === 1) {
      doc[key] = values[0];
    } else if (values.length > 1) {
      doc[key] = values;
    }
  }
  return attachAggregateSearchField(doc);
}
export {
  AGGREGATE_SEARCH_FIELD,
  attachAggregateSearchField,
  buildIndexDocument
};
//# sourceMappingURL=document.js.map
