Skip to content

Automate browser based workflows with AI

5.6 KB
Raw Blame
import { GeoTarget, ProxyLocation } from "@/api/types";

export const SUPPORTED_COUNTRY_CODES = [
  "US",
  "AR",
  "AU",
  "BR",
  "CA",
  "DE",
  "ES",
  "FR",
  "GB",
  "IE",
  "IN",
  "IT",
  "JP",
  "MX",
  "NL",
  "NZ",
  "PH",
  "KR",
  "SA",
  "TR",
  "ZA",
] as const;

export type SupportedCountryCode = (typeof SUPPORTED_COUNTRY_CODES)[number];

export const COUNTRY_NAMES: Record<SupportedCountryCode, string> = {
  US: "United States",
  AR: "Argentina",
  AU: "Australia",
  BR: "Brazil",
  CA: "Canada",
  DE: "Germany",
  ES: "Spain",
  FR: "France",
  GB: "United Kingdom",
  IE: "Ireland",
  IN: "India",
  IT: "Italy",
  JP: "Japan",
  MX: "Mexico",
  NL: "Netherlands",
  NZ: "New Zealand",
  PH: "Philippines",
  KR: "South Korea",
  SA: "Saudi Arabia",
  TR: "Turkey",
  ZA: "South Africa",
};

export const COUNTRY_FLAGS: Record<SupportedCountryCode, string> = {
  US: "๐Ÿ‡บ๐Ÿ‡ธ",
  AR: "๐Ÿ‡ฆ๐Ÿ‡ท",
  AU: "๐Ÿ‡ฆ๐Ÿ‡บ",
  BR: "๐Ÿ‡ง๐Ÿ‡ท",
  CA: "๐Ÿ‡จ๐Ÿ‡ฆ",
  DE: "๐Ÿ‡ฉ๐Ÿ‡ช",
  ES: "๐Ÿ‡ช๐Ÿ‡ธ",
  FR: "๐Ÿ‡ซ๐Ÿ‡ท",
  GB: "๐Ÿ‡ฌ๐Ÿ‡ง",
  IE: "๐Ÿ‡ฎ๐Ÿ‡ช",
  IN: "๐Ÿ‡ฎ๐Ÿ‡ณ",
  IT: "๐Ÿ‡ฎ๐Ÿ‡น",
  JP: "๐Ÿ‡ฏ๐Ÿ‡ต",
  MX: "๐Ÿ‡ฒ๐Ÿ‡ฝ",
  NL: "๐Ÿ‡ณ๐Ÿ‡ฑ",
  NZ: "๐Ÿ‡ณ๐Ÿ‡ฟ",
  PH: "๐Ÿ‡ต๐Ÿ‡ญ",
  KR: "๐Ÿ‡ฐ๐Ÿ‡ท",
  SA: "๐Ÿ‡ธ๐Ÿ‡ฆ",
  TR: "๐Ÿ‡น๐Ÿ‡ท",
  ZA: "๐Ÿ‡ฟ๐Ÿ‡ฆ",
};

// Map legacy ProxyLocation to Country Code
const PROXY_LOCATION_TO_COUNTRY: Record<string, string> = {
  [ProxyLocation.Residential]: "US",
  [ProxyLocation.ResidentialISP]: "US",
  [ProxyLocation.ResidentialAR]: "AR",
  [ProxyLocation.ResidentialAU]: "AU",
  [ProxyLocation.ResidentialBR]: "BR",
  [ProxyLocation.ResidentialCA]: "CA",
  [ProxyLocation.ResidentialDE]: "DE",
  [ProxyLocation.ResidentialES]: "ES",
  [ProxyLocation.ResidentialFR]: "FR",
  [ProxyLocation.ResidentialGB]: "GB",
  [ProxyLocation.ResidentialIE]: "IE",
  [ProxyLocation.ResidentialIN]: "IN",
  [ProxyLocation.ResidentialIT]: "IT",
  [ProxyLocation.ResidentialJP]: "JP",
  [ProxyLocation.ResidentialMX]: "MX",
  [ProxyLocation.ResidentialNL]: "NL",
  [ProxyLocation.ResidentialNZ]: "NZ",
  [ProxyLocation.ResidentialPH]: "PH",
  [ProxyLocation.ResidentialKR]: "KR",
  [ProxyLocation.ResidentialSA]: "SA",
  [ProxyLocation.ResidentialTR]: "TR",
  [ProxyLocation.ResidentialZA]: "ZA",
};

