import type { TCountryMeta, TImpactStats } from "./types";

export const IMPACT_ENDPOINT =
  "https://ctx-wire-telemetry.iweb-ivanov.workers.dev/v1/impact";

// Stars + stargazers are proxied through a dedicated worker so visitors never
// hit GitHub's 60/hr unauthenticated limit (worker caches 1h, stale 7d on 403).
export const COMMUNITY_ENDPOINT =
  "https://ctx-wire-community.iweb-ivanov.workers.dev/v1/community";

// /v1/impact is edge-cached for ~5 min, so polling faster than that just re-fetches
// identical bytes while spending Worker requests (100k/day on the free tier). 30s
// stays live enough and is ~6x lighter; useImpact also pauses polling entirely
// while the tab is hidden, so only a visible tab ever hits the endpoint.
export const POLL_MS = 30000;

// USD per 1M tokens for the site-side savings estimate (the CLI sends tokens,
// never dollars). Swappable; always shown as a labeled estimate.
export const TOKEN_PRICE_PER_M = 3;

// Public repo; stars + stargazers come from the GitHub API.
export const STARGAZER_REPO = "pivanov/ctx-wire";

export const emptyStats: TImpactStats = {
  totals: {},
  programs: [],
  countries: [],
};

// Country centroids (ISO 3166-1 alpha-2), matching the Cloudflare geo codes the
// worker stores. Full inhabited set so any country with real data renders a
// marker; uninhabited codes (AQ/BV/HM/GS/UM) are intentionally omitted.
export const countryMeta: Record<string, TCountryMeta> = {
  AD: { name: "Andorra", lat: 42.55, lng: 1.6 },
  AE: { name: "United Arab Emirates", lat: 23.42, lng: 53.85 },
  AF: { name: "Afghanistan", lat: 33.94, lng: 67.71 },
  AG: { name: "Antigua and Barbuda", lat: 17.06, lng: -61.8 },
  AI: { name: "Anguilla", lat: 18.22, lng: -63.07 },
  AL: { name: "Albania", lat: 41.15, lng: 20.17 },
  AM: { name: "Armenia", lat: 40.07, lng: 45.04 },
  AO: { name: "Angola", lat: -11.2, lng: 17.87 },
  AR: { name: "Argentina", lat: -38.42, lng: -63.62 },
  AS: { name: "American Samoa", lat: -14.27, lng: -170.13 },
  AT: { name: "Austria", lat: 47.52, lng: 14.55 },
  AU: { name: "Australia", lat: -25.27, lng: 133.78 },
  AW: { name: "Aruba", lat: 12.52, lng: -69.97 },
  AX: { name: "Åland Islands", lat: 60.18, lng: 19.92 },
  AZ: { name: "Azerbaijan", lat: 40.14, lng: 47.58 },
  BA: { name: "Bosnia and Herzegovina", lat: 43.92, lng: 17.68 },
  BB: { name: "Barbados", lat: 13.19, lng: -59.54 },
  BD: { name: "Bangladesh", lat: 23.68, lng: 90.36 },
  BE: { name: "Belgium", lat: 50.5, lng: 4.47 },
  BF: { name: "Burkina Faso", lat: 12.24, lng: -1.56 },
  BG: { name: "Bulgaria", lat: 42.73, lng: 25.49 },
  BH: { name: "Bahrain", lat: 25.93, lng: 50.64 },
  BI: { name: "Burundi", lat: -3.37, lng: 29.92 },
  BJ: { name: "Benin", lat: 9.31, lng: 2.32 },
  BL: { name: "Saint Barthélemy", lat: 17.9, lng: -62.83 },
  BM: { name: "Bermuda", lat: 32.32, lng: -64.76 },
  BN: { name: "Brunei", lat: 4.54, lng: 114.73 },
  BO: { name: "Bolivia", lat: -16.29, lng: -63.59 },
  BQ: { name: "Caribbean Netherlands", lat: 12.18, lng: -68.24 },
  BR: { name: "Brazil", lat: -14.24, lng: -51.93 },
  BS: { name: "Bahamas", lat: 25.03, lng: -77.4 },
  BT: { name: "Bhutan", lat: 27.51, lng: 90.43 },
  BW: { name: "Botswana", lat: -22.33, lng: 24.68 },
  BY: { name: "Belarus", lat: 53.71, lng: 27.95 },
  BZ: { name: "Belize", lat: 17.19, lng: -88.5 },
  CA: { name: "Canada", lat: 56.13, lng: -106.35 },
  CC: { name: "Cocos Islands", lat: -12.16, lng: 96.87 },
  CD: { name: "DR Congo", lat: -4.04, lng: 21.76 },
  CF: { name: "Central African Republic", lat: 6.61, lng: 20.94 },
  CG: { name: "Congo", lat: -0.23, lng: 15.83 },
  CH: { name: "Switzerland", lat: 46.82, lng: 8.23 },
  CI: { name: "Côte d'Ivoire", lat: 7.54, lng: -5.55 },
  CK: { name: "Cook Islands", lat: -21.24, lng: -159.78 },
  CL: { name: "Chile", lat: -35.68, lng: -71.54 },
  CM: { name: "Cameroon", lat: 7.37, lng: 12.35 },
  CN: { name: "China", lat: 35.86, lng: 104.2 },
  CO: { name: "Colombia", lat: 4.57, lng: -74.3 },
  CR: { name: "Costa Rica", lat: 9.75, lng: -83.75 },
  CU: { name: "Cuba", lat: 21.52, lng: -77.78 },
  CV: { name: "Cape Verde", lat: 16.0, lng: -24.01 },
  CW: { name: "Curaçao", lat: 12.17, lng: -68.99 },
  CX: { name: "Christmas Island", lat: -10.45, lng: 105.69 },
  CY: { name: "Cyprus", lat: 35.13, lng: 33.43 },
  CZ: { name: "Czechia", lat: 49.82, lng: 15.47 },
  DE: { name: "Germany", lat: 51.17, lng: 10.45 },
  DJ: { name: "Djibouti", lat: 11.83, lng: 42.59 },
  DK: { name: "Denmark", lat: 56.26, lng: 9.5 },
  DM: { name: "Dominica", lat: 15.41, lng: -61.37 },
  DO: { name: "Dominican Republic", lat: 18.74, lng: -70.16 },
  DZ: { name: "Algeria", lat: 28.03, lng: 1.66 },
  EC: { name: "Ecuador", lat: -1.83, lng: -78.18 },
  EE: { name: "Estonia", lat: 58.6, lng: 25.01 },
  EG: { name: "Egypt", lat: 26.82, lng: 30.8 },
  EH: { name: "Western Sahara", lat: 24.22, lng: -12.89 },
  ER: { name: "Eritrea", lat: 15.18, lng: 39.78 },
  ES: { name: "Spain", lat: 40.46, lng: -3.75 },
  ET: { name: "Ethiopia", lat: 9.15, lng: 40.49 },
  FI: { name: "Finland", lat: 61.92, lng: 25.75 },
  FJ: { name: "Fiji", lat: -16.58, lng: 179.41 },
  FK: { name: "Falkland Islands", lat: -51.8, lng: -59.52 },
  FM: { name: "Micronesia", lat: 7.43, lng: 150.55 },
  FO: { name: "Faroe Islands", lat: 61.89, lng: -6.91 },
  FR: { name: "France", lat: 46.23, lng: 2.21 },
  GA: { name: "Gabon", lat: -0.8, lng: 11.61 },
  GB: { name: "United Kingdom", lat: 55.38, lng: -3.44 },
  GD: { name: "Grenada", lat: 12.12, lng: -61.68 },
  GE: { name: "Georgia", lat: 42.32, lng: 43.36 },
  GF: { name: "French Guiana", lat: 3.93, lng: -53.13 },
  GG: { name: "Guernsey", lat: 49.47, lng: -2.59 },
  GH: { name: "Ghana", lat: 7.95, lng: -1.02 },
  GI: { name: "Gibraltar", lat: 36.14, lng: -5.35 },
  GL: { name: "Greenland", lat: 71.71, lng: -42.6 },
  GM: { name: "Gambia", lat: 13.44, lng: -15.31 },
  GN: { name: "Guinea", lat: 9.95, lng: -9.7 },
  GP: { name: "Guadeloupe", lat: 16.27, lng: -61.55 },
  GQ: { name: "Equatorial Guinea", lat: 1.65, lng: 10.27 },
  GR: { name: "Greece", lat: 39.07, lng: 21.82 },
  GT: { name: "Guatemala", lat: 15.78, lng: -90.23 },
  GU: { name: "Guam", lat: 13.44, lng: 144.79 },
  GW: { name: "Guinea-Bissau", lat: 11.8, lng: -15.18 },
  GY: { name: "Guyana", lat: 4.86, lng: -58.93 },
  HK: { name: "Hong Kong", lat: 22.4, lng: 114.11 },
  HN: { name: "Honduras", lat: 15.2, lng: -86.24 },
  HR: { name: "Croatia", lat: 45.1, lng: 15.2 },
  HT: { name: "Haiti", lat: 18.97, lng: -72.29 },
  HU: { name: "Hungary", lat: 47.16, lng: 19.5 },
  ID: { name: "Indonesia", lat: -0.79, lng: 113.92 },
  IE: { name: "Ireland", lat: 53.41, lng: -8.24 },
  IL: { name: "Israel", lat: 31.05, lng: 34.85 },
  IM: { name: "Isle of Man", lat: 54.24, lng: -4.55 },
  IN: { name: "India", lat: 20.59, lng: 78.96 },
  IQ: { name: "Iraq", lat: 33.22, lng: 43.68 },
  IR: { name: "Iran", lat: 32.43, lng: 53.69 },
  IS: { name: "Iceland", lat: 64.96, lng: -19.02 },
  IT: { name: "Italy", lat: 41.87, lng: 12.57 },
  JE: { name: "Jersey", lat: 49.21, lng: -2.13 },
  JM: { name: "Jamaica", lat: 18.11, lng: -77.3 },
  JO: { name: "Jordan", lat: 30.59, lng: 36.24 },
  JP: { name: "Japan", lat: 36.2, lng: 138.25 },
  KE: { name: "Kenya", lat: -0.02, lng: 37.91 },
  KG: { name: "Kyrgyzstan", lat: 41.2, lng: 74.77 },
  KH: { name: "Cambodia", lat: 12.57, lng: 104.99 },
  KI: { name: "Kiribati", lat: -3.37, lng: -168.73 },
  KM: { name: "Comoros", lat: -11.65, lng: 43.33 },
  KN: { name: "Saint Kitts and Nevis", lat: 17.36, lng: -62.78 },
  KP: { name: "North Korea", lat: 40.34, lng: 127.51 },
  KR: { name: "South Korea", lat: 35.91, lng: 127.77 },
  KW: { name: "Kuwait", lat: 29.31, lng: 47.48 },
  KY: { name: "Cayman Islands", lat: 19.31, lng: -81.25 },
  KZ: { name: "Kazakhstan", lat: 48.02, lng: 66.92 },
  LA: { name: "Laos", lat: 19.86, lng: 102.5 },
  LB: { name: "Lebanon", lat: 33.85, lng: 35.86 },
  LC: { name: "Saint Lucia", lat: 13.91, lng: -60.98 },
  LI: { name: "Liechtenstein", lat: 47.17, lng: 9.56 },
  LK: { name: "Sri Lanka", lat: 7.87, lng: 80.77 },
  LR: { name: "Liberia", lat: 6.43, lng: -9.43 },
  LS: { name: "Lesotho", lat: -29.61, lng: 28.23 },
  LT: { name: "Lithuania", lat: 55.17, lng: 23.88 },
  LU: { name: "Luxembourg", lat: 49.82, lng: 6.13 },
  LV: { name: "Latvia", lat: 56.88, lng: 24.6 },
  LY: { name: "Libya", lat: 26.34, lng: 17.23 },
  MA: { name: "Morocco", lat: 31.79, lng: -7.09 },
  MC: { name: "Monaco", lat: 43.75, lng: 7.41 },
  MD: { name: "Moldova", lat: 47.41, lng: 28.37 },
  ME: { name: "Montenegro", lat: 42.71, lng: 19.37 },
  MF: { name: "Saint Martin", lat: 18.08, lng: -63.05 },
  MG: { name: "Madagascar", lat: -18.77, lng: 46.87 },
  MH: { name: "Marshall Islands", lat: 7.13, lng: 171.18 },
  MK: { name: "North Macedonia", lat: 41.61, lng: 21.75 },
  ML: { name: "Mali", lat: 17.57, lng: -4.0 },
  MM: { name: "Myanmar", lat: 21.91, lng: 95.96 },
  MN: { name: "Mongolia", lat: 46.86, lng: 103.85 },
  MO: { name: "Macau", lat: 22.2, lng: 113.54 },
  MP: { name: "Northern Mariana Islands", lat: 17.33, lng: 145.38 },
  MQ: { name: "Martinique", lat: 14.64, lng: -61.02 },
  MR: { name: "Mauritania", lat: 21.01, lng: -10.94 },
  MS: { name: "Montserrat", lat: 16.74, lng: -62.19 },
  MT: { name: "Malta", lat: 35.94, lng: 14.38 },
  MU: { name: "Mauritius", lat: -20.35, lng: 57.55 },
  MV: { name: "Maldives", lat: 3.2, lng: 73.22 },
  MW: { name: "Malawi", lat: -13.25, lng: 34.3 },
  MX: { name: "Mexico", lat: 23.63, lng: -102.55 },
  MY: { name: "Malaysia", lat: 4.21, lng: 101.98 },
  MZ: { name: "Mozambique", lat: -18.67, lng: 35.53 },
  NA: { name: "Namibia", lat: -22.96, lng: 18.49 },
  NC: { name: "New Caledonia", lat: -20.9, lng: 165.62 },
  NE: { name: "Niger", lat: 17.61, lng: 8.08 },
  NF: { name: "Norfolk Island", lat: -29.04, lng: 167.95 },
  NG: { name: "Nigeria", lat: 9.08, lng: 8.68 },
  NI: { name: "Nicaragua", lat: 12.87, lng: -85.21 },
  NL: { name: "Netherlands", lat: 52.13, lng: 5.29 },
  NO: { name: "Norway", lat: 60.47, lng: 8.47 },
  NP: { name: "Nepal", lat: 28.39, lng: 84.12 },
  NR: { name: "Nauru", lat: -0.52, lng: 166.93 },
  NU: { name: "Niue", lat: -19.05, lng: -169.87 },
  NZ: { name: "New Zealand", lat: -40.9, lng: 174.89 },
  OM: { name: "Oman", lat: 21.51, lng: 55.92 },
  PA: { name: "Panama", lat: 8.54, lng: -80.78 },
  PE: { name: "Peru", lat: -9.19, lng: -75.02 },
  PF: { name: "French Polynesia", lat: -17.68, lng: -149.41 },
  PG: { name: "Papua New Guinea", lat: -6.31, lng: 143.96 },
  PH: { name: "Philippines", lat: 12.88, lng: 121.77 },
  PK: { name: "Pakistan", lat: 30.38, lng: 69.35 },
  PL: { name: "Poland", lat: 51.92, lng: 19.15 },
  PM: { name: "Saint Pierre and Miquelon", lat: 46.94, lng: -56.27 },
  PR: { name: "Puerto Rico", lat: 18.22, lng: -66.59 },
  PS: { name: "Palestine", lat: 31.95, lng: 35.23 },
  PT: { name: "Portugal", lat: 39.4, lng: -8.22 },
  PW: { name: "Palau", lat: 7.51, lng: 134.58 },
  PY: { name: "Paraguay", lat: -23.44, lng: -58.44 },
  QA: { name: "Qatar", lat: 25.35, lng: 51.18 },
  RE: { name: "Réunion", lat: -21.12, lng: 55.54 },
  RO: { name: "Romania", lat: 45.94, lng: 24.97 },
  RS: { name: "Serbia", lat: 44.02, lng: 21.01 },
  RU: { name: "Russia", lat: 61.52, lng: 105.32 },
  RW: { name: "Rwanda", lat: -1.94, lng: 29.87 },
  SA: { name: "Saudi Arabia", lat: 23.89, lng: 45.08 },
  SB: { name: "Solomon Islands", lat: -9.65, lng: 160.16 },
  SC: { name: "Seychelles", lat: -4.68, lng: 55.49 },
  SD: { name: "Sudan", lat: 12.86, lng: 30.22 },
  SE: { name: "Sweden", lat: 60.13, lng: 18.64 },
  SG: { name: "Singapore", lat: 1.35, lng: 103.82 },
  SH: { name: "Saint Helena", lat: -24.14, lng: -10.03 },
  SI: { name: "Slovenia", lat: 46.15, lng: 14.99 },
  SJ: { name: "Svalbard", lat: 77.55, lng: 23.67 },
  SK: { name: "Slovakia", lat: 48.67, lng: 19.7 },
  SL: { name: "Sierra Leone", lat: 8.46, lng: -11.78 },
  SM: { name: "San Marino", lat: 43.94, lng: 12.46 },
  SN: { name: "Senegal", lat: 14.5, lng: -14.45 },
  SO: { name: "Somalia", lat: 5.15, lng: 46.2 },
  SR: { name: "Suriname", lat: 3.92, lng: -56.03 },
  SS: { name: "South Sudan", lat: 6.88, lng: 31.31 },
  ST: { name: "São Tomé and Príncipe", lat: 0.19, lng: 6.61 },
  SV: { name: "El Salvador", lat: 13.79, lng: -88.9 },
  SX: { name: "Sint Maarten", lat: 18.04, lng: -63.07 },
  SY: { name: "Syria", lat: 34.8, lng: 39.0 },
  SZ: { name: "Eswatini", lat: -26.52, lng: 31.47 },
  TC: { name: "Turks and Caicos Islands", lat: 21.69, lng: -71.8 },
  TD: { name: "Chad", lat: 15.45, lng: 18.73 },
  TG: { name: "Togo", lat: 8.62, lng: 0.82 },
  TH: { name: "Thailand", lat: 15.87, lng: 100.99 },
  TJ: { name: "Tajikistan", lat: 38.86, lng: 71.28 },
  TK: { name: "Tokelau", lat: -8.97, lng: -171.86 },
  TL: { name: "Timor-Leste", lat: -8.87, lng: 125.73 },
  TM: { name: "Turkmenistan", lat: 38.97, lng: 59.56 },
  TN: { name: "Tunisia", lat: 33.89, lng: 9.54 },
  TO: { name: "Tonga", lat: -21.18, lng: -175.2 },
  TR: { name: "Turkey", lat: 38.96, lng: 35.24 },
  TT: { name: "Trinidad and Tobago", lat: 10.69, lng: -61.22 },
  TV: { name: "Tuvalu", lat: -7.11, lng: 177.65 },
  TW: { name: "Taiwan", lat: 23.7, lng: 120.96 },
  TZ: { name: "Tanzania", lat: -6.37, lng: 34.89 },
  UA: { name: "Ukraine", lat: 48.38, lng: 31.17 },
  UG: { name: "Uganda", lat: 1.37, lng: 32.29 },
  US: { name: "United States", lat: 37.09, lng: -95.71 },
  UY: { name: "Uruguay", lat: -32.52, lng: -55.77 },
  UZ: { name: "Uzbekistan", lat: 41.38, lng: 64.59 },
  VA: { name: "Vatican City", lat: 41.9, lng: 12.45 },
  VC: { name: "Saint Vincent and the Grenadines", lat: 12.98, lng: -61.29 },
  VE: { name: "Venezuela", lat: 6.42, lng: -66.59 },
  VG: { name: "British Virgin Islands", lat: 18.42, lng: -64.64 },
  VI: { name: "U.S. Virgin Islands", lat: 18.34, lng: -64.9 },
  VN: { name: "Vietnam", lat: 14.06, lng: 108.28 },
  VU: { name: "Vanuatu", lat: -15.38, lng: 166.96 },
  WF: { name: "Wallis and Futuna", lat: -13.77, lng: -177.16 },
  WS: { name: "Samoa", lat: -13.76, lng: -172.1 },
  YE: { name: "Yemen", lat: 15.55, lng: 48.52 },
  YT: { name: "Mayotte", lat: -12.83, lng: 45.17 },
  ZA: { name: "South Africa", lat: -30.56, lng: 22.94 },
  ZM: { name: "Zambia", lat: -13.13, lng: 27.85 },
  ZW: { name: "Zimbabwe", lat: -19.02, lng: 29.15 },
};
