<?php
/**
 * Local i18n helper (delegates to translations.php / custausu_t()).
 * Keeps this file clean and scalable (EN/IT/FR/TR/SV...).
 */

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

if (!function_exists('custausu__t')) {
  function custausu__t(string $s): string {
    return function_exists('custausu_t') ? (string) custausu_t($s) : $s;
  }
}

if (!function_exists('custausu__t_safe')) {
  function custausu__t_safe(string $s): string {
    if (function_exists('custausu__t')) return (string) custausu__t($s);
    if (function_exists('custausu_t'))  return (string) custausu_t($s);
    return $s;
  }
}

function custausu_home_url_for_lang(string $lang): string {
    unset($lang);
    $lang = function_exists('custausu_current_lang')
      ? (string) custausu_current_lang()
      : (string) custausu_config('routes.policy.default_lang', 'en');

    if (function_exists('custausu_config')) {
        $path = trim((string) custausu_config("routes.paths.$lang.home", ''));
        if ($path !== '') return home_url($path);
    }

    return home_url('/');
}

function custausu_user_lang(int $user_id, string $fallback = 'en'): string {
    $default = function_exists('custausu_current_lang')
      ? sanitize_key((string) custausu_current_lang())
      : sanitize_key((string) custausu_config('routes.policy.default_lang', 'en'));
    if (!in_array($default, ['en', 'it'], true)) $default = 'en';

    $stored = sanitize_key(strtolower(trim((string) get_user_meta($user_id, 'user_lang', true))));
    if ($stored === $default) return $stored;

    $fallback = sanitize_key(strtolower(trim($fallback)));
    return $fallback === $default ? $fallback : $default;
}

if (!function_exists('custausu_popup_lang_context')) {
  /**
   * Return the active page and user language context for popup payloads.
   *
   * @return array{current_lang:string,user_lang:string}
   */
  function custausu_popup_lang_context(): array {
  $current = function_exists('custausu_js_lang')
    ? strtolower(trim((string) custausu_js_lang()))
    : strtolower(trim((string) custausu_config('routes.policy.default_lang', 'en')));

  if ($current === '') $current = 'en';

  $user = '';
  if (is_user_logged_in()) {
    $user = custausu_user_lang((int) get_current_user_id(), $current);
  }

  return [
    'current_lang' => $current,
    'user_lang'    => $user,
  ];
  }
}

if (!function_exists('custausu_popup_js_cfg')) {
  function custausu_popup_js_cfg(string $current_lang, string $user_lang = ''): array {
    return [
      'debug' => [
        'enabled'  => (bool) custausu_config('debug.enabled', false),
        'register' => (bool) custausu_config('debug.channels.register', false),
        'on'       => (bool) (custausu_config('debug.enabled', false) && custausu_config('debug.channels.register', false)),
      ],
      'lang' => $current_lang,
      'i18nEnabled' => function_exists('custausu_i18n_enabled') ? (bool) custausu_i18n_enabled() : false,
      'userLang' => $user_lang,

      'i18n' => [
        'emailVerified' => custausu__t_safe(
          '✅ Thanks! Your email is now verified'
        ),
        'emailVerifiedEn' =>
          '✅ Thanks! Your email is now verified',
        'emailVerifiedIt' =>
          '✅ Grazie! La tua email è stata verificata',

        'registrationDone' => custausu__t_safe(
          "✅ Registration successful!<br />Check your inbox to confirm your account."
        ),
        'registrationDoneEn' =>
          "✅ Registration successful!<br />Check your inbox to confirm your account.",
        'registrationDoneIt' =>
          "✅ Registrazione completata!<br />Controlla la tua email per confermare l'account.",

        'emailInvalidEn' =>
          '⚠️ This verification link is no longer valid.<br />Request a new link or use the link in the most recent confirmation email.',
        'emailInvalidIt' =>
          '⚠️ Questo link di verifica non è più valido.<br />Richiedi un nuovo link oppure usa il link contenuto nell’email di conferma più recente.',

        'emailExpiredEn' =>
          '⚠️ This verification link has expired.<br />Request a new link or use the link in the most recent confirmation email.',
        'emailExpiredIt' =>
          '⚠️ Questo link di verifica è scaduto.<br />Richiedi un nuovo link oppure usa il link contenuto nell’email di conferma più recente.',
      ],
    ];
  }
}

// Evitiamo duplicazioni AUTH CF7
if (!function_exists('custausu_js_debug_flags')) {
  function custausu_js_debug_flags(string $channel): array {
    $enabled = (bool) custausu_config('debug.enabled', false);
    $chan    = (bool) custausu_config('debug.channels.' . $channel, false);

    return [
      'enabled' => $enabled,
      $channel  => $chan,
      'on'      => (bool) ($enabled && $chan),
    ];
  }
}

if (!function_exists('custausu_js_lang')) {
  function custausu_js_lang(): string {
    return function_exists('custausu_current_lang')
      ? (string) custausu_current_lang()
      : (string) custausu_config('routes.policy.default_lang', 'en');
  }
}