<?php

if (!function_exists('easychair_i18n_sidebar_label_key')) {
    function easychair_i18n_sidebar_label_key(string $label): ?string
    {
        $label = trim($label);

        $labelMap = [
            // Groups
            'إدارة المؤتمر' => 'menu.group.conference_management',
            'Conference Management' => 'menu.group.conference_management',
            'البريد والنظام' => 'menu.group.mail_system',
            'Mail & System' => 'menu.group.mail_system',
            'الحساب' => 'menu.group.account',
            'Account' => 'menu.group.account',

            // Core
            'لوحة التحكم' => 'menu.dashboard',
            'Dashboard' => 'menu.dashboard',
            'Conferences' => 'menu.conferences',
            'المؤتمرات' => 'menu.conferences',
            'المؤسسات' => 'menu.organizations',
            'Organizations' => 'menu.organizations',

            // Workflow
            'تنبيهات سير العمل' => 'menu.workflow_notifications',
            'Workflow Notifications' => 'menu.workflow_notifications',
            'قواعد مراحل التقديم' => 'menu.submission_stage_rules',
            'Submission Stage Rules' => 'menu.submission_stage_rules',
            'لوحة سير العمل' => 'menu.workflow_board',
            'Workflow Board' => 'menu.workflow_board',
            'سير العمل' => 'menu.workflow',
            'Workflow' => 'menu.workflow',

            // Mail
            'Conference Mail' => 'menu.conference_mail',
            'بريد المؤتمر' => 'menu.conference_mail',
            'قوالب بريد المؤتمر' => 'menu.conference_email_templates',
            'Conference Email Templates' => 'menu.conference_email_templates',
            'Email Templates' => 'menu.email_templates',
            'قوالب البريد' => 'menu.email_templates',
            'Test Email' => 'menu.test_email',
            'اختبار البريد' => 'menu.test_email',

            // Permissions / RBAC
            'صلاحيات أدوار المؤتمر' => 'menu.conference_role_permissions',
            'Conference Role Permissions' => 'menu.conference_role_permissions',
            'صلاحيات أعمال المؤتمر' => 'menu.conference_business_permissions',
            'Conference Business Permissions' => 'menu.conference_business_permissions',
            'RBAC Matrix' => 'menu.rbac_matrix',
            'مصفوفة الصلاحيات' => 'menu.rbac_matrix',
            'Dictionary' => 'menu.dictionary',
            'قاموس الترجمة' => 'menu.dictionary',
            'الحقوق المخصصة' => 'menu.custom_rights',
            'Custom Rights' => 'menu.custom_rights',

            // Reviews
            'مراقبة التحكيم' => 'menu.review_monitor',
            'Review Monitor' => 'menu.review_monitor',
            'Reviewer Expertise' => 'menu.reviewer_expertise',
            'خبرات المحكمين' => 'menu.reviewer_expertise',
            'Reviewers' => 'menu.reviewers',
            'المحكمون' => 'menu.reviewers',
            'نموذج التحكيم' => 'menu.review_form',
            'Review Form' => 'menu.review_form',
            'توزيع المحكمين' => 'menu.reviewer_assignment',
            'Reviewer Assignment' => 'menu.reviewer_assignment',

            // Profiles / notifications
            'Profile Completion' => 'menu.profile_completion',
            'اكتمال الملفات' => 'menu.profile_completion',
            'Profile Fields' => 'menu.profile_fields',
            'حقول الملف الأكاديمي' => 'menu.profile_fields',
            'ملفي الأكاديمي' => 'menu.my_academic_profile',
            'My Academic Profile' => 'menu.my_academic_profile',
            'إشعاراتي' => 'menu.my_notifications',
            'My Notifications' => 'menu.my_notifications',
            'Notification Broadcast' => 'menu.notification_broadcast',
            'إرسال إشعار' => 'menu.notification_broadcast',
            'Notification Digest' => 'menu.notification_digest',
            'ملخص الإشعارات' => 'menu.notification_digest',
            'Notification Audit' => 'menu.notification_audit',
            'تدقيق الإشعارات' => 'menu.notification_audit',
            'الإشعارات' => 'menu.notifications',
            'Notifications' => 'menu.notifications',

            // Submissions / decisions
            'بحث التقديمات' => 'menu.submission_search',
            'Submission Search' => 'menu.submission_search',
            'Submissions' => 'menu.submissions',
            'التقديمات' => 'menu.submissions',
            'Decisions' => 'menu.decisions',
            'القرارات' => 'menu.decisions',

            // Website / production
            'بناء الموقع' => 'menu.website_builder',
            'Website Builder' => 'menu.website_builder',
            'صور اللجان والمشاركين' => 'menu.committee_images',
            'Committee & Participant Images' => 'menu.committee_images',
            'الأعمال والجلسات والإصدار' => 'menu.business_sessions_issues',
            'Business, Sessions & Issues' => 'menu.business_sessions_issues',
            'الشعارات والهوية' => 'menu.branding',
            'Branding & Logos' => 'menu.branding',
            'الصفحة الذكية والمؤتمرات' => 'menu.smart_home_conferences',
            'Smart Homepage & Conferences' => 'menu.smart_home_conferences',
            'الصفحة الذكية' => 'menu.smart_homepage',
            'Smart Homepage' => 'menu.smart_homepage',
            'النشر' => 'menu.publishing',
            'Publishing' => 'menu.publishing',
            'Production / Camera-ready' => 'menu.production',
            'الإنتاج والنسخة النهائية' => 'menu.production',
            'Issues / Proceedings' => 'menu.issues',
            'وقائع المؤتمر' => 'menu.issues',
            'المسارات' => 'menu.conference_themes',
            // Users/account
            'إدارة المستخدمين' => 'menu.users_management',
            'Users Management' => 'menu.users_management',
            'Users' => 'menu.users',
            'المستخدمون' => 'menu.users',
            'تغيير المؤتمر' => 'menu.change_conference',
            'Change Conference' => 'menu.change_conference',
            'تسجيل الخروج' => 'menu.logout',
            'Logout' => 'menu.logout',
        ];

        return $labelMap[$label] ?? null;
    }
}

