<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

use Magento\Catalog\Block\Category\View;
use Magento\Catalog\ViewModel\Category\Image;
use Magento\Catalog\ViewModel\Category\Output;
use Magento\Framework\Escaper;

/** @var View $block */
/** @var Escaper $escaper */

/** @var Image $image */
$image = $block->getImage();

/** @var Output $categoryOutput */
$categoryOutput = $block->getOutput();

$category = $block->getCurrentCategory();
$width = $block->getVar('category_page_category_image/width', 'Magento_Catalog') ?: 500;
$height = $block->getVar('category_page_category_image/height', 'Magento_Catalog') ?: 500;
$imgUrl = $image->getUrl($category);

if (!$imgUrl) {
    return '';
}

$imgHtml = '<img'
    . ' src="' . $escaper->escapeUrl($imgUrl) . '"'
    . ' alt="' . $escaper->escapeHtmlAttr($category->getName()) . '"'
    . ($width ? (' width="' . $escaper->escapeHtmlAttr($width) . '"') : '')
    . ($height ? (' height="' . $escaper->escapeHtmlAttr($height) . '"') : '')
    . ' loading="eager"'
    . '>';
?>

<div class="category-image">
    <?= /* @noEscape */ $categoryOutput->categoryAttribute($category, $imgHtml, 'image'); ?>
</div>
