<?php
namespace Grav\Plugin\Shortcodes;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class ScrollySectionShortcode extends Shortcode
{
    public function init(): void
    {
        $this->shortcode->getHandlers()->add('scrolly-section', 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);
            $caption   = htmlspecialchars($sc->getParameter('caption', ''), ENT_QUOTES);
            $content   = $sc->getContent(); // ShortcodeCore renders inner Markdown to HTML; trusted author content
            $imageUrl  = ($page && $imageName) ? $page->url() . '/' . $imageName : $imageName;

            $captionHtml = $caption
                ? '<p class="scrolly__caption">' . $caption . '</p>'
                : '';

            return <<<HTML
<div class="scrolly">
  <div class="scrolly__media" aria-hidden="true">
    <div class="scrolly__media-inner">
      <img src="{$imageUrl}" alt="{$alt}" class="scrolly__img" loading="lazy">
      <div class="scrolly__img-overlay"></div>
    </div>
    {$captionHtml}
  </div>
  <div class="scrolly__steps">
    <div class="scrolly__steps-content">
      {$content}
    </div>
  </div>
</div>
HTML;
        });
    }
}
