File size: 4,549 Bytes
ffc4f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { CONFIG } from "./config.js";
import { createClient } from "https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2.45.4/+esm";

export const sb = createClient(CONFIG.SUPABASE_URL, CONFIG.SUPABASE_ANON_KEY);

const T = {
  sw: {
    picks: "Utabiri", admin: "Admin", login: "Ingia", logout: "Toka",
    freeTitle: "Utabiri wa Bure", comp: "Ligi", market: "Soko", all: "Zote",
    empty: "Hakuna utabiri uliochapishwa bado.", kickoff: "Muda wa kuanza", pick: "Utabiri",
    explain: "Maelezo", pending: "Inasubiri", won: "Umeshinda", lost: "Umeshindwa", void: "Umebatilika",
    disclaimer: "Haya ni maoni ya uchambuzi, si uhakika wa matokeo. Cheza kwa uwajibikaji.",
    record: "Rekodi", winRate: "Kiwango cha ushindi", settled: "yaliyokamilika",
    email: "Barua pepe", password: "Nenosiri", name: "Jina", signIn: "Ingia", signUp: "Jisajili",
    forgot: "Umesahau nenosiri?", sendReset: "Tuma kiungo cha kubadilisha", newPass: "Nenosiri jipya",
    repeatPass: "Rudia nenosiri", changePass: "Badilisha nenosiri", resetDone: "Nenosiri limebadilishwa. Ingia sasa.",
    checkMail: "Angalia barua pepe yako kuthibitisha akaunti.", resetSent: "Kiungo cha kubadilisha kimetumwa kwenye barua pepe yako.",
    badLink: "Kiungo hakifanyi kazi au kimeisha muda.", noAccess: "Huna ruhusa ya kuona ukurasa huu.",
    backHome: "Rudi kwenye utabiri", haveAccount: "Tayari una akaunti?", noAccount: "Huna akaunti?"
  },
  en: {
    picks: "Picks", admin: "Admin", login: "Sign in", logout: "Sign out",
    freeTitle: "Free Picks", comp: "League", market: "Market", all: "All",
    empty: "No published picks yet.", kickoff: "Kickoff", pick: "Pick",
    explain: "Explanation", pending: "Pending", won: "Won", lost: "Lost", void: "Void",
    disclaimer: "These are analytical opinions, not guaranteed results. Play responsibly.",
    record: "Record", winRate: "Win rate", settled: "settled",
    email: "Email", password: "Password", name: "Name", signIn: "Sign in", signUp: "Sign up",
    forgot: "Forgot password?", sendReset: "Send reset link", newPass: "New password",
    repeatPass: "Repeat password", changePass: "Change password", resetDone: "Password changed. Sign in now.",
    checkMail: "Check your email to confirm your account.", resetSent: "A reset link has been sent to your email.",
    badLink: "This link is invalid or has expired.", noAccess: "You do not have access to this page.",
    backHome: "Back to picks", haveAccount: "Already have an account?", noAccount: "No account yet?"
  }
};

export let lang = localStorage.getItem("fp_lang") || "sw";
export const t = k => T[lang][k] || k;

// Inatafsiri vipengele vyenye data-t="jina"
export function applyI18n() {
  document.documentElement.lang = lang;
  document.querySelectorAll("[data-t]").forEach(el => (el.textContent = t(el.dataset.t)));
}

export const esc = s => String(s ?? "").replace(/[&<>"']/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));

export const fmtDate = iso =>
  new Date(iso).toLocaleString(lang === "sw" ? "sw-TZ" : "en-GB", { dateStyle: "medium", timeStyle: "short" });

export async function currentProfile() {
  const { data: { session } } = await sb.auth.getSession();
  if (!session) return { session: null, profile: null };
  const { data: profile } = await sb.from("profiles").select("*").eq("id", session.user.id).maybeSingle();
  return { session, profile };
}

export function renderNav(active, profile) {
  const el = document.getElementById("nav");
  if (el) {
    el.innerHTML = `
      <div class="brand">FUTBOL PICKS<small>WIN SMART</small></div>
      <nav class="row">
        <a href="index.html" class="${active === "picks" ? "on" : ""}">${t("picks")}</a>
        ${profile?.role === "admin" ? `<a href="admin.html" class="${active === "admin" ? "on" : ""}">${t("admin")}</a>` : ""}
        ${profile
          ? `<button class="ghost" id="logoutBtn">${t("logout")}</button>`
          : `<a href="login.html">${t("login")}</a>`}
        <select id="langSel" style="width:auto">
          <option value="sw" ${lang === "sw" ? "selected" : ""}>Kiswahili</option>
          <option value="en" ${lang === "en" ? "selected" : ""}>English</option>
        </select>
      </nav>`;
    document.getElementById("langSel").onchange = e => {
      localStorage.setItem("fp_lang", e.target.value);
      location.reload();
    };
    document.getElementById("logoutBtn")?.addEventListener("click", async () => {
      await sb.auth.signOut();
      location.href = "index.html";
    });
  }
  applyI18n();
}