<?php
/**
 * Logo element template.
 *
 * Renders a single .brandy-logo link that contains the logo image(s) — the
 * primary image plus an optional sticky image swapped in while the header is
 * pinned — followed by the site title and tagline (rendered once).
 *
 * @package Brandy\Templates\Builder\Elements
 */

$element    = $args['element'];
$device     = isset( $args['device'] ) ? $args['device'] : 'desktop';
$settings   = $element['settings'];
$section_id = $element['id'];

$sticky_logo_enabled = ! empty( $settings['sticky_logo']['enabled'] );

$wrapper_attributes = array(
	'data-builder'         => $args['builder'],
	'data-section-id'      => $section_id,
	'data-has-sticky-logo' => $sticky_logo_enabled ? 'true' : 'false',
);

// In the customizer, expose the site's fallback images (Site Logo, then Site
// Icon) so the preview can restore them when the logo's own image is cleared.
// Seeded here (correct at load) and kept fresh by the customizer on change.
if ( is_customize_preview() ) {
	$custom_logo_id                       = get_theme_mod( 'custom_logo' );
	$wrapper_attributes['data-site-logo'] = $custom_logo_id ? (string) wp_get_attachment_image_url( $custom_logo_id, 'full' ) : '';
	$wrapper_attributes['data-site-icon'] = (string) get_site_icon_url();
}

// Image variants rendered inside the single logo link. Only the image is
// duplicated for the sticky state; the title/tagline are rendered once below.
$image_types = array( 'primary' );
if ( $sticky_logo_enabled ) {
	$image_types[] = 'sticky';
}
?>

<div class="brandy-element brandy-logo-element <?php echo esc_attr( brandy_get_editable_class() ); ?>" <?php brandy_print_dom_attributes( $wrapper_attributes ); ?>>
	<div class="brandy-element-wrapper">
		<a class="brandy-logo" href="<?php echo esc_url( home_url() ); ?>">
			<?php
			foreach ( $image_types as $image_type ) {
				get_template_part(
					'template-parts/builder/elements/logo-image',
					'',
					array(
						'logo_type' => $image_type,
						'settings'  => $settings,
						'device'    => $device,
					)
				);
			}
			?>
			<div class="brandy-logo__content">
				<?php
				// Title and tagline are plain text; the wrapping .brandy-logo
				// anchor is the single home-page link. Both expose the raw
				// configured text via data-text for the live customizer preview.
				foreach ( array( 'title', 'tagline' ) as $type ) :
					$content_tag = 'div';
					$item_attrs  = array(
						'class' => "brandy-logo__{$type} " . brandy_get_enabled_device_classes( $settings[ $type ]['enabled_devices'] ),
					);

					$text = ! empty( $settings[ $type ]['text'] ) ? $settings[ $type ]['text'] : '';

					if ( ! empty( $text ) ) {
						// brandy_print_dom_attributes() escapes attribute values,
						// so the raw text is passed here to avoid double-encoding.
						$item_attrs['data-text'] = $text;
					}

					if ( 'title' === $type ) {
						$content_tag                        = 'span';
						$item_attrs['data-hover-underline'] = empty( $settings[ $type ]['underline_when_hovering'] ) ? 'false' : 'true';
						$text                               = '' !== $text ? $text : get_bloginfo( 'name' );
					} else {
						$text = '' !== $text ? $text : get_bloginfo( 'description' );
					}

					echo '<' . $content_tag . ' '; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static tag name.
					brandy_print_dom_attributes( $item_attrs );
					echo '>' . esc_html( $text ) . '</' . $content_tag . '>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- tag name is static; text is escaped.
				endforeach;
				?>
			</div>
		</a>
	</div>
	<?php
	if ( is_customize_preview() ) {
		get_template_part(
			'template-parts/common/edit-section-button',
			'',
			array(
				'part_id' => $section_id,
			)
		);
	}
	?>
</div>
