// image — a single image with optional caption. `src` must be a same-origin
// upload path or an http/https URL (isSafeUrl); `alignment` controls layout.
// Stays URL-based until the Wave 3 asset picker lands, then src swaps to an
// asset id via a small migration.
const { registerBlock } = require('../registry')
const { isSafeUrl, oneOf, optionalText, onlyKeys } = require('../propHelpers')

const ALIGNMENTS = ['left', 'center', 'right', 'full']
const MAX_ALT = 300
const MAX_CAPTION = 500

registerBlock({
  type: 'image',
  version: 1,
  cacheTTL: null,
  schema(props) {
    const errors = onlyKeys(props, ['src', 'alt', 'caption', 'alignment'])
    if (!isSafeUrl(props.src)) errors.push('src must be a same-origin path or http(s) URL')
    const alt = optionalText('alt', props.alt, MAX_ALT)
    if (alt) errors.push(alt)
    const caption = optionalText('caption', props.caption, MAX_CAPTION)
    if (caption) errors.push(caption)
    const alignment = oneOf('alignment', ALIGNMENTS)(props.alignment)
    if (alignment) errors.push(alignment)
    return errors
  },
})
