const a = "Reserved IP", r = "ip-region-cache";
async function c(t) {
  const o = `https://ipapi.co/${t}/json/`, e = await (await fetch(o)).json();
  let i = "";
  return e.error ? e.reserved && (i = a) : i = [e.country_name, e.region, e.city].filter(Boolean).join("/"), i;
}
async function s(t) {
  const o = `https://api.ip.sb/geoip/${t}`, e = await (await fetch(o)).json();
  let i = "";
  return e.error ? e.reserved && (i = a) : i = [e.country, e.region, e.city].filter(Boolean).join("/"), i;
}
async function l(t) {
  let o = "", n = {};
  try {
    const e = localStorage.getItem(r);
    e && (n = JSON.parse(e));
  } catch {
    n = {};
  }
  if (n[t])
    o = n[t];
  else {
    try {
      o = await s(t);
    } catch {
      console.warn("Fail to get ip region from ip.sb");
    }
    if (!o)
      try {
        o = await c(t);
      } catch {
        console.warn("Fail to get ip region from ip-api.co");
      }
  }
  if (o) {
    const e = Object.keys(n);
    e.length > 100 && delete n[e[0]], n[t] && delete n[t], n[t] = o, localStorage.setItem(r, JSON.stringify(n));
  }
  return o;
}
async function g(t) {
  return Promise.all(t.map((o) => l(o)));
}
export {
  g as batchIp2Region,
  l as ip2Region
};