if (!function_exists('easychair_i18n_sidebar_url_key')) {
    function easychair_i18n_sidebar_url_key(string $url): ?string
    {
        $url = trim($url);

        $urlMap = [
            '/easychair/admin/dashboard' => 'menu.dashboard',
            '/easychair/admin/conferences' => 'menu.conferences',
            '/easychair/admin/organizations' => 'menu.organizations',
            '/easychair/admin/users' => 'menu.users_management',
            '/easychair/admin/roles' => 'menu.roles',

            '/easychair/admin/submissions' => 'menu.submissions',
            '/easychair/admin/submission-search' => 'menu.submission_search',
            '/easychair/admin/reviewers' => 'menu.reviewers',
            '/easychair/admin/reviewer-expertise' => 'menu.reviewer_expertise',
            '/easychair/admin/review-form' => 'menu.review_form',
            '/easychair/admin/review-monitor' => 'menu.review_monitor',
            '/easychair/admin/reviewer-assignments' => 'menu.reviewer_assignment',

            '/easychair/admin/workflow' => 'menu.workflow',
            '/easychair/admin/workflow-notifications' => 'menu.workflow_notifications',
            '/easychair/admin/submission-stage-rules' => 'menu.submission_stage_rules',
            '/easychair/admin/decisions' => 'menu.decisions',

            '/easychair/admin/email-templates' => 'menu.email_templates',
            '/easychair/admin/conference-email-templates' => 'menu.conference_email_templates',
            '/easychair/admin/conference-mail' => 'menu.conference_mail',
            '/easychair/admin/test-email' => 'menu.test_email',

            '/easychair/admin/notification-broadcast' => 'menu.notification_broadcast',
            '/easychair/admin/notification-audit' => 'menu.notification_audit',
            '/easychair/admin/notification-digest' => 'menu.notification_digest',
            '/easychair/notifications' => 'menu.my_notifications',
            '/easychair/notification-settings' => 'menu.notification_settings',

            '/easychair/admin/conference-home' => 'menu.smart_homepage',
            '/easychair/admin/committee-images' => 'menu.committee_images',
            '/easychair/admin/conference-business' => 'menu.business_sessions_issues',
            '/easychair/admin/branding' => 'menu.branding',
            '/easychair/admin/production' => 'menu.production',
            '/easychair/admin/issues' => 'menu.issues',
            '/easychair/admin/certificates' => 'menu.certificates',

            '/easychair/admin/profile-completion' => 'menu.profile_completion',
            '/easychair/admin/profile-fields' => 'menu.profile_fields',
            '/easychair/profile' => 'menu.my_academic_profile',

            '/easychair/admin/rbac-matrix' => 'menu.rbac_matrix',
            '/easychair/admin/dictionary' => 'menu.dictionary',
            '/easychair/admin/conference-role-permissions' => 'menu.conference_role_permissions',
            '/easychair/admin/conference-business-permissions' => 'menu.conference_business_permissions',
            '/easychair/admin/custom-rights' => 'menu.custom_rights',

            '/easychair/select-conference' => 'menu.change_conference',
            '/easychair/logout' => 'menu.logout',
        ];

        return $urlMap[$url] ?? null;
    }
}

if (!function_exists('easychair_i18n_sidebar_item')) {
    function easychair_i18n_sidebar_item(array $item): array
    {
        $key = $item['i18n_key'] ?? null;

        if (!$key && !empty($item['url'])) {
            $key = easychair_i18n_sidebar_url_key((string)$item['url']);
        }

        foreach (['label', 'title', 'heading', 'text', 'name'] as $field) {
            if (!$key && !empty($item[$field])) {
                $key = easychair_i18n_sidebar_label_key((string)$item[$field]);
            }
        }

        if ($key) {
            foreach (['label', 'title', 'heading', 'text', 'name'] as $field) {
                if (isset($item[$field])) {
                    $item[$field] = t($key, (string)$item[$field]);
                }
            }

            if (!isset($item['label'])) {
                $item['label'] = t($key, $key);
            }
        }

        foreach (['children', 'items', 'submenu', 'links'] as $childKey) {
            if (!empty($item[$childKey]) && is_array($item[$childKey])) {
                $item[$childKey] = easychair_i18n_sidebar_items($item[$childKey]);
            }
        }

        return $item;
    }
}

if (!function_exists('easychair_i18n_sidebar_items')) {
    function easychair_i18n_sidebar_items(array $items): array
    {
        foreach ($items as $k => $item) {
            if (is_array($item)) {
                $items[$k] = easychair_i18n_sidebar_item($item);
            } elseif (is_string($item)) {
                $key = easychair_i18n_sidebar_label_key($item);
                if ($key) {
                    $items[$k] = t($key, $item);
                }
            }
        }

        return $items;
    }
}

if (!function_exists('easychair_i18n_label')) {
    function easychair_i18n_label(string $label): string
    {
        $key = easychair_i18n_sidebar_label_key($label);
        return $key ? t($key, $label) : $label;
    }
}
