/**
 * Helper function to format file size
 * @param bytes
 * @param decimals
 */
export const formatBytes = (bytes: number, decimals = 2): string => {
    if (bytes === 0) return "0 Bytes";
    if (bytes < 0 || !Number.isFinite(bytes)) return "Invalid size";
    const k = 1024;
    const dm = decimals < 0 ? 0 : decimals;
    const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
};

/**
 * Helper function to format date (relative time)
 * @param dateString
 */
export const formatRelativeTime = (dateString: string): string => {
    if (!dateString) return "N/A";
    try {
        const date = new Date(dateString);
        if (isNaN(date.getTime())) return "N/A";

        const now = new Date();
        const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
        const diffInMinutes = Math.floor(diffInSeconds / 60);
        const diffInHours = Math.floor(diffInMinutes / 60);
        const diffInDays = Math.floor(diffInHours / 24);

        if (diffInSeconds < 60) return `${diffInSeconds}s ago`;
        if (diffInMinutes < 60) return `${diffInMinutes}m ago`;
        if (diffInHours < 24) return `${diffInHours}h ago`;
        if (diffInDays === 1) return `Yesterday`;
        if (diffInDays < 7) return `${diffInDays}d ago`;
        return date.toLocaleDateString();
    } catch (e) {
        console.error("Error formatting date:", e);
        return "Invalid date";
    }
};

/**
 * Helper function to format ISO string
 * @param isoString - The ISO date string to format
 * @param format - The format type: "datetime" (default), "date", or "time"
 */
export const formatTimestamp = (isoString?: string | null, format: "datetime" | "date" | "time" = "datetime"): string => {
    if (!isoString) return "N/A";
    try {
        const date = new Date(isoString);
        if (isNaN(date.getTime())) return "N/A";
        switch (format) {
            case "date":
                return date.toLocaleDateString();
            case "time":
                return date.toLocaleTimeString();
            default:
                return date.toLocaleString();
        }
    } catch {
        return "N/A";
    }
};
