import { computed, Ref } from "vue";

const formatDate = (dateString: string): string => {
    if (!dateString) return "";

    // Parse the date string
    const date = new Date(dateString);

    // Check if the date is valid
    if (isNaN(date.getTime())) return "";

    // Extract day, month, and year with leading zeros
    const day = String(date.getDate()).padStart(2, "0");
    const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are 0-based
    const year = date.getFullYear();

    // Return formatted date as DD/MM/YYYY
    return `${day}/${month}/${year}`;
};

export const useDateFormatter = (dateString: Ref<string | undefined>) => {
    const formattedDate = computed(() => {
        return formatDate(dateString.value || "");
    });

    return {
        formattedDate,
    };
};
