<?php
/**
 * A single logo image variant (primary or sticky) inside the logo link.
 *
 * The sticky variant carries only the image — the title/tagline live once in
 * the parent .brandy-logo. It starts hidden and is swapped in by logo.js when
 * the header pins (the .sticky-start state).
 *
 * Expected $args:
 * - logo_type string 'primary' | 'sticky'
 * - settings  array  The logo element settings.
 * - device    string Current device being rendered ('desktop' by default).
 *
 * @package Brandy\Templates\Builder\Elements
 */

$logo_type = $args['logo_type'];
$settings  = $args['settings'];
$device    = isset( $args['device'] ) ? $args['device'] : 'desktop';

$is_primary = 'primary' === $logo_type;

// Image sources. The primary variant has separate desktop/mobile logos;
// the sticky variant uses its single sticky logo for both.
$logo_settings        = $is_primary ? ( $settings['logo'] ?? array( 'url' => '#' ) ) : $settings['sticky_logo'];
$mobile_logo_settings = $is_primary ? ( $settings['logo_mobile'] ?? array( 'url' => '#' ) ) : $settings['sticky_logo'];

$enabled_devices = $settings['logo_enabled_devices'] ?? array( 'desktop', 'mobile' );

// Fall back to the site logo (then the site icon) when no logo is set; the
// mobile logo falls back to the desktop logo when it has no dedicated image.
$logo_url        = empty( $logo_settings['url'] ) ? brandy_get_site_logo_url() : $logo_settings['url'];
$mobile_logo_url = empty( $mobile_logo_settings['url'] ) ? $logo_url : $mobile_logo_settings['url'];

$site_name = get_bloginfo( 'name' );

// The logo image is only output for devices where the logo is enabled (all
// devices render in the customizer preview so the live toggle works).
if ( ! ( in_array( $device, $enabled_devices, true ) || is_customize_preview() ) ) {
	return;
}
?>
<span class="brandy-logo-image <?php echo is_customize_preview() ? esc_attr( brandy_get_enabled_device_classes( $enabled_devices ) ) : ''; ?>" logo-type="<?php echo esc_attr( $logo_type ); ?>"<?php echo $is_primary ? '' : ' style="display:none;"'; ?>>
	<?php if ( 'desktop' === $device || is_customize_preview() ) : ?>
		<img width="150" height="150" class="brandy-logo__img logo-desktop" src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr( $site_name ); ?>"/>
	<?php endif; ?>
	<?php if ( 'mobile' === $device || is_customize_preview() ) : ?>
		<img width="150" height="150" class="brandy-logo__img logo-mobile" src="<?php echo esc_url( $mobile_logo_url ); ?>" alt="<?php echo esc_attr( $site_name ); ?>" data-using-desktop-source="<?php echo empty( $mobile_logo_settings['url'] ) ? 'true' : 'false'; ?>"/>
	<?php endif; ?>
</span>
