export function pruneFields(
  fields: Record<string, unknown>,
  localizedTextKeys: Set<string>,
  defaultLang: string,
): Record<string, unknown> {
  const out: Record<string, unknown> = {};

  for (const [key, value] of Object.entries(fields)) {
    if (value === undefined || value === null || value === "") continue;

    if (typeof value === "object" && !Array.isArray(value)) {
      const map = value as Record<string, unknown>;
      // Single-language authoring: a localized_text value keeps only the default lang.
      const entries = localizedTextKeys.has(key)
        ? Object.entries(map).filter(([lang]) => lang === defaultLang)
        : Object.entries(map);

      const inner = Object.fromEntries(
        entries.filter(([, v]) => v !== undefined && v !== null && v !== ""),
      );

      if (Object.keys(inner).length > 0) out[key] = inner;

      continue;
    }

    out[key] = value;
  }

  return out;
}
