<?php
/**
 * Gestión de assets (reemplaza wp_enqueue_*).
 */

final class JB_Assets {
	private static $styles = array();
	private static $scripts = array();
	private static $localized = array();
	private static $inline = array();
	private static $enqueued_styles = array();
	private static $enqueued_scripts = array();
	private static $done = false;

	public static function enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) {
		if ( 'dashicons' === $handle && '' === $src ) {
			$src = home_url( 'assets/css/dashicons.css' );
			if ( false === $ver && defined( 'LOGISTICA_VERSION' ) ) {
				$ver = LOGISTICA_VERSION;
			}
		}
		self::$styles[ $handle ] = compact( 'handle', 'src', 'deps', 'ver', 'media' );
	}

	public static function enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = true ) {
		self::$scripts[ $handle ] = array(
			'handle'    => $handle,
			'src'       => $src,
			'deps'      => $deps,
			'ver'       => $ver,
			'in_footer' => $in_footer,
		);
	}

	public static function register_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = true ) {
		self::enqueue_script( $handle, $src, $deps, $ver, $in_footer );
	}

	public static function localize_script( $handle, $object_name, $data ) {
		self::$localized[ $handle ] = array( 'object' => $object_name, 'data' => $data );
	}

	public static function add_inline_script( $handle, $data, $position = 'after' ) {
		if ( ! isset( self::$inline[ $handle ] ) ) {
			self::$inline[ $handle ] = '';
		}
		self::$inline[ $handle ] .= $data;
	}

	public static function render_head() {
		$ordered = self::resolve_deps( self::$styles );
		foreach ( $ordered as $handle ) {
			$s = self::$styles[ $handle ];
			if ( empty( $s['src'] ) ) {
				continue;
			}
			$src = self::asset_url( $s['src'], $s['ver'] );
			echo '<link rel="stylesheet" id="' . esc_attr( $handle ) . '-css" href="' . esc_url( $src ) . '" media="' . esc_attr( $s['media'] ) . '" />' . "\n";
		}
	}

	public static function render_footer() {
		$ordered = self::resolve_deps( self::$scripts );
		foreach ( $ordered as $handle ) {
			$s = self::$scripts[ $handle ];
			if ( ! empty( self::$localized[ $handle ] ) ) {
				$loc = self::$localized[ $handle ];
				echo '<script>var ' . esc_attr( $loc['object'] ) . ' = ' . wp_json_encode( $loc['data'] ) . ';</script>' . "\n";
			}
			if ( ! empty( self::$inline[ $handle ] ) && empty( $s['src'] ) ) {
				echo '<script id="' . esc_attr( $handle ) . '-js">' . self::$inline[ $handle ] . '</script>' . "\n";
				continue;
			}
			if ( empty( $s['src'] ) ) {
				if ( ! empty( self::$inline[ $handle ] ) ) {
					echo '<script id="' . esc_attr( $handle ) . '-js">' . self::$inline[ $handle ] . '</script>' . "\n";
				}
				continue;
			}
			$src = self::asset_url( $s['src'], $s['ver'] );
			echo '<script id="' . esc_attr( $handle ) . '-js" src="' . esc_url( $src ) . '"></script>' . "\n";
			if ( ! empty( self::$inline[ $handle ] ) ) {
				echo '<script>' . self::$inline[ $handle ] . '</script>' . "\n";
			}
		}
	}

	private static function asset_url( $src, $ver ) {
		if ( str_starts_with( $src, 'http' ) ) {
			return $ver ? add_query_arg( 'ver', $ver, $src ) : $src;
		}
		$url = home_url( 'assets/' . ltrim( preg_replace( '#^.*?assets/#', '', $src ), '/' ) );
		return $ver ? add_query_arg( 'ver', $ver, $url ) : $url;
	}

	private static function resolve_deps( $items ) {
		$resolved = array();
		$resolve = function ( $handle ) use ( &$resolve, &$resolved, $items ) {
			if ( in_array( $handle, $resolved, true ) || ! isset( $items[ $handle ] ) ) {
				return;
			}
			foreach ( (array) $items[ $handle ]['deps'] as $dep ) {
				$resolve( $dep );
			}
			$resolved[] = $handle;
		};
		foreach ( array_keys( $items ) as $handle ) {
			$resolve( $handle );
		}
		return $resolved;
	}
}

function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) {
	JB_Assets::enqueue_style( $handle, $src, $deps, $ver, $media );
}

function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = true ) {
	JB_Assets::enqueue_script( $handle, $src, $deps, $ver, $in_footer );
}

function wp_register_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = true ) {
	JB_Assets::register_script( $handle, $src, $deps, $ver, $in_footer );
}

function wp_localize_script( $handle, $object_name, $data ) {
	JB_Assets::localize_script( $handle, $object_name, $data );
}

function wp_add_inline_script( $handle, $data, $position = 'after' ) {
	JB_Assets::add_inline_script( $handle, $data, $position );
}

function wp_enqueue_media() {
	wp_enqueue_script( 'jb-media-uploader', home_url( 'assets/js/media-uploader.js' ), array(), LOGISTICA_VERSION, true );
	wp_localize_script( 'jb-media-uploader', 'jbMedia', array(
		'uploadUrl' => home_url( 'upload.php' ),
		'nonce'     => wp_create_nonce( 'jb_media_upload' ),
	) );
}

function wp_enqueue_editor() {
	wp_enqueue_script( 'tinymce', 'https://cdn.jsdelivr.net/npm/tinymce@6.8.3/tinymce.min.js', array(), '6.8.3', true );
	wp_enqueue_script( 'jb-wp-editor', home_url( 'assets/js/wp-editor.js' ), array( 'tinymce' ), LOGISTICA_VERSION, true );
}

function wp_print_styles() {
	JB_Assets::render_head();
}

function wp_print_scripts() {
	JB_Assets::render_footer();
}
