
export function removeEmptyKeys(obj: Record<string, unknown>): object {
  // Filter out key-value pairs where the value is null or undefined
  const filteredObj = Object.fromEntries(
    Object.entries(obj).filter(
      ([_, value]) => value !== null && value !== undefined && value !== ''
    )
  );
  return filteredObj;
}

// Define a reusable function to extract unique keys from an array of objects
export const extractUniqueKeys = (data: string[]): string[] => {
  return data.reduce((keys: string[], obj: string) => {
    Object.keys(obj).forEach(key => {
      if (!keys.includes(key)) {
        keys.push(key);
      }
    });
    return keys;
  }, []);
};

export const getHighestValue = (data: any[], keyToCheck: string) => {
  return JSON.parse(JSON.stringify(data)).reduce((highest: { project_name: any, [key: string]: any }, obj: any) => {
    Object.keys(obj).forEach(key => {
      if (key !== keyToCheck) {
        const value = obj[key];
        if (value && (highest[key] === undefined || value > parseFloat(highest[key]))) {
          highest[key] = parseFloat(value);
        }
      }
    });
    return highest;
  }, {});
};

export const formatBarGraphDate = (dateStr : string) => {
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    const dateObj = new Date(dateStr);
    const day = dateObj.getDate();
    const month = months[dateObj.getMonth()];
    return `${month}-${day}`;
  };