// rich_text — a block of HTML authored in the shared rich-text editor. Validated
// only for type/size here; the actual safety comes from `sanitize`, which runs
// the html through the same allowlist (cleanBody) used for posts/wiki bodies, so
// a direct API call can't smuggle unsafe markup past the editor.
const { registerBlock } = require('../registry')
const { onlyKeys } = require('../propHelpers')
const { cleanBody } = require('../../utils/sanitizeHtml')

const MAX_HTML = 50000

registerBlock({
  type: 'rich_text',
  version: 1,
  cacheTTL: null,
  schema(props) {
    const errors = onlyKeys(props, ['html'])
    if (typeof props.html !== 'string') errors.push('html must be a string')
    else if (props.html.length > MAX_HTML) {
      errors.push(`html must be at most ${MAX_HTML} characters`)
    }
    return errors
  },
  sanitize(props) {
    return { ...props, html: cleanBody(props.html) }
  },
})
