<?php
declare(strict_types=1);

/** @var \Mirasvit\Brand\Block\Logo $block */
/** @var \Magento\Framework\Escaper $escaper */
/** @var \Hyva\Theme\Model\ViewModelRegistry $viewModels */

$brandUrl       = $block->getBrandUrl();
$title          = $block->getTitle();
$defaultLogoUrl = $block->getLogoImageUrl();
$imageWidth     = $block->getImageWidth(); // currently unused
$tooltipContent = $block->getTooltipContent();
$isProductPage  = $block->isProductPage();

// Use ViewModel to fetch product page logo
/** @var \Banyo\CustomProduct\ViewModel\BrandLogo $brandLogoViewModel */
$brandLogoViewModel = $viewModels->require(\Banyo\CustomProduct\ViewModel\BrandLogo::class);

$brandUrlParts = explode('/', rtrim($brandUrl, '/'));
$urlKey = end($brandUrlParts);
$productPageLogoUrl = $brandLogoViewModel->getProductPageLogoImageUrl($urlKey);
$logoUrlToShow = $isProductPage && $productPageLogoUrl ? $productPageLogoUrl : $defaultLogoUrl;

?>

<?php if ($logoUrlToShow): ?>
    <div class="bottom-0 flex flex-col-end items-center justify-center mb-0 mt-4 text-center" x-data="{show: false}">
        <a class="m-brand-tooltip relative float-left mr-2.5" href="<?= $escaper->escapeUrl($brandUrl) ?>" @mouseover="show = true" @mouseleave="show = false">
            <img
                src="<?= $escaper->escapeUrl($logoUrlToShow) ?>"
                alt="<?= $escaper->escapeHtmlAttr($title) ?>"
                title="<?= $escaper->escapeHtmlAttr($title) ?>"
            />
            <?php if ($tooltipContent): ?>
                <div class="mst_tooltip absolute w-80 p-2.5 border border-container rounded bottom-full mb-5 left-1/4 text-justify bg-white opacity-0 shadow-xl z-50 flex flex-wrap justify-center"
                     x-show="show"
                     :class="{'opacity-100': show}">
                    <?= $tooltipContent ?>
                    <div class="mst_tooltip__pointer absolute w-4 h-4 border-l border-b border-container -bottom-2 left-2.5 -rotate-45 bg-white"></div>
                </div>
            <?php endif; ?>
        </a>
        <?php if ($isProductPage): ?>
            <span><?= $escaper->escapeHtml($block->getBrandDescription()) ?></span>
        <?php endif ?>
    </div>
<?php endif ?>
