import axios from "axios";
import { createSignal } from "solid-js";


// YYYY-MM-DD to DD/MM/YYYY
export function formatDate(date: string): string {
    const [year, month, day] = date.split("-");
    return `${day}/${month}/${year}`;
}

export const backend = axios.create({
    baseURL: import.meta.env.VITE_BACKEND_URL,
});

export function wait(ms: number): Promise<void> {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

export enum LoadingStatus {
    Init,
    Loading,
    Ok,
    Error,
}
export function useLoading() {
    const [status, setStatus] = createSignal(LoadingStatus.Init);
    const [error, setError] = createSignal("");

    return {error, setError, status, setStatus};
}

export function numberToMonth(n: number): string | null {
    switch (n) {
        case 1: return "ENERO";
        case 2: return "FEBRERO";
        case 3: return "MARZO";
        case 4: return "ABRIL";
        case 5: return "MAYO";
        case 6: return "JUNIO";
        case 7: return "JULIO";
        case 8: return "AGOSTO";
        case 9: return "SEPTIEMBRE";
        case 10: return "OCTUBRE";
        case 11: return "NOVIEMBRE";
        case 12: return "DICIEMBRE";
        default: return null;
    }
}
