<?php
$supported = ['ar', 'en', 'fr'];
$default   = 'ar';

if (isset($_GET['lang']) && in_array($_GET['lang'], $supported)) {
    $lang = $_GET['lang'];
    setcookie('tahwissa_lang', $lang, time() + 60*60*24*365, '/');
} elseif (isset($_COOKIE['tahwissa_lang']) && in_array($_COOKIE['tahwissa_lang'], $supported)) {
    $lang = $_COOKIE['tahwissa_lang'];
} else {
    $lang = $default;
}

$t = require __DIR__ . '/../lang/' . $lang . '.php';

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

function lang_url(string $code): string {
    $params = $_GET;
    $params['lang'] = $code;
    return '?' . http_build_query($params);
}

function is_rtl(): bool {
    global $lang;
    return $lang === 'ar';
}
