import { computed, Ref } from "vue";

const truncateText = (text: string, maxLength: number): string => {
    if (!text) return "";
    return text.length > maxLength ? text.slice(0, maxLength) + " ..." : text;
};

export const useTextTruncator = (
    text: Ref<string | undefined | null>,
    maxLength: number
) => {
    const truncatedText = computed<string>(() => {
        return truncateText(text.value || "", maxLength);
    });

    return {
        truncatedText,
    };
};