// Reverse map for round-tripping simple country selections
const COUNTRY_TO_PROXY_LOCATION: Record<string, ProxyLocation> = {
  US: ProxyLocation.Residential,
  AR: ProxyLocation.ResidentialAR,
  AU: ProxyLocation.ResidentialAU,
  BR: ProxyLocation.ResidentialBR,
  CA: ProxyLocation.ResidentialCA,
  DE: ProxyLocation.ResidentialDE,
  ES: ProxyLocation.ResidentialES,
  FR: ProxyLocation.ResidentialFR,
  GB: ProxyLocation.ResidentialGB,
  IE: ProxyLocation.ResidentialIE,
  IN: ProxyLocation.ResidentialIN,
  IT: ProxyLocation.ResidentialIT,
  JP: ProxyLocation.ResidentialJP,
  MX: ProxyLocation.ResidentialMX,
  NL: ProxyLocation.ResidentialNL,
  NZ: ProxyLocation.ResidentialNZ,
  PH: ProxyLocation.ResidentialPH,
  KR: ProxyLocation.ResidentialKR,
  SA: ProxyLocation.ResidentialSA,
  TR: ProxyLocation.ResidentialTR,
  ZA: ProxyLocation.ResidentialZA,
};

export function proxyLocationToGeoTarget(
  input: ProxyLocation,
): GeoTarget | null {
  if (!input) return null;

  // If it's already a GeoTarget object
  if (typeof input === "object" && "country" in input) {
    return input;
  }

  // If it's a legacy string
  if (typeof input === "string") {
    if (input === ProxyLocation.None) return null;
    if (input === ProxyLocation.ResidentialISP) {
      return { country: "US", isISP: true };
    }
    const country = PROXY_LOCATION_TO_COUNTRY[input];
    if (country) {
      return { country };
    }
  }

  return null;
}

export function geoTargetToProxyLocationInput(
  target: GeoTarget | null,
): ProxyLocation {
  if (!target) return ProxyLocation.None;

  if (target.isISP) {
    return ProxyLocation.ResidentialISP;
  }

  // Guard against malformed subdivision (e.g., boolean instead of string)
  if (target.subdivision != null && typeof target.subdivision !== "string") {
    console.error(
      "[geoTargetToProxyLocationInput] Invalid subdivision:",
      target.subdivision,
    );
    target = { ...target, subdivision: undefined };
  }

  // Try to map back to legacy enum if it's just a country
  if (target.country && !target.subdivision && !target.city) {
    const legacyLocation = COUNTRY_TO_PROXY_LOCATION[target.country];
    if (legacyLocation) {
      return legacyLocation;
    }
  }

  // Otherwise return the object
  return target;
}

export function formatGeoTarget(target: GeoTarget | null): string {
  if (!target || !target.country) return "No Proxy";

  const parts = [];

  // Add Flag
  if (target.country in COUNTRY_FLAGS) {
    parts.push(COUNTRY_FLAGS[target.country as SupportedCountryCode]);
  }

  if (target.city) parts.push(target.city);
  if (target.subdivision) parts.push(target.subdivision);

  // Country Name
  const countryName =
    COUNTRY_NAMES[target.country as SupportedCountryCode] || target.country;
  if (target.isISP) {
    parts.push(`${countryName} (ISP)`);
  } else {
    parts.push(countryName);
  }

  return parts.join(" ");
}

export function formatGeoTargetCompact(target: GeoTarget | null): string {
  if (!target || !target.country) return "No Proxy";

  const parts = [];
  if (target.city) parts.push(target.city);
  if (target.subdivision) parts.push(target.subdivision);
  const countryName =
    COUNTRY_NAMES[target.country as SupportedCountryCode] || target.country;
  if (target.isISP) {
    parts.push(`${countryName} (ISP)`);
  } else {
    parts.push(countryName);
  }

  const text = parts.join(", ");
  const flag = COUNTRY_FLAGS[target.country as SupportedCountryCode] || "";

  return `${flag} ${text}`.trim();
}