<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\Logo\LogoSizeResolver;
use Hyva\Theme\ViewModel\Logo\LogoPathResolver;
use Hyva\Theme\ViewModel\StoreConfig;
use Magento\Framework\Escaper;
use Magento\Theme\Block\Html\Header\Logo;

/** @var Logo $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

/** @var StoreConfig $storeConfig */
$storeConfig = $viewModels->require(StoreConfig::class);
$storeName = $block->getThemeName() ?: $storeConfig->getStoreConfig('general/store_information/name') ?: __('Store logo');

/** @var LogoSizeResolver $logoSizeResolver */
$logoSizeResolver = $viewModels->require(LogoSizeResolver::class);

$logoBaseWidth = $logoSizeResolver && $logoSizeResolver->getWidth()
    ? $logoSizeResolver->getWidth()
    : ($block->getLogoWidth() ?: 200);
$logoMaxWidth = $block->getMaxWidth() ?: $logoBaseWidth;
$logoWidth = $logoBaseWidth > $logoMaxWidth ? $logoMaxWidth : $logoBaseWidth;

$logoBaseHeight = $logoSizeResolver && $logoSizeResolver->getHeight()
    ? $logoSizeResolver->getHeight()
    : ($block->getLogoHeight() ?: 150);
$logoMaxHeight = $block->getMaxHeight() ?: $logoBaseHeight;
$logoHeight = $logoBaseHeight > $logoMaxHeight ? $logoBaseHeight : $logoBaseHeight;

/** @var LogoPathResolver $logoPathResolver */
$logoPathResolver = $viewModels->require(LogoPathResolver::class);
$logoSrc = $logoPathResolver && method_exists($logoPathResolver, 'getLogoSrc')
    ? $logoPathResolver->getLogoSrc($block->getData('logo_file'))
    : $block->getLogoSrc();
?>

<?php if ($logoSrc): ?>
    <img
        src="<?= $escaper->escapeUrl($logoSrc) ?>"
        alt="<?= $escaper->escapeHtmlAttr($block->getLogoAlt() ?: $storeName) ?>"
        width="<?= $escaper->escapeHtmlAttr($logoWidth) ?>"
        height="<?= $escaper->escapeHtmlAttr($logoHeight) ?>"
        loading="eager"
    >
<?php endif; ?>
