const convertToLocalTime = () => {
    document.querySelectorAll('time.local-time').forEach((el) => {
        const isoDate = el.getAttribute('datetime')
        const format = el.getAttribute('data-format') || 'datetime'

        if (!isoDate) return

        try {
            const date = new Date(isoDate)
            if (isNaN(date.getTime())) return

            let options = {}
            if (format === 'date') {
                options = {
                    year: 'numeric',
                    month: 'short',
                    day: 'numeric'
                }
            } else if (format === 'time') {
                options = {
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit'
                }
            } else {
                options = {
                    year: 'numeric',
                    month: 'short',
                    day: 'numeric',
                    hour: '2-digit',
                    minute: '2-digit'
                }
            }

            el.textContent = date.toLocaleString(undefined, options)
            el.title = date.toISOString()
        } catch (e) {
            console.error('Error parsing date:', isoDate, e)
        }
    })
}

document.addEventListener('DOMContentLoaded', convertToLocalTime)
