<?php
/**
 * Shared admin assets for reorder modules.
 *
 * @package WP_PowerSuite\Ordering\Admin
 */

namespace WP_PowerSuite\Ordering\Admin;

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

/**
 * Enqueues shared reorder UI assets.
 */
final class ReorderAssets {

	public const VERSION = '2.2.1';

	public const STYLE_HANDLE  = 'wp-powersuite-reorder-ui';
	public const SCRIPT_HANDLE = 'wp-powersuite-reorder-ui';

	public static function register(): void {
		$base_url = defined( 'WP_POWERSUITE_PLUGIN_URL' ) ? WP_POWERSUITE_PLUGIN_URL : '';
		$base_dir = defined( 'WP_POWERSUITE_SRC_DIR' ) ? WP_POWERSUITE_SRC_DIR : '';

		wp_register_style(
			self::STYLE_HANDLE,
			$base_url . 'src/Ordering/assets/reorder-ui.css',
			[],
			self::VERSION
		);

		wp_register_script(
			self::SCRIPT_HANDLE,
			$base_url . 'src/Ordering/assets/reorder-ui.js',
			[ 'jquery', 'jquery-ui-sortable' ],
			self::VERSION,
			true
		);
	}

	/**
	 * @param list<string> $extra_script_deps Extra script dependencies.
	 */
	public static function enqueue( array $extra_script_deps = [] ): void {
		self::register();

		wp_enqueue_style( self::STYLE_HANDLE );

		$deps = array_values( array_unique( array_merge( [ 'jquery', 'jquery-ui-sortable', self::SCRIPT_HANDLE ], $extra_script_deps ) ) );
		wp_enqueue_script( self::SCRIPT_HANDLE );

		foreach ( $deps as $handle ) {
			if ( $handle !== self::SCRIPT_HANDLE && wp_script_is( $handle, 'registered' ) ) {
				wp_enqueue_script( $handle );
			}
		}
	}

	/**
	 * @return array<string, string>
	 */
	public static function sharedStrings(): array {
		return [
			'dragHandle'      => __( 'Drag to reorder', 'wp-powersuite' ),
			'orderSaved'      => __( 'Order saved.', 'wp-powersuite' ),
			'orderSaveFailed' => __( 'Failed to save order.', 'wp-powersuite' ),
		];
	}
}
