import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { CountryPhoneCodes } from "../public/countries/country-phone-code";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

// phonecode => 'EG'   phone=> 1032323232
export const formatPhoneNumber = (phonecode: string, phone: string): string => {
  // const [code, number] = phone.split('+')
  // const dialCode = (CountryPhoneCodes.find((country: { [key: string]: string }) => code.toLowerCase() == country.code.toLowerCase()).dial_code || '')

  const dial_code =
    CountryPhoneCodes.find(
      (country) => `+${phonecode}` == country.dial_code.toLowerCase()
    )?.dial_code || "";
  return `${dial_code}${phone}`;
};
export const updateURLParams = (
  params: { [key: string]: string | null },
  router: any,
  pathname: any,
  page?: boolean,
  refetch?: any
) => {
  const newSearchParams = new URLSearchParams(window.location.search);
  Object.entries(params).forEach(([key, value]) => {
    if (value === null) {
      newSearchParams.delete(key);
    } else {
      newSearchParams.set(key, value);
    }
    if (page) {
      newSearchParams.delete("page");
    }
    if (refetch) {
      refetch();
    }
  });
  router.replace(`${pathname}?${newSearchParams.toString()}`, {
    scroll: false,
  });
};
