<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class DC_V2_Public {

	public static function init() {
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'maybe_enqueue' ) );
	}

	private static function asset_ver( $relative_path ) {
		$path = DC_V2_PATH . $relative_path;
		$mtime = file_exists( $path ) ? (string) filemtime( $path ) : '0';
		return DC_V2_VERSION . '.' . $mtime;
	}

	public static function maybe_enqueue() {
		if ( ! get_query_var( 'dc_v2_slug' ) ) {
			return;
		}
		$css = 'public-v2/css/chat-checkout.css';
		wp_enqueue_style(
			'dc-v2-chat',
			dc_v2_asset_url( $css ),
			array(),
			self::asset_ver( $css )
		);

		$runtime = DC_V2_Chat_Runtime::resolve();
		if ( empty( $runtime['ready'] ) || empty( $runtime['url'] ) ) {
			return;
		}

		wp_enqueue_script(
			'dc-v2-chat',
			$runtime['url'],
			array(),
			$runtime['ver'],
			true
		);
	}

	public static function resolve_appearance() {
		$app = DC_V2_Document_CPT::normalize_appearance(
			get_option( 'dc_v2_appearance', dc_v2_default_appearance() )
		);
		$preview_keys = array( 'preset', 'primary', 'primary_foreground', 'secondary', 'background', 'foreground', 'card', 'card_foreground', 'muted', 'muted_foreground', 'border', 'input', 'footer', 'header', 'shadow', 'logo_url', 'typing_speed' );
		foreach ( $preview_keys as $key ) {
			$param = 'pv_' . $key;
			if ( isset( $_GET[ $param ] ) ) {
				$app[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $param ] ) );
			}
		}
		return $app;
	}

	public static function enqueue_for_chat( $post, $meta ) {
		// Keep chat footer emoji as text (⚡), not WordPress CDN images that blow up layout.
		remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
		remove_action( 'wp_print_styles', 'print_emoji_styles' );
		remove_action( 'wp_footer', 'print_emoji_detection_script' );
		add_filter( 'emoji_svg_url', '__return_false' );

		self::maybe_enqueue();
		$app = self::resolve_appearance();
		wp_localize_script(
			'dc-v2-chat',
			'DCV2Chat',
			array(
				'restUrl'     => esc_url_raw( rest_url( DC_V2_REST::NS ) ),
				'slug'        => $meta['slug'],
				'preview'     => isset( $_GET['preview'] ),
				'previewMode' => ( isset( $_GET['preview'] ) && isset( $_GET['mode'] ) && in_array( $_GET['mode'], array( 'admin', 'storyboard' ), true ) )
					? sanitize_key( (string) $_GET['mode'] )
					: 'customer',
				'appearance'         => $app,
				'faviconUrl'         => dc_v2_site_favicon_url( 96 ),
				'pluginBrandMarkUrl' => dc_v2_plugin_brand_mark_url(),
				'currency'           => dc_v2_store_currency(),
				'locale'             => dc_v2_plugin_locale(),
				'version'     => DC_V2_VERSION,
				'i18n'        => DC_V2_I18n::public_strings(),
			)
		);
		wp_add_inline_style(
			'dc-v2-chat',
			':root{--primary:' . esc_attr( $app['primary'] ) . ';--secondary:' . esc_attr( $app['secondary'] ) .
			';--primary-fg:' . esc_attr( $app['primary_foreground'] ) .
			';--background:' . esc_attr( $app['background'] ) . ';--foreground:' . esc_attr( $app['foreground'] ) .
			';--card:' . esc_attr( $app['card'] ) . ';--card-fg:' . esc_attr( $app['card_foreground'] ) .
			';--muted:' . esc_attr( $app['muted'] ) . ';--muted-fg:' . esc_attr( $app['muted_foreground'] ) .
			';--border:' . esc_attr( $app['border'] ) . ';--input:' . esc_attr( $app['input'] ) .
			';--footer:' . esc_attr( $app['footer'] ) . ';--header:' . esc_attr( $app['header'] ) .
			';--shadow:' . esc_attr( $app['shadow'] ) . ';}' .
			'.dc-v2-chat img.emoji{display:inline;height:1em;width:1em;margin:0 .15em 0 0;vertical-align:-.1em;}'
		);
	}
}
