// cta — a call-to-action button. `text` is the label, `url` the destination
// (same-origin path or http/https), `style` picks primary/secondary appearance.
const { registerBlock } = require('../registry')
const { isSafeUrl, oneOf, requiredText, onlyKeys } = require('../propHelpers')

const STYLES = ['primary', 'secondary']
const MAX_TEXT = 100

registerBlock({
  type: 'cta',
  version: 1,
  cacheTTL: null,
  schema(props) {
    const errors = onlyKeys(props, ['text', 'url', 'style'])
    const text = requiredText('text', props.text, MAX_TEXT)
    if (text) errors.push(text)
    if (!isSafeUrl(props.url)) errors.push('url must be a same-origin path or http(s) URL')
    const style = oneOf('style', STYLES)(props.style)
    if (style) errors.push(style)
    return errors
  },
})
