<?php

if ( ! isset( $value ) || empty( $value ) ) {
	return;
}

$name   = $field['name'] ?? '';
$label  = $field['label'] ?? $name;
$video_url = trim( $value );

// Try to get embeddable HTML from the URL
$embed_html = wp_oembed_get( $video_url );

// Fallback: if oEmbed fails, display a normal iframe
if ( empty( $embed_html ) ) {
	$embed_html = sprintf(
		'<iframe src="%s" width="640" height="360" frameborder="0" allowfullscreen loading="lazy"></iframe>',
		esc_url( $video_url )
	);
}

?>

<div class="ecm-oembed ecm-oembed--<?php echo esc_attr( $name ); ?>">
	<?php if ( ! empty( $with_label ) ) : ?>
		<strong class="ecm-oembed__label"><?php echo esc_html( $label ); ?>:</strong>
	<?php endif; ?>

	<div class="ecm-oembed__container embed-container">
		<?php
		// Output the generated embed HTML (safe; generated by WordPress)
		echo $embed_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		?>
	</div>
</div>
