<?php
/**
 * Admin integration for client-side upload optimization.
 *
 * @package Sityn_Image_Optimizer
 */

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

/**
 * Registers hooks and admin assets.
 */
final class Sityn_Image_Optimizer_Plugin {
	/**
	 * Hook entrypoint.
	 */
	public static function init() {
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_assets' ) );
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_bricks_builder_assets' ) );
		add_filter( 'wp_prepare_attachment_for_js', array( __CLASS__, 'prepare_attachment_for_js' ), 10, 3 );
	}

	/**
	 * Enqueue the optimizer on media-enabled admin screens.
	 */
	public static function enqueue_admin_assets() {
		if ( ! current_user_can( 'upload_files' ) ) {
			return;
		}

		self::enqueue_assets();
	}

	/**
	 * Enqueue assets for frontend builder contexts that use wp.media.
	 */
	public static function enqueue_bricks_builder_assets() {
		if ( ! current_user_can( 'upload_files' ) ) {
			return;
		}

		self::enqueue_assets();
	}

	/**
	 * Enqueue shared media optimizer assets.
	 */
	private static function enqueue_assets() {
		wp_enqueue_media();

		wp_enqueue_style(
			'sityn-image-optimizer-admin',
			SITYN_IMAGE_OPTIMIZER_URL . 'assets/css/admin.css',
			array(),
			SITYN_IMAGE_OPTIMIZER_VERSION
		);

		wp_enqueue_script(
			'sityn-image-optimizer-admin',
			SITYN_IMAGE_OPTIMIZER_URL . 'assets/js/admin.js',
			array( 'media-views', 'plupload-all', 'wp-i18n' ),
			SITYN_IMAGE_OPTIMIZER_VERSION,
			true
		);

		wp_localize_script(
			'sityn-image-optimizer-admin',
			'sitynImageOptimizer',
			array(
				'workerUrl'      => SITYN_IMAGE_OPTIMIZER_URL . 'assets/js/optimizer-worker.js',
				'avifEncoderUrl' => SITYN_IMAGE_OPTIMIZER_URL . 'assets/vendor/jsquash-avif/encode.js',
				'avifWasmUrl'    => SITYN_IMAGE_OPTIMIZER_URL . 'assets/vendor/jsquash-avif/codec/enc/avif_enc.wasm',
				'version'        => SITYN_IMAGE_OPTIMIZER_VERSION,
				'defaults'       => array(
					'enabled'  => true,
					'format'   => 'webp',
					'quality'  => 82,
					'maxWidth' => 1920,
				),
				'i18n'           => array(
					'panelTitle'      => __( 'Sityn Image Optimizer', 'sityn-image-optimizer' ),
					'enableOptimizer' => __( 'Optimize uploads', 'sityn-image-optimizer' ),
					'format'          => __( 'Format', 'sityn-image-optimizer' ),
					'quality'         => __( 'Quality', 'sityn-image-optimizer' ),
					'maxWidth'        => __( 'Max Width', 'sityn-image-optimizer' ),
					'pixels'          => __( 'px', 'sityn-image-optimizer' ),
					'totalProgress'   => __( 'Batch Progress', 'sityn-image-optimizer' ),
					'totalSaved'      => __( 'Total Data Saved', 'sityn-image-optimizer' ),
					'processing'      => __( 'Processing images...', 'sityn-image-optimizer' ),
					'complete'        => __( 'Optimization complete.', 'sityn-image-optimizer' ),
					'avifUnsupported' => __( 'AVIF is not supported by this browser.', 'sityn-image-optimizer' ),
				),
			)
		);
	}

	/**
	 * Add a marker to media modal attachment data for integrations.
	 *
	 * @param array       $response   Attachment response.
	 * @param WP_Post     $attachment Attachment object.
	 * @param array|false $meta       Attachment meta.
	 * @return array
	 */
	public static function prepare_attachment_for_js( $response, $attachment, $meta ) {
		unset( $attachment, $meta );

		$response['sitynOptimizer'] = array(
			'available' => current_user_can( 'upload_files' ),
		);

		return $response;
	}
}
