export async function enrichHTMLFields(data, schemaFields) {
  const enrichedFields = {}

  for (const [key, field] of Object.entries(schemaFields ?? {})) {
    if (field instanceof foundry.data.fields.HTMLField) {
      enrichedFields[key] = await foundry.applications.ux.TextEditor.implementation.enrichHTML(data?.[key] ?? "", { async: true })
      continue
    }

    if (field instanceof foundry.data.fields.SchemaField) {
      const nested = await enrichHTMLFields(data?.[key], field.fields)
      if (Object.keys(nested).length) enrichedFields[key] = nested
    }
  }

  return enrichedFields
}
