export const DATE_STRING_FORMAT = 'dd/MM/yyyy';

// formatter.ts
export function formatDate(date: Date): string {
    const day = date.getDate();
    const month = date.getMonth() + 1;
    const year = date.getFullYear().toString();
    
    const dayStr = day < 10 ? '0' + day : '' + day;
    const monthStr = month < 10 ? '0' + month : '' + month;
    
    return `${dayStr}/${monthStr}/${year}`;
}

export function formatNumber(number: number): string {
    const numberFormatter = new Intl.NumberFormat('it-IT', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
    });

    return numberFormatter.format(number);
}

export function setSpinnerMessage(message: string, name: string = 'mainSpinner'): void {
    const ispSpinner = _getSpinner(name);
    
    if (ispSpinner) {
        ispSpinner.setAttribute('data-message', message);
    }
}

export function clearSpinnerMessage(name: string = 'mainSpinner'): void {
    const ispSpinner = _getSpinner(name);
    
    if (ispSpinner) {
        ispSpinner.setAttribute('data-message', "");
    }
}

function _getSpinner(name: string){
    const spinners = document.getElementsByName(name);
    const spinnerArray = Array.from(spinners);
    
    return spinnerArray.find(spinner => spinner.tagName.toLowerCase() === 'isp-spinner');
}