<?php

use Tangibledesign\Framework\Models\User\User;

/* @var User $mhTempUser */
/* @var string $mhImageSize */
/* @var string $mhPlaceholderType */
global $mhTempUser, $mhImageSize, $mhPlaceholderType;

$mhPlaceholderInitials = '';
$mhDisplayName = trim((string)$mhTempUser->getDisplayName());
if ($mhDisplayName !== '') {
    $mhNameParts = preg_split('/[\s-]+/u', $mhDisplayName, -1, PREG_SPLIT_NO_EMPTY);
    if (is_array($mhNameParts)) {
        $mhNameParts = array_slice($mhNameParts, 0, 2);
        foreach ($mhNameParts as $mhNamePart) {
            $mhLetter = function_exists('mb_substr')
                ? mb_substr($mhNamePart, 0, 1, 'UTF-8')
                : substr($mhNamePart, 0, 1);

            if (!is_string($mhLetter) || $mhLetter === '') {
                continue;
            }

            $mhPlaceholderInitials .= function_exists('mb_strtoupper')
                ? mb_strtoupper($mhLetter, 'UTF-8')
                : strtoupper($mhLetter);
        }
    }
}

if ($mhTempUser->hasSocialImage()) : ?>
    <img
            class="lazyload"
            src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAQAAAA3fa6RAAAADklEQVR42mNkAANGCAUAACMAA2w/AMgAAAAASUVORK5CYII="
            data-src="<?php echo esc_url($mhTempUser->getSocialImage()); ?>"
            alt="<?php echo esc_attr($mhTempUser->getDisplayName()); ?>"
    >
<?php else :
    $mhTempUserImage = $mhTempUser->getImage();
    if ($mhTempUserImage):
        $mhTempUserImageSrcset = $mhTempUserImage->getSrcset($mhImageSize);
        ?>
        <img
                class="lazyload"
                src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAQAAAA3fa6RAAAADklEQVR42mNkAANGCAUAACMAA2w/AMgAAAAASUVORK5CYII="
                alt="<?php echo esc_attr($mhTempUser->getDisplayName()); ?>"
            <?php if (!empty($mhTempUserImageSrcset)) : ?>
                data-srcset="<?php echo esc_attr($mhTempUserImageSrcset); ?>"
                data-sizes="auto"
            <?php else : ?>
                data-src="<?php echo esc_url($mhTempUserImage->getImageUrl($mhImageSize)); ?>"
            <?php endif; ?>
        >
    <?php else : ?>
        <img
                src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAQAAAA3fa6RAAAADklEQVR42mNkAANGCAUAACMAA2w/AMgAAAAASUVORK5CYII="
                alt="<?php echo esc_attr($mhTempUser->getDisplayName()); ?>"
        >

        <div
            <?php if ($mhPlaceholderType === 'circle') : ?>
                class="myhome-user-image-placeholder myhome-user-image-placeholder--circle"
            <?php else : ?>
                class="myhome-user-image-placeholder"
            <?php endif; ?>
        >
            <?php if ($mhPlaceholderInitials !== '') : ?>
                <span class="myhome-user-image-placeholder__initials"><?php echo esc_html($mhPlaceholderInitials); ?></span>
            <?php else : ?>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" aria-hidden="true" focusable="false">
                    <!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.-->
                    <path d="M304 128a80 80 0 1 0 -160 0 80 80 0 1 0 160 0zM96 128a128 128 0 1 1 256 0A128 128 0 1 1 96 128zM49.3 464H398.7c-8.9-63.3-63.3-112-129-112H178.3c-65.7 0-120.1 48.7-129 112zM0 482.3C0 383.8 79.8 304 178.3 304h91.4C368.2 304 448 383.8 448 482.3c0 16.4-13.3 29.7-29.7 29.7H29.7C13.3 512 0 498.7 0 482.3z"/>
                </svg>
            <?php endif; ?>
        </div>
    <?php endif; ?>
<?php endif;
