<?php
/**
 * Asset loading logic for the email admin UI.
 *
 * @package MailDoctor
 */

namespace MailDoctor;

use MailDoctor\Admin;

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

/**
 * Handles asset orchestration for email admin screens.
 */
trait WPMD_Email_Admin_Assets {


	/**
	 * Enqueue assets for template screens.
	 *
	 * @param string $hook Current admin hook.
	 * @return void
	 */
	public static function enqueue_assets( string $hook ): void {
		$is_mail_doctor_tab = false;

		if ( ! self::is_templates_screen( $hook ) ) {
			$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only.
			$tab  = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only.

			if ( Admin::PAGE_SLUG === $page && 'templates' === $tab ) {
				$is_mail_doctor_tab = true;
			} else {
				return;
			}
		}

		wp_enqueue_style(
			'wpmd-email-templates',
			MAIL_DOCTOR_URL . 'assets/css/email/main.css',
			$is_mail_doctor_tab ? array( 'mail-doctor-admin' ) : array(),
			MAIL_DOCTOR_VERSION
		);
		wp_enqueue_style(
			'wpmd-email-editor',
			MAIL_DOCTOR_URL . 'assets/css/email/editor.css',
			array( 'wpmd-email-templates' ),
			MAIL_DOCTOR_VERSION
		);

		$code_editor_settings = wp_enqueue_code_editor( array( 'type' => 'text/html' ) );
		if ( $code_editor_settings ) {
			wp_localize_script(
				'code-editor',
				'wpmdCodeEditorSettings',
				array(
					'html' => $code_editor_settings,
					'css'  => wp_enqueue_code_editor( array( 'type' => 'text/css' ) ),
				)
			);
		}

		wp_enqueue_editor();

		wp_enqueue_script(
			'wpmd-email-editor',
			MAIL_DOCTOR_URL . 'assets/js/email/editor.js',
			array( 'jquery', 'wp-util', 'code-editor', 'wp-editor' ),
			MAIL_DOCTOR_VERSION,
			true
		);

		wp_localize_script(
			'wpmd-email-editor',
			'WPMDEmailEditor',
			array(
				'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
				'nonces'    => array(
					'preview'  => wp_create_nonce( 'mail_doctor_live_preview' ),
					'sendTest' => wp_create_nonce( 'mail_doctor_send_test' ),
				),
				'locales'   => self::get_locale_options(),
				'statuses'  => self::get_status_options(),
				'mergeTags' => self::get_merge_tag_catalog(),
				'i18n'      => array(
					'modeVisual'        => esc_html__( 'Visual', 'mail-doctor' ),
					'modeCode'          => esc_html__( 'Code (HTML)', 'mail-doctor' ),
					'rendering'         => esc_html__( 'Rendering preview…', 'mail-doctor' ),
					'previewError'      => esc_html__( 'Unable to render preview.', 'mail-doctor' ),
					'saveSuccess'       => esc_html__( 'Template saved.', 'mail-doctor' ),
					'saveError'         => esc_html__( 'Unable to save this template.', 'mail-doctor' ),
					'sendSuccess'       => esc_html__( 'Test email sent.', 'mail-doctor' ),
					'sendError'         => esc_html__( 'Unable to send the test email.', 'mail-doctor' ),
					'copySuccess'       => esc_html__( 'Merge tag copied', 'mail-doctor' ),
					'copyError'         => esc_html__( 'Unable to copy merge tag.', 'mail-doctor' ),
					'confirmReset'      => esc_html__( 'Reset this template to plugin defaults?', 'mail-doctor' ),
					'nothingToCopy'     => esc_html__( 'Nothing to copy.', 'mail-doctor' ),
					'warningsTitle'     => esc_html__( 'Validation warnings', 'mail-doctor' ),
					'noWarnings'        => esc_html__( 'Looks good - no validation warnings found.', 'mail-doctor' ),
					'warningsAvailable' => esc_html__( 'Validation warnings detected. Please review them below.', 'mail-doctor' ),
					'genericWarning'    => esc_html__( 'Please review this template section.', 'mail-doctor' ),
				),
			)
		);
	}
}
