<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/* -------------------------------------------------------------------------
 * Frontend asset detection
 * ---------------------------------------------------------------------- */

function misticease_content_may_need_assets( string $content ): bool {
	if ( '' === $content ) {
		return false;
	}
	if ( preg_match( '#(?:youtu\.be/|(?:(?:www|m)\.)?youtube\.com/(?:watch\?|shorts/|embed/|live/|v/)|(?:www\.)?youtube-nocookie\.com/embed/|(?:www\.)?vimeo\.com/|player\.vimeo\.com/video/|(?:www\.)?dailymotion\.com/(?:video|embed/video)/|dai\.ly/)#i', $content ) ) {
		return true;
	}
	if ( false !== stripos( $content, 'wp-image-' ) && false !== stripos( $content, '<a' ) ) {
		return true;
	}
	return false;
}

function misticease_should_enqueue_frontend_assets(): bool {
	if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
		return false;
	}
	if ( is_singular() ) {
		return true;
	}
	global $wp_query;
	if ( ! isset( $wp_query ) || ! isset( $wp_query->posts ) || ! is_array( $wp_query->posts ) ) {
		return true;
	}
	foreach ( $wp_query->posts as $post ) {
		if ( ! is_object( $post ) || ! isset( $post->post_content ) ) {
			continue;
		}
		if ( misticease_content_may_need_assets( (string) $post->post_content ) ) {
			return true;
		}
	}
	return false;
}

/* -------------------------------------------------------------------------
 * Frontend enqueue (JS/CSS) – use effective CDN hosts
 * ---------------------------------------------------------------------- */

add_action(
	'wp_enqueue_scripts',
	static function (): void {
		if ( ! misticease_should_enqueue_frontend_assets() ) {
			return;
		}

		[ $css_url, $css_ver ] = misticease_asset( 'inc/app/css/misticease.css' );
		wp_enqueue_style( 'misticease', $css_url, [], $css_ver );

		[ $wm_url, $wm_ver ] = misticease_asset( 'inc/app/js/misticease-window-manager.js' );
		wp_register_script( 'misticease-window-manager', $wm_url, [], $wm_ver, true );

		[ $js_url, $js_ver ] = misticease_asset( 'inc/app/js/misticease.js' );
		wp_register_script( 'misticease', $js_url, [ 'misticease-window-manager' ], $js_ver, true );

		[ $sml_url, $sml_ver ] = misticease_asset( 'inc/app/js/misticease-sml-parser.js' );
		wp_register_script( 'misticease-sml-parser', $sml_url, [ 'misticease' ], $sml_ver, true );

		$o           = misticease_opts_get();
		$image_hosts = array_filter(
			array_unique(
				array_map(
					'misticease_normalize_image_popup_host',
					[
						home_url(),
						site_url(),
						content_url(),
					]
				)
			)
		);
		$cfg         = [
			'images'      => ( isset( $o['image_popup'] ) && 'on' === $o['image_popup'] ),
			'cdnHosts'    => misticease_cdn_hosts_effective(),
			'imageHosts'  => array_values( $image_hosts ),
			'playPng'     => plugins_url( 'inc/app/img/play.png', misticease_basefile() ),
			'pluginUrl'   => plugins_url( 'inc/app/', misticease_basefile() ),
			'floatingUx'  => ( isset( $o['floating_ux'] ) && 'on' === $o['floating_ux'] ) ? 'on' : 'off',
			'i18n'       => [
				'drag'           => _x( 'Drag', 'floating pop-up control label', 'misticease' ),
				'pin'            => _x( 'Pin', 'floating pop-up control label', 'misticease' ),
				'closeVideo'     => __( 'Close video', 'misticease' ),
				'embeddedVideo'  => __( 'Embedded video', 'misticease' ),
				'image'          => __( 'Image', 'misticease' ),
				'closeImage'     => __( 'Close image', 'misticease' ),
				'previousImage'  => __( 'Previous image', 'misticease' ),
				'nextImage'      => __( 'Next image', 'misticease' ),
			],
		];

		wp_localize_script( 'misticease-window-manager', 'misticeaseCfg', $cfg );

		wp_enqueue_script( 'misticease' );
		wp_enqueue_script( 'misticease-sml-parser' );
	},
	20
);