﻿import { Difficulty, IOption } from "@/types";
import type { AxiosError } from "axios";

export const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
export const MAX_FILES = 5;

export function getAxiosErrorMessage(error: unknown): string {
    const err = error as AxiosError<{ message?: string }>;
    return err.response?.data?.message || "Something went wrong!";
}

export function getOptionLabel(options: IOption[], id: string) {
    const label = options?.find(option => option.value === id)?.label || "";
    return label
}

export const difficultyColor = (diff: Difficulty) => {
    switch (diff) {
        case Difficulty.Easy:
            return "bg-green-100 text-green-700 border-green-200";
        case Difficulty.Medium:
            return "bg-yellow-100 text-yellow-700 border-yellow-200";
        case Difficulty.Hard:
            return "bg-red-100 text-red-700 border-red-200";
    }
};

export const buildQuery = (filters: any, page: number, limit: number) => {
    const params = new URLSearchParams();

    params.append("page", page.toString());
    params.append("limit", limit.toString());

    Object.entries(filters).forEach(([key, val]) => {
        if (val !== "" && val !== undefined && val !== null) {
            params.append(key, val.toString());
        }
    });

    return params.toString();
};



