<?php
/**
 * Admin Language Loader
 * Include at the top of each admin page (after session_start).
 */
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Determine language
if (isset($_GET['lang']) && in_array($_GET['lang'], ['ar', 'en'])) {
    $_SESSION['lang'] = $_GET['lang'];
}
$currentLang = $_SESSION['lang'] ?? 'ar';

// Load translation array
$langFile = dirname(__DIR__, 2) . '/lang/' . $currentLang . '.php';
$t = file_exists($langFile) ? require $langFile : require dirname(__DIR__, 2) . '/lang/ar.php';

$isRTL = ($t['dir'] === 'rtl');
$bootstrapCSS = $isRTL
    ? 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css'
    : 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css';
$bootstrapIntegrity = '';
// Using CDN HTTPS for Bootstrap; integrity left empty to avoid invalid-SRI blocking locally.

function __($key) {
    global $t;
    return $t[$key] ?? $key;
}

function lang_switch_url($targetLang) {
    $params = $_GET;
    $params['lang'] = $targetLang;
    return '?' . http_build_query($params);
}
