const YOUTUBE_ID_EXTRACTOR =
	/(?<video_domain>youtu(?:be\.com|\.be|be\.googleapis\.com))(?:\/(?<action>video|embed|watch|shorts|v))?.*(?:(?:\/|v=)(?<video_id>[A-Za-z0-9._%-]*))\S*/gi;

function embed_youtube(link_info) {
	const is_youtube_link = ["youtube.com", "youtu.be", "youtube.googleapis.com"].includes(
		link_info.domain?.toLowerCase(),
	);

	if (!is_youtube_link) {
		return;
	}

	YOUTUBE_ID_EXTRACTOR.lastIndex = 0;

	const {
		groups: { video_domain, action, video_id },
	} = YOUTUBE_ID_EXTRACTOR.exec(link_info.url) ?? { groups: {} };

	if (!video_id) {
		return;
	}

	return `
	<div class="embed-container iframe ${action === "shorts" ? "vertical" : "letterbox"} youtube">
		<div class="embed-actions-container">
			<button class="icon plus" onclick="console.log(\"close\");"/>
			<button class="icon pause" onclick="console.log(\"stop\");"/>
		</div>
		<iframe
		src="https://www.youtube.com/embed/${video_id}"
		title="YouTube video player"
		allow="clipboard-write; encrypted-media; picture-in-picture; web-share;"
		referrerpolicy="strict-origin-when-cross-origin"
	 	allowfullscreen
		loading="lazy"
		></iframe>
	</div>`;
}
