/**
 * Tapops-local i18n — bilingual ID/EN for the document-repository sections.
 *
 * The shared assets/global/localization.js owns the nav/footer translations
 * and exposes window.getLang() plus a `languageChanged` event. Its internal
 * `translations` object is not extendable from here, so this layer keeps its
 * own dictionary under a separate `data-tp-i18n*` attribute namespace. The
 * global applyLang() ignores those attributes, and this script ignores the
 * global `data-i18n*` ones — no collisions.
 *
 * Exposes window.applyTp() and window.tpT(key) so main.js can translate the
 * dynamically-rendered document cards and state messages too.
 */
(function () {
    const dict = {
        id: {
            // Hero
            hero_kicker: "Digital Administrative Hub",
            hero_title: "Administrasi Akademik Digital",
            hero_tagline: "Satu pintu menuju semua dokumen penting. Akses terpusat menuju repositori dokumen akademik Program Studi Informatika Universitas Tanjungpura untuk mahasiswa dan staf.",
            hero_btn_search: "Cari Dokumen",
            hero_btn_flow: "ALUR PENGAJUAN",
            stat_active: "Dokumen Aktif",
            stat_updates: "Pembaruan Terbaru",

            // Repository — sidebar
            sidebar_category: "KATEGORI BERKAS",
            filter_all: "Semua Berkas",
            filter_skripsi: "Panduan Skripsi",
            filter_akademik: "Kalender Akademik",
            filter_praktik: "Kerja Praktik",
            filter_umum: "Formulir Umum",
            sidebar_info: "Pastikan mengunduh versi terbaru untuk menjamin validitas berkas pengajuan.",

            // Repository — controls
            repo_title: "Eksplorasi Dokumen Prodi",
            repo_subtitle: "Temukan berkas administrasi dan akademik terbaru Program Studi Informatika.",
            search_ph: "Cari nama berkas atau kata kunci...",
            label_period: "Periode Unggahan",
            range_divider: "s/d",
            label_sort: "Urutkan",
            sort_newest: "Terbaru",
            sort_oldest: "Terlama",
            sort_az: "Nama A-Z",
            loading: "Menghubungkan ke repositori berkas...",

            // Repository — dynamic (cards + states, used by main.js)
            card_open: "Buka Berkas",
            card_download: "Unduh Berkas",
            state_empty: "Belum ada dokumen yang tersedia di repositori.",
            state_error: "Gagal memuat berkas. Silakan periksa koneksi internet Anda atau coba lagi nanti.",

            // Procedures
            proc_title: "Tata Cara Pengajuan Berkas",
            proc_tag: "Alur Pengajuan",
            proc_s1_h: "Unduh Formulir",
            proc_s1_p: "Pilih kategori pengajuan Anda, lalu unduh formulir versi terbaru. Pastikan kode revisi pada formulir sesuai dengan yang tertera di halaman ini.",
            proc_s2_h: "Lengkapi Formulir",
            proc_s2_p: "Isi setiap kolom dengan lengkap dan teliti. Sertakan seluruh dokumen pendukung yang diminta sebelum pengiriman.",
            proc_s3_h: "Kirim Berkas",
            proc_s3_p: "Kirimkan formulir beserta dokumen pendukung melalui email resmi atau WhatsApp admin prodi. Berkas yang lengkap akan langsung diproses oleh admin prodi.",

            // Help Center
            help_title: "Butuh Bantuan Lebih Lanjut?",
            help_desc: "Tim admin kami siap membantu Anda melalui kanal komunikasi resmi di bawah ini.",
            help_email_title: "Korespondensi Resmi (Email)",
            help_email_desc: "Gunakan untuk permohonan form khusus non-publik atau administrasi formal lainnya.",
            help_email_c1: "Respon Maks. 24 Jam",
            help_email_c2: "Pelayanan Formal",
            help_email_btn: "HUBUNGI ADMIN",
            help_wa_title: "Quick Support (WhatsApp)",
            help_wa_desc: "Konsultasi cepat alur birokrasi atau pelaporan kendala akses pada berkas repositori.",
            help_wa_c1: "Respon Instan Jam Kerja",
            help_wa_c2: "Konsultasi Teknis",
            help_wa_btn: "CHAT WHATSAPP",
        },
        en: {
            // Hero
            hero_kicker: "Digital Administrative Hub",
            hero_title: "Digital Academic Administration",
            hero_tagline: "One gateway to every important document. A centralized access point to the academic document repository of the Informatics Department, Universitas Tanjungpura, for students and staff.",
            hero_btn_search: "Search Documents",
            hero_btn_flow: "SUBMISSION FLOW",
            stat_active: "Active Documents",
            stat_updates: "Recent Updates",

            // Repository — sidebar
            sidebar_category: "DOCUMENT CATEGORY",
            filter_all: "All Files",
            filter_skripsi: "Thesis Guides",
            filter_akademik: "Academic Calendar",
            filter_praktik: "Internship",
            filter_umum: "General Forms",
            sidebar_info: "Always download the latest version to ensure your submission files remain valid.",

            // Repository — controls
            repo_title: "Explore Department Documents",
            repo_subtitle: "Find the latest administrative and academic files of the Informatics Department.",
            search_ph: "Search file name or keyword...",
            label_period: "Upload Period",
            range_divider: "to",
            label_sort: "Sort by",
            sort_newest: "Newest",
            sort_oldest: "Oldest",
            sort_az: "Name A-Z",
            loading: "Connecting to the file repository...",

            // Repository — dynamic (cards + states, used by main.js)
            card_open: "Open File",
            card_download: "Download File",
            state_empty: "No documents are available in the repository yet.",
            state_error: "Failed to load files. Please check your internet connection or try again later.",

            // Procedures
            proc_title: "Document Submission Procedure",
            proc_tag: "Submission Flow",
            proc_s1_h: "Download the Form",
            proc_s1_p: "Choose your submission category, then download the latest version of the form. Make sure the revision code on the form matches the one shown on this page.",
            proc_s2_h: "Complete the Form",
            proc_s2_p: "Fill in every field completely and carefully. Include all required supporting documents before submitting.",
            proc_s3_h: "Submit the Document",
            proc_s3_p: "Send the form along with the supporting documents via the official email or the department admin's WhatsApp. Complete submissions are processed immediately by the department admin.",

            // Help Center
            help_title: "Need Further Assistance?",
            help_desc: "Our admin team is ready to help you through the official communication channels below.",
            help_email_title: "Official Correspondence (Email)",
            help_email_desc: "Use this for requests of non-public special forms or other formal administration.",
            help_email_c1: "Response within 24 Hours",
            help_email_c2: "Formal Service",
            help_email_btn: "CONTACT ADMIN",
            help_wa_title: "Quick Support (WhatsApp)",
            help_wa_desc: "Quick consultation on bureaucratic flow or reporting access issues with repository files.",
            help_wa_c1: "Instant Response in Work Hours",
            help_wa_c2: "Technical Consultation",
            help_wa_btn: "CHAT ON WHATSAPP",
        },
    };

    function currentLang() {
        if (typeof window.getLang === 'function') {
            const l = window.getLang();
            if (l === 'id' || l === 'en') return l;
        }
        try {
            const saved = localStorage.getItem('if_untan_lang');
            if (saved === 'id' || saved === 'en') return saved;
        } catch (e) { /* localStorage unavailable */ }
        const nav = (navigator.language || 'id').toLowerCase();
        return nav.startsWith('en') ? 'en' : 'id';
    }

    function tpT(key) {
        const lang = currentLang();
        return (dict[lang] && dict[lang][key]) || dict.id[key] || null;
    }

    function applyTp(root) {
        const scope = root || document;

        scope.querySelectorAll('[data-tp-i18n]').forEach(el => {
            const val = tpT(el.getAttribute('data-tp-i18n'));
            if (val != null) el.textContent = val;
        });

        scope.querySelectorAll('[data-tp-i18n-placeholder]').forEach(el => {
            const val = tpT(el.getAttribute('data-tp-i18n-placeholder'));
            if (val != null) el.placeholder = val;
        });
    }

    // Expose for main.js (dynamic cards/states).
    window.tpT = tpT;
    window.applyTp = applyTp;

    // Re-translate when the shared toggle switches language.
    document.addEventListener('languageChanged', () => applyTp());

    // Initial pass (runs after localization.js's own DOMContentLoaded handler,
    // so getLang() already reflects the resolved language).
    document.addEventListener('DOMContentLoaded', () => applyTp());
})();
