<?php
/*
 * Mega519E view compatibility helpers.
 * This file must not define base_path(), url(), asset(), e(), or h().
 */

if (!function_exists('ec_h')) {
    function ec_h($value): string
    {
        return htmlspecialchars((string)($value ?? ''), ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('ec_attr')) {
    function ec_attr($value): string
    {
        return ec_h($value);
    }
}

if (!function_exists('ec_raw')) {
    function ec_raw($value): string
    {
        return (string)($value ?? '');
    }
}

if (!function_exists('ec_base_url')) {
    function ec_base_url(string $path = ''): string
    {
        if (function_exists('base_path')) {
            return base_path($path);
        }

        $base = '/easychair';
        return $path === '' ? $base : $base . '/' . ltrim($path, '/');
    }
}

if (!function_exists('ec_url')) {
    function ec_url(string $path = ''): string
    {
        return ec_base_url($path);
    }
}

if (!function_exists('ec_lang')) {
    function ec_lang(): string
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            @session_start();
        }

        $lang = $_GET['set'] ?? $_GET['lang'] ?? $_SESSION['lang'] ?? $_COOKIE['easychair_lang'] ?? 'ar';
        $lang = strtolower((string)$lang);

        if (!in_array($lang, ['ar', 'en'], true)) {
            $lang = 'ar';
        }

        $_SESSION['lang'] = $lang;

        return $lang;
    }
}

if (!function_exists('ec_is_rtl')) {
    function ec_is_rtl(): bool
    {
        return ec_lang() === 'ar';
    }
}

if (!function_exists('ec_dir')) {
    function ec_dir(): string
    {
        return ec_is_rtl() ? 'rtl' : 'ltr';
    }
}

if (!function_exists('ec_t')) {
    function ec_t(string $key, string $fallback = ''): string
    {
        if (function_exists('t')) {
            return t($key, $fallback);
        }

        return $fallback !== '' ? $fallback : $key;
    }
}

if (!function_exists('trans')) {
    function trans(string $key, string $fallback = ''): string
    {
        return function_exists('ec_t') ? ec_t($key, $fallback) : ($fallback !== '' ? $fallback : $key);
    }
}

if (!function_exists('ec_csrf_field')) {
    function ec_csrf_field(): string
    {
        return function_exists('csrf_field') ? csrf_field() : '';
    }
}

if (!function_exists('ec_old')) {
    function ec_old(string $field, $default = '')
    {
        return function_exists('old') ? old($field, $default) : $default;
    }
}

if (!function_exists('ec_selected')) {
    function ec_selected($current, $expected): string
    {
        return function_exists('selected') ? selected($current, $expected) : ((string)$current === (string)$expected ? 'selected' : '');
    }
}

if (!function_exists('ec_checked')) {
    function ec_checked($current, $expected = 1): string
    {
        return function_exists('checked') ? checked($current, $expected) : ((string)$current === (string)$expected ? 'checked' : '');
    }
}

if (!function_exists('ec_flash')) {
    function ec_flash(string $key, $default = null)
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            @session_start();
        }

        $value = $_SESSION[$key] ?? $default;
        unset($_SESSION[$key]);

        return $value;
    }
}

if (!function_exists('ec_badge')) {
    function ec_badge($value, string $class = 'secondary'): string
    {
        return '<span class="badge bg-' . ec_h($class) . '">' . ec_h($value) . '</span>';
    }
}

if (!function_exists('ec_yes_no')) {
    function ec_yes_no($value): string
    {
        return !empty($value) ? ec_t('common.yes', 'Yes') : ec_t('common.no', 'No');
    }
}
