<?php
/**
 * Admin assets for Price Labels.
 *
 * @package PhoenixWP\PriceLabels\Admin
 */

declare(strict_types=1);

namespace PhoenixWP\PriceLabels\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Enqueues admin scripts and styles on the labels screen.
 */
final class Assets {

	private static ?self $instance = null;

	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	private function __construct() {}

	private function __clone() {}

	public function init(): void {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
	}

	/**
	 * @param string $hook_suffix Current admin page hook.
	 */
	public function enqueue( string $hook_suffix ): void {
		if ( ! str_contains( $hook_suffix, Labels_Admin::PAGE_SLUG ) ) {
			return;
		}

		wp_enqueue_style(
			'phoenix-price-labels-for-woocommerce-admin',
			PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_URL . 'assets/css/admin.css',
			array(),
			PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_VERSION
		);

		wp_enqueue_script(
			'phoenix-price-labels-for-woocommerce-admin',
			PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_URL . 'assets/js/admin-product-picker.js',
			array( 'jquery' ),
			PHOENIX_PRICE_LABELS_FOR_WOOCOMMERCE_VERSION,
			true
		);

		wp_localize_script(
			'phoenix-price-labels-for-woocommerce-admin',
			'phoenixWpPriceLabelsAdmin',
			array(
				'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
				'nonce'        => wp_create_nonce( Product_Search::NONCE_ACTION ),
				'action'       => Product_Search::AJAX_ACTION,
				'pickerRepeat' => phoenix_wp_price_labels_admin_picker_repeat_enabled(),
				'i18n'         => array(
					'searchPlaceholder' => __( 'Search by name, SKU, or product ID…', 'phoenix-price-labels-for-woocommerce' ),
					'loading'           => __( 'Loading products…', 'phoenix-price-labels-for-woocommerce' ),
					'empty'             => __( 'No products found.', 'phoenix-price-labels-for-woocommerce' ),
					'selected'          => __( 'Selected:', 'phoenix-price-labels-for-woocommerce' ),
					'products'          => __( 'products', 'phoenix-price-labels-for-woocommerce' ),
				'loadMore'          => __( 'Load more', 'phoenix-price-labels-for-woocommerce' ),
				'scanCodeSingleTitleMaxMm' => 35,
			),
			)
		);
	}
}
