export function getConvertedDataTime(dateObject: string): string {
  const date = new Date(dateObject);
  /**
   * its return type date
   * And its object type
   */
  return date.toLocaleDateString("en-US", {
    year: "numeric",
    month: "long",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
  });
}

export function checkNumeric(value: any) {
  return /^-?\d+$/.test(value);
}

export function kgToLbs(kg: number) {
  const lbs = kg * 2.20462;
  return lbs.toFixed(2);
}

export function googleAddressActionUrl(
  searchedString: string | number,
  type: string = "address",
) {

  const isNumeric = /^[0-9]+$/.test(searchedString.toString());
  const isAlphaNumeric = /^[A-Za-z0-9\s]+$/.test(searchedString.toString());

  if (isNumeric || isAlphaNumeric) {
    type = "postal_code";
  } else {
    type = "address";
  }
  const googleApiKey = process.env.NEXT_PUBLIC_GOOGLE_GEOCODE_CLIENT_KEY;
  const regionFilter = "country:US|country:CA|country:MX"; // Region filter for US, Canada, and Mexico
  const queryString = `address=${encodeURIComponent(searchedString.toString())}&types=${type}&components=${regionFilter}&key=${googleApiKey}`;
  return `${process.env.NEXT_PUBLIC_GOOGLE_ADDRESS}/maps/api/geocode/json?${queryString}`;
}

export function googleAddressActionUrl2(
  searchedString: string | number,
  type: string = "address",
) {
  if (checkNumeric(searchedString)) {
    type = "postal_code";
  }
  const googleApiKey = process.env.NEXT_PUBLIC_GOOGLE_GEOCODE_CLIENT_KEY;
  const queryString = `address=${encodeURIComponent(searchedString.toString())}&components=country:CA&types=${type}&key=${googleApiKey}`;
  return `${process.env.NEXT_PUBLIC_GOOGLE_ADDRESS}/maps/api/geocode/json?${queryString}`;
}

export function mapboxAddressActionUrl(
  searchedString: string | number,
  type: string = "address",
) {
  // Adjust the type if a numeric value or format resembles a postal code
  if (checkNumeric(searchedString) || /^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/.test(searchedString.toString())) {
    type = "postcode";
  }

  const mapBoxAccessToken = process.env.NEXT_PUBLIC_MAPBOX_GL_TOKEN;
  const queryString = `limit=50&language=en-GB&country=us,ca,mx&types=${type}&fuzzyMatch=true&access_token=${mapBoxAccessToken}`;
  const encodedSearchedString = encodeURIComponent(searchedString.toString());

  return `${process.env.NEXT_PUBLIC_MAP_ADDRESS}/geocoding/v5/mapbox.places/${encodedSearchedString}.json?${queryString}`;
}



export function replaceCapitalLettersAndUnderscores(inputValue:any) {
  // Replace capital letters and underscores with spaces
  let replaced = inputValue.replace(/[A-Z_]/g, function(match:any) {
      return match === '_' ? ' ' : ' ' + match.toLowerCase();
  });

  // Capitalize the first letter
  return replaced.charAt(0).toUpperCase() + replaced.slice(1);
}

/* This function is used for string camel case convert */
export function convertCamelCaseToWords(inputValue:any) {
  return inputValue?.replace(/([a-z])([A-Z])/g, '$1 $2');
}

/* This function is used to null value check */
export function toCheckNullValue(inputData: any){
  return (inputData !== null) ? inputData : '';
}

/* This is used to validate input field digit and accept both value int and float */
export function toValidateCostAndDigit(inputData: any): boolean {
  if (typeof inputData !== 'string') inputData = String(inputData);
  if (!/^\d*\.?\d*$/.test(inputData)) return false;
  if (inputData.endsWith(".")) {
    const [wholePart] = inputData.split(".");
    return wholePart.length <= 6;
  }
  const [integerPart = '', decimalPart = ''] = inputData.split(".");
  const totalLength = integerPart.length + decimalPart.length;
  return totalLength <= 6;
}

/* This is used to only accept 6 digit number  */
export function toValidDigitNumber(inputData: any): boolean{
  return /^(?:\d{1,6}|\d{1,5}\.\d{1}|\d{1,4}\.\d{2}|\d{1,3}\.\d{3}|\d{1,2}\.\d{4}|\d{1}\.\d{5}|0?\.\d{1,6})$/.test(inputData);
}

