export const extractProvinceFromAddress = (address, provinceData) => {
  if (
    !address ||
    typeof address !== "string" ||
    !provinceData ||
    provinceData.length === 0
  ) {
    return "";
  }

  const normalized = address
    .replace(/\u200c/g, " ")
    .replace(/\s+/g, " ")
    .trim();

  const words = normalized.split(/\s+/).slice(0, 2);
  const firstTwoWords = words.join(" ");

  const matchedProvince = provinceData.find((province) => {
    const provinceName = province.name || "";
    return (
      provinceName.includes(firstTwoWords) ||
      firstTwoWords.includes(provinceName) ||
      normalized.includes(provinceName)
    );
  });

  return matchedProvince ? matchedProvince.name : "";
};
