import React, { useState, useRef, useEffect } from 'react' import ReactDOM from 'react-dom' import { Clock, ChevronUp, ChevronDown } from 'lucide-react' import { useSettingsStore } from '../../store/settingsStore' function formatDisplay(val, is12h) { if (!val) return '' const [h, m] = val.split(':').map(Number) if (isNaN(h) || isNaN(m)) return val if (!is12h) return val const period = h >= 12 ? 'PM' : 'AM' const h12 = h === 0 ? 12 : h > 12 ? h - 12 : h return `${h12}:${String(m).padStart(2, '0')} ${period}` } export default function CustomTimePicker({ value, onChange, placeholder = '00:00', style = {} }) { const is12h = useSettingsStore(s => s.settings.time_format) === '12h' const [open, setOpen] = useState(false) const [inputFocused, setInputFocused] = useState(false) const ref = useRef(null) const dropRef = useRef(null) const [h, m] = (value || '').split(':').map(Number) const hour = isNaN(h) ? null : h const minute = isNaN(m) ? null : m useEffect(() => { const handler = (e) => { if (ref.current?.contains(e.target)) return if (dropRef.current?.contains(e.target)) return setOpen(false) } if (open) document.addEventListener('mousedown', handler) return () => document.removeEventListener('mousedown', handler) }, [open]) const update = (newH, newM) => { const hh = String(Math.max(0, Math.min(23, newH))).padStart(2, '0') const mm = String(Math.max(0, Math.min(59, newM))).padStart(2, '0') onChange(`${hh}:${mm}`) } const incHour = () => update(((hour ?? -1) + 1) % 24, minute ?? 0) const decHour = () => update(((hour ?? 1) - 1 + 24) % 24, minute ?? 0) const incMin = () => { const newM = ((minute ?? -5) + 5) % 60 const newH = newM < (minute ?? 0) ? ((hour ?? 0) + 1) % 24 : (hour ?? 0) update(newH, newM) } const decMin = () => { const newM = ((minute ?? 5) - 5 + 60) % 60 const newH = newM > (minute ?? 0) ? ((hour ?? 0) - 1 + 24) % 24 : (hour ?? 0) update(newH, newM) } const btnStyle = { background: 'none', border: 'none', cursor: 'pointer', padding: 2, color: 'var(--text-faint)', display: 'flex', borderRadius: 4, transition: 'color 0.15s', } const handleInput = (e) => { const raw = e.target.value onChange(raw) // Auto-format: wenn "1430" → "14:30" const clean = raw.replace(/[^0-9:]/g, '') if (/^\d{2}:\d{2}$/.test(clean)) onChange(clean) else if (/^\d{4}$/.test(clean)) onChange(clean.slice(0, 2) + ':' + clean.slice(2)) else if (/^\d{1,2}:\d{2}$/.test(clean)) { const [hh, mm] = clean.split(':') onChange(hh.padStart(2, '0') + ':' + mm) } } const handleBlur = () => { if (!value) return const clean = value.replace(/[^0-9:]/g, '') if (/^\d{1,2}:\d{2}$/.test(clean)) { const [hh, mm] = clean.split(':') const h = Math.min(23, Math.max(0, parseInt(hh))) const m = Math.min(59, Math.max(0, parseInt(mm))) onChange(String(h).padStart(2, '0') + ':' + String(m).padStart(2, '0')) } else if (/^\d{3,4}$/.test(clean)) { const s = clean.padStart(4, '0') const h = Math.min(23, Math.max(0, parseInt(s.slice(0, 2)))) const m = Math.min(59, Math.max(0, parseInt(s.slice(2)))) onChange(String(h).padStart(2, '0') + ':' + String(m).padStart(2, '0')) } } return (