<?php
namespace Grav\Plugin\Shortcodes;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class PullQuoteShortcode extends Shortcode
{
    public function init(): void
    {
        $this->shortcode->getHandlers()->add('pull-quote', function (ShortcodeInterface $sc) {
            $plugin    = $this->grav['plugins']->getPlugin('story-blocks');
            $page      = $plugin ? $plugin->getCurrentPage() : null;

            $imageName = htmlspecialchars($sc->getParameter('image', ''), ENT_QUOTES);
            $alt       = htmlspecialchars($sc->getParameter('alt', ''), ENT_QUOTES);
            $content   = trim($sc->getContent()); // ShortcodeCore renders inner Markdown to HTML; trusted author content
            $imageUrl  = ($page && $imageName) ? $page->url() . '/' . $imageName : '';

            $bgHtml = '';
            if ($imageUrl) {
                $bgHtml = <<<HTML
  <div class="pull-quote__bg" aria-hidden="true">
    <img src="{$imageUrl}" alt="{$alt}" class="pull-quote__bg-img" loading="lazy">
    <div class="pull-quote__bg-tint"></div>
  </div>
HTML;
            }

            $innerClass = $imageUrl ? 'pull-quote__inner' : 'pull-quote__inner pull-quote__inner--no-image';

            return <<<HTML
<blockquote class="pull-quote" aria-label="Pull quote">
{$bgHtml}
  <div class="{$innerClass}">
    <span class="pull-quote__mark" aria-hidden="true">"</span>
    <p class="pull-quote__text">{$content}</p>
    <span class="pull-quote__mark pull-quote__mark--close" aria-hidden="true">"</span>
  </div>
</blockquote>
HTML;
        });
    }
}
