import { useEffect, useState } from "react";

export const formatFileSize=(bytes)=> {
  if (bytes < 1024) return bytes + " B";
  else if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + " KB";
  else return (bytes / (1024 * 1024)).toFixed(2) + " MB";
}
export const AppColorconfig = {
  colors: {
    primary: '#696cff',
    secondary: '#8592a3',
    success: '#71dd37',
    info: '#03c3ec',
    warning: '#ffab00',
    danger: '#ff3e1d',
    dark: '#233446',
    black: '#000',
    white: '#fff',
    cardColor: '#fff',
    bodyBg: '#f5f5f9',
    bodyColor: '#697a8d',
    headingColor: '#566a7f',
    textMuted: '#a1acb8',
    borderColor: '#eceef1'
  }
};
export const getColorNameFromHex = (hex) => {
  const normalizedHex = hex?.replace(/'/g, '').toLowerCase(); 
  const colors = AppColorconfig.colors;

  for (const [name, value] of Object.entries(colors)) {
    if (value.toLowerCase() === normalizedHex) {
      return name;
    }
  }

  return null; //
};

export const useDebounce = (value, delay = 500) => {
  const [debouncedValue, setDebouncedValue] = useState(value);

  useEffect(() => {
    const timer = setTimeout(() => setDebouncedValue(value), delay);
    return () => clearTimeout(timer);
  }, [value, delay]);

  return debouncedValue;
};

export const getIconByFileType = (type = "") => {
  const lower = type.toLowerCase();

  if (lower === "application/pdf") return "bxs-file-pdf";
  if (lower.includes("word")) return "bxs-file-doc";
  if (lower.includes("excel") || lower.includes("spreadsheet"))
    return "bxs-file-xls";
  if (lower === "image/png") return "bxs-file-png";
  if (lower === "image/jpeg" || lower === "image/jpg") return "bxs-file-jpg";
  if (lower.includes("zip") || lower.includes("rar")) return "bxs-file-archive";

  return "bx bx-file";
};
