import { __ } from '@wordpress/i18n';
import { RichText } from '@wordpress/block-editor';

const URL_TAGS = ['post_permalink', 'comments_url'];

export function BodycopyContent({ isDynamic, content, selectedTag, dynamicContent, setAttributes }) {
  if (!isDynamic) {
    return (
      <RichText
        tagName="span"
        value={content}
        onChange={(value) => setAttributes({ content: value })}
        placeholder={__('Enter body copy…', 'axcelersblocks')}
        className="axcelersblocks-bodycopy__content"
        allowedFormats={['core/bold', 'core/italic', 'core/link']}
      />
    );
  }

  // URL tags output a raw URL — show a descriptive chip instead
  if (URL_TAGS.includes(selectedTag)) {
    const label = selectedTag === 'post_permalink'
      ? __('Post Link', 'axcelersblocks')
      : __('Comments URL', 'axcelersblocks');
    return <span className="axcelersblocks-dynamic-chip axcelersblocks-dynamic-chip--link">{label}</span>;
  }

  // Bodycopy may receive HTML (excerpt, term description, ACF WYSIWYG)
  if (dynamicContent) {
    return <span className="axcelersblocks-dynamic-preview" dangerouslySetInnerHTML={{ __html: dynamicContent }} />;
  }

  return <span className="axcelersblocks-dynamic-chip">{selectedTag || 'dynamic'}</span>;
}
