export default function usePhoneCountries() {
  const countries: TPhoneCountry[] = [
    // Asia Pacific
    {
      _id: "sg",
      country: "Singapore",
      dialCode: "+65",
      minLength: 8,
      maxLength: 8,
      placeholder: "8123 4567",
      format: /^[89]\d{7}$/,
      label: "+65",
    },
    {
      _id: "my",
      country: "Malaysia",
      dialCode: "+60",
      minLength: 9,
      maxLength: 11,
      placeholder: "12 345 6789",
      format: /^1[0-9]\d{7,9}$/,
      label: "+60",
    },
    {
      _id: "ph",
      country: "Philippines",
      dialCode: "+63",
      minLength: 10,
      maxLength: 10,
      placeholder: "917 123 4567",
      format: /^9\d{9}$/,
      label: "+63",
    },
    {
      _id: "_id",
      country: "Indonesia",
      dialCode: "+62",
      minLength: 9,
      maxLength: 12,
      placeholder: "812 3456 789",
      format: /^8\d{8,11}$/,
      label: "+62",
    },
    {
      _id: "th",
      country: "Thailand",
      dialCode: "+66",
      minLength: 9,
      maxLength: 9,
      placeholder: "81 234 5678",
      format: /^[689]\d{8}$/,
      label: "+66",
    },
    {
      _id: "vn",
      country: "Vietnam",
      dialCode: "+84",
      minLength: 9,
      maxLength: 10,
      placeholder: "912 345 678",
      format: /^[3-9]\d{8,9}$/,
      label: "+84",
    },
    {
      _id: "kh",
      country: "Cambodia",
      dialCode: "+855",
      minLength: 8,
      maxLength: 9,
      placeholder: "12 345 678",
      format: /^[1-9]\d{7,8}$/,
      label: "+855",
    },
    {
      _id: "la",
      country: "Laos",
      dialCode: "+856",
      minLength: 8,
      maxLength: 10,
      placeholder: "20 1234 567",
      format: /^[2-9]\d{7,9}$/,
      label: "+856",
    },
    {
      _id: "mm",
      country: "Myanmar",
      dialCode: "+95",
      minLength: 9,
      maxLength: 10,
      placeholder: "9 1234 5678",
      format: /^9\d{8,9}$/,
      label: "+95",
    },
    {
      _id: "bn",
      country: "Brunei",
      dialCode: "+673",
      minLength: 7,
      maxLength: 7,
      placeholder: "712 3456",
      format: /^[2-8]\d{6}$/,
      label: "+673",
    },

    // Oceania
    {
      _id: "au",
      country: "Australia",
      dialCode: "+61",
      minLength: 9,
      maxLength: 9,
      placeholder: "412 345 678",
      format: /^[2-9]\d{8}$/,
      label: "+61",
    },
    {
      _id: "nz",
      country: "New Zealand",
      dialCode: "+64",
      minLength: 8,
      maxLength: 9,
      placeholder: "21 123 456",
      format: /^[2-9]\d{7,8}$/,
      label: "+64",
    },
    {
      _id: "pg",
      country: "Papua New Guinea",
      dialCode: "+675",
      minLength: 8,
      maxLength: 8,
      placeholder: "7012 3456",
      format: /^[7-9]\d{7}$/,
      label: "+675",
    },
    {
      _id: "fj",
      country: "Fiji",
      dialCode: "+679",
      minLength: 7,
      maxLength: 7,
      placeholder: "712 3456",
      format: /^[7-9]\d{6}$/,
      label: "+679",
    },

    // North America
    {
      _id: "us",
      country: "United States",
      dialCode: "+1",
      minLength: 10,
      maxLength: 10,
      placeholder: "(555) 123-4567",
      format: /^\d{10}$/,
      label: "+1 (US)",
    },
    {
      _id: "ca",
      country: "Canada",
      dialCode: "+1",
      minLength: 10,
      maxLength: 10,
      placeholder: "(416) 123-4567",
      format: /^\d{10}$/,
      label: "+1 (CA)",
    },

    // Europe
    {
      _id: "gb",
      country: "United Kingdom",
      dialCode: "+44",
      minLength: 10,
      maxLength: 11,
      placeholder: "7700 900123",
      format: /^[1-9]\d{9,10}$/,
      label: "+44",
    },
    {
      _id: "de",
      country: "Germany",
      dialCode: "+49",
      minLength: 10,
      maxLength: 12,
      placeholder: "151 12345678",
      format: /^[1-9]\d{9,11}$/,
      label: "+49",
    },
    {
      _id: "fr",
      country: "France",
      dialCode: "+33",
      minLength: 9,
      maxLength: 9,
      placeholder: "6 12 34 56 78",
      format: /^[1-9]\d{8}$/,
      label: "+33",
    },
    {
      _id: "it",
      country: "Italy",
      dialCode: "+39",
      minLength: 9,
      maxLength: 11,
      placeholder: "312 345 6789",
      format: /^3\d{8,10}$/,
      label: "+39",
    },
    {
      _id: "es",
      country: "Spain",
      dialCode: "+34",
      minLength: 9,
      maxLength: 9,
      placeholder: "612 34 56 78",
      format: /^[6-9]\d{8}$/,
      label: "+34",
    },
    {
      _id: "nl",
      country: "Netherlands",
      dialCode: "+31",
      minLength: 9,
      maxLength: 9,
      placeholder: "6 12345678",
      format: /^6\d{8}$/,
      label: "+31",
    },
    {
      _id: "ch",
      country: "Switzerland",
      dialCode: "+41",
      minLength: 9,
      maxLength: 9,
      placeholder: "78 123 45 67",
      format: /^[7-9]\d{8}$/,
      label: "+41",
    },
    {
      _id: "at",
      country: "Austria",
      dialCode: "+43",
      minLength: 10,
      maxLength: 11,
      placeholder: "664 123456",
      format: /^[6-9]\d{9,10}$/,
      label: "+43",
    },
    {
      _id: "be",
      country: "Belgium",
      dialCode: "+32",
      minLength: 9,
      maxLength: 9,
      placeholder: "470 12 34 56",
      format: /^4\d{8}$/,
      label: "+32",
    },
    {
      _id: "se",
      country: "Sweden",
      dialCode: "+46",
      minLength: 9,
      maxLength: 9,
      placeholder: "70 123 45 67",
      format: /^7\d{8}$/,
      label: "+46",
    },
    {
      _id: "no",
      country: "Norway",
      dialCode: "+47",
      minLength: 8,
      maxLength: 8,
      placeholder: "412 34 567",
      format: /^[4-9]\d{7}$/,
      label: "+47",
    },
    {
      _id: "dk",
      country: "Denmark",
      dialCode: "+45",
      minLength: 8,
      maxLength: 8,
      placeholder: "20 12 34 56",
      format: /^[2-9]\d{7}$/,
      label: "+45",
    },
    {
      _id: "fi",
      country: "Finland",
      dialCode: "+358",
      minLength: 9,
      maxLength: 10,
      placeholder: "50 123 4567",
      format: /^[4-5]\d{8,9}$/,
      label: "+358",
    },

    // Middle East
    {
      _id: "sa",
      country: "Saudi Arabia",
      dialCode: "+966",
      minLength: 9,
      maxLength: 9,
      placeholder: "50 123 4567",
      format: /^5\d{8}$/,
      label: "+966",
    },
    {
      _id: "ae",
      country: "United Arab Emirates",
      dialCode: "+971",
      minLength: 9,
      maxLength: 9,
      placeholder: "50 123 4567",
      format: /^5\d{8}$/,
      label: "+971",
    },
    {
      _id: "qa",
      country: "Qatar",
      dialCode: "+974",
      minLength: 8,
      maxLength: 8,
      placeholder: "3012 3456",
      format: /^[3-7]\d{7}$/,
      label: "+974",
    },
    {
      _id: "kw",
      country: "Kuwait",
      dialCode: "+965",
      minLength: 8,
      maxLength: 8,
      placeholder: "5012 3456",
      format: /^[569]\d{7}$/,
      label: "+965",
    },
    {
      _id: "bh",
      country: "Bahrain",
      dialCode: "+973",
      minLength: 8,
      maxLength: 8,
      placeholder: "3612 3456",
      format: /^[3-9]\d{7}$/,
      label: "+973",
    },
    {
      _id: "om",
      country: "Oman",
      dialCode: "+968",
      minLength: 8,
      maxLength: 8,
      placeholder: "9012 3456",
      format: /^[79]\d{7}$/,
      label: "+968",
    },

    // East Asia
    {
      _id: "cn",
      country: "China",
      dialCode: "+86",
      minLength: 11,
      maxLength: 11,
      placeholder: "138 0013 8000",
      format: /^1[3-9]\d{9}$/,
      label: "+86",
    },
    {
      _id: "jp",
      country: "Japan",
      dialCode: "+81",
      minLength: 10,
      maxLength: 11,
      placeholder: "90 1234 5678",
      format: /^[7-9]\d{9,10}$/,
      label: "+81",
    },
    {
      _id: "kr",
      country: "South Korea",
      dialCode: "+82",
      minLength: 10,
      maxLength: 11,
      placeholder: "10 1234 5678",
      format: /^1[0-9]\d{8,9}$/,
      label: "+82",
    },
    {
      _id: "hk",
      country: "Hong Kong",
      dialCode: "+852",
      minLength: 8,
      maxLength: 8,
      placeholder: "5123 4567",
      format: /^[5-9]\d{7}$/,
      label: "+852",
    },
    {
      _id: "tw",
      country: "Taiwan",
      dialCode: "+886",
      minLength: 9,
      maxLength: 9,
      placeholder: "912 345 678",
      format: /^9\d{8}$/,
      label: "+886",
    },
    {
      _id: "mo",
      country: "Macau",
      dialCode: "+853",
      minLength: 8,
      maxLength: 8,
      placeholder: "6123 4567",
      format: /^6\d{7}$/,
      label: "+853",
    },

    // South Asia
    {
      _id: "in",
      country: "India",
      dialCode: "+91",
      minLength: 10,
      maxLength: 10,
      placeholder: "98765 43210",
      format: /^[6-9]\d{9}$/,
      label: "+91",
    },
    {
      _id: "pk",
      country: "Pakistan",
      dialCode: "+92",
      minLength: 10,
      maxLength: 10,
      placeholder: "301 2345678",
      format: /^3\d{9}$/,
      label: "+92",
    },
    {
      _id: "bd",
      country: "Bangladesh",
      dialCode: "+880",
      minLength: 10,
      maxLength: 10,
      placeholder: "1712 345678",
      format: /^1[3-9]\d{8}$/,
      label: "+880",
    },
    {
      _id: "lk",
      country: "Sri Lanka",
      dialCode: "+94",
      minLength: 9,
      maxLength: 9,
      placeholder: "71 234 5678",
      format: /^7[0-8]\d{7}$/,
      label: "+94",
    },
    {
      _id: "np",
      country: "Nepal",
      dialCode: "+977",
      minLength: 10,
      maxLength: 10,
      placeholder: "98 1234 5678",
      format: /^9[78]\d{8}$/,
      label: "+977",
    },
    {
      _id: "mv",
      country: "Maldives",
      dialCode: "+960",
      minLength: 7,
      maxLength: 7,
      placeholder: "791 2345",
      format: /^[79]\d{6}$/,
      label: "+960",
    },
  ];

  const getCountryByDialCode = (
    dialCode: string
  ): TPhoneCountry | undefined => {
    return countries.find((country) => country.dialCode === dialCode);
  };

  const getDialCodes = (): string[] => {
    return countries.map((country) => country.dialCode);
  };

  const formatPhoneNumber = (phoneNumber: string, dialCode: string): string => {
    const country = getCountryByDialCode(dialCode);
    if (!country) return phoneNumber;

    let value = phoneNumber.replace(/\D/g, ""); // Remove non-digits

    switch (dialCode) {
      case "+65": // Singapore
        if (value.length > 4) {
          value = value.substring(0, 4) + " " + value.substring(4, 8);
        }
        break;
      case "+60": // Malaysia
        if (value.length > 2) {
          value =
            value.substring(0, 2) +
            " " +
            value.substring(2, 5) +
            " " +
            value.substring(5);
        }
        break;
      case "+1": // US/Canada
        if (value.length > 6) {
          value =
            "(" +
            value.substring(0, 3) +
            ") " +
            value.substring(3, 6) +
            "-" +
            value.substring(6, 10);
        } else if (value.length > 3) {
          value = "(" + value.substring(0, 3) + ") " + value.substring(3);
        }
        break;
      case "+44": // UK
        if (value.length > 4) {
          value = value.substring(0, 4) + " " + value.substring(4);
        }
        break;
      default:
        // Generic formatting for other countries
        if (value.length > 3 && value.length <= 6) {
          value = value.substring(0, 3) + " " + value.substring(3);
        } else if (value.length > 6) {
          value =
            value.substring(0, 3) +
            " " +
            value.substring(3, 6) +
            " " +
            value.substring(6);
        }
    }

    return value;
  };

  return {
    countries,
    getCountryByDialCode,
    getDialCodes,
    formatPhoneNumber,
  };
}
