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

/**
 * AnyWareX Clone Translate (Free) – PHP fallback layer
 *
 * Goal:
 * 1) Provide awxct_get_json_value() so themes do not fatal.
 * 2) Delegate to Pro when available.
 * 3) Free must NOT touch JS placeholders like {{ "key" | awxct }}.
 */

add_action(
	'plugins_loaded',
	function () {

		/**
		 * Return a marker for known premium namespaces.
		 *
		 * @param string $key
		 * @return string|null
		 */
		function awxct_free_marker_for_key( string $key ) {
			$key = trim( $key );

			if ( defined( 'AWXCT_PRO_VERSION' ) ) {
				return null;
			}

			$pro_prefixes = array(
				'aliases.',
				'meta.',
			);

			foreach ( $pro_prefixes as $p ) {
				if ( 0 === strpos( $key, $p ) ) {
					return '[AWXCT: Extension required]';
				}
			}

			$pluspro_prefixes = array(
				'address.',
				'contact.',
				'hours.',
				'labels.',
				'nav.',
				'ui.',
				'footer.',
			);

			foreach ( $pluspro_prefixes as $p ) {
				if ( 0 === strpos( $key, $p ) ) {
					return '[AWXCT: Extension required]';
				}
			}

			return null;
		}

		/**
		 * Safe fallback if the separate extension is not active.
		 *
		 * @param mixed  $loc
		 * @param string $key
		 * @param mixed  $fallback
		 * @return mixed
		 */
		if ( ! function_exists( 'awxct_get_json_value' ) ) {
			function awxct_get_json_value( $loc, $key, $fallback = '' ) {
				if ( function_exists( 'awx_plg_ct_get_json_value' ) ) {
					return awx_plg_ct_get_json_value( $loc, (string) $key, $fallback );
				}

				$key = (string) $key;
				$marker = awxct_free_marker_for_key( $key );
				if ( null !== $marker ) {
					return $marker;
				}

				return '[AWXCT: Extension required]';
			}
		}
	},
	999999
);
