<?php
/**
 * Loads the WordPress media frame and picker script on the theme's edit screens
 * so image fields offer a Select / Upload button (not URL-only).
 *
 * @package    Odysseo
 * @author     Chandrabhan Shekhawat
 * @company    Gigai Kripa Services
 * @link       https://gigaikripaservices.com/
 * @copyright  Copyright (c) 2026, Gigai Kripa Services
 */

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Enqueues the media library and the picker script on the post types and the
 * state taxonomy that expose image URL fields.
 *
 * @param string $hook Current admin page hook.
 */
function odysseo_admin_media_enqueue( $hook ) {
	$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
	if ( ! $screen ) {
		return;
	}
	$on_post = in_array( $hook, array( 'post.php', 'post-new.php' ), true )
		&& in_array( $screen->post_type, array( 'destination', 'tour_package' ), true );
	$on_term = in_array( $hook, array( 'term.php', 'edit-tags.php' ), true )
		&& 'dest_state' === $screen->taxonomy;
	if ( ! $on_post && ! $on_term ) {
		return;
	}
	wp_enqueue_media();
	wp_enqueue_style(
		'odysseo-admin-media',
		ODYSSEO_CORE_URL . 'assets/css/admin-media.css',
		array(),
		'1.0.0'
	);
	wp_enqueue_script(
		'odysseo-admin-media',
		ODYSSEO_CORE_URL . 'assets/js/admin-media.js',
		array( 'jquery', 'jquery-ui-sortable' ),
		'1.0.0',
		true
	);
}
add_action( 'admin_enqueue_scripts', 'odysseo_admin_media_enqueue' );
