import React, { useState, useEffect } from 'react' import { MapPin, CalendarOff, AlertCircle, Building2, Unlink, ArrowRightLeft, Globe } from 'lucide-react' import { useVacayStore } from '../../store/vacayStore' import { useTranslation } from '../../i18n' import { useToast } from '../shared/Toast' import CustomSelect from '../shared/CustomSelect' import apiClient from '../../api/client' export default function VacaySettings({ onClose }) { const { t } = useTranslation() const toast = useToast() const { plan, updatePlan, isFused, dissolve, users } = useVacayStore() const [countries, setCountries] = useState([]) const [regions, setRegions] = useState([]) const [loadingRegions, setLoadingRegions] = useState(false) const { language } = useTranslation() // Load available countries with localized names useEffect(() => { apiClient.get('/addons/vacay/holidays/countries').then(r => { let displayNames try { displayNames = new Intl.DisplayNames([language === 'de' ? 'de' : 'en'], { type: 'region' }) } catch { /* */ } const list = r.data.map(c => ({ value: c.countryCode, label: displayNames ? (displayNames.of(c.countryCode) || c.name) : c.name, })) list.sort((a, b) => a.label.localeCompare(b.label)) setCountries(list) }).catch(() => {}) }, [language]) // When country changes, check if it has regions const selectedCountry = plan?.holidays_region?.split('-')[0] || '' const selectedRegion = plan?.holidays_region?.includes('-') ? plan.holidays_region : '' useEffect(() => { if (!selectedCountry || !plan?.holidays_enabled) { setRegions([]); return } setLoadingRegions(true) const year = new Date().getFullYear() apiClient.get(`/addons/vacay/holidays/${year}/${selectedCountry}`).then(r => { const allCounties = new Set() r.data.forEach(h => { if (h.counties) h.counties.forEach(c => allCounties.add(c)) }) if (allCounties.size > 0) { let subdivisionNames try { subdivisionNames = new Intl.DisplayNames([language === 'de' ? 'de' : 'en'], { type: 'region' }) } catch { /* */ } const regionList = [...allCounties].sort().map(c => { let label = c.split('-')[1] || c // Try Intl for full subdivision name (not all browsers support subdivision codes) // Fallback: use known mappings for DE if (c.startsWith('DE-')) { const deRegions = { BW:'Baden-Württemberg',BY:'Bayern',BE:'Berlin',BB:'Brandenburg',HB:'Bremen',HH:'Hamburg',HE:'Hessen',MV:'Mecklenburg-Vorpommern',NI:'Niedersachsen',NW:'Nordrhein-Westfalen',RP:'Rheinland-Pfalz',SL:'Saarland',SN:'Sachsen',ST:'Sachsen-Anhalt',SH:'Schleswig-Holstein',TH:'Thüringen' } label = deRegions[c.split('-')[1]] || label } else if (c.startsWith('CH-')) { const chRegions = { AG:'Aargau',AI:'Appenzell Innerrhoden',AR:'Appenzell Ausserrhoden',BE:'Bern',BL:'Basel-Landschaft',BS:'Basel-Stadt',FR:'Freiburg',GE:'Genf',GL:'Glarus',GR:'Graubünden',JU:'Jura',LU:'Luzern',NE:'Neuenburg',NW:'Nidwalden',OW:'Obwalden',SG:'St. Gallen',SH:'Schaffhausen',SO:'Solothurn',SZ:'Schwyz',TG:'Thurgau',TI:'Tessin',UR:'Uri',VD:'Waadt',VS:'Wallis',ZG:'Zug',ZH:'Zürich' } label = chRegions[c.split('-')[1]] || label } return { value: c, label } }) setRegions(regionList) } else { setRegions([]) // If no regions, just set country code as region if (plan.holidays_region !== selectedCountry) { updatePlan({ holidays_region: selectedCountry }) } } }).catch(() => setRegions([])).finally(() => setLoadingRegions(false)) }, [selectedCountry, plan?.holidays_enabled]) if (!plan) return null const toggle = (key) => updatePlan({ [key]: !plan[key] }) const handleCountryChange = (countryCode) => { updatePlan({ holidays_region: countryCode }) } const handleRegionChange = (regionCode) => { updatePlan({ holidays_region: regionCode }) } return (
{/* Block weekends */} toggle('block_weekends')} /> {/* Carry-over */} toggle('carry_over_enabled')} /> {/* Company holidays */}
toggle('company_holidays_enabled')} /> {plan.company_holidays_enabled && (
{t('vacay.companyHolidaysNoDeduct')}
)}
{/* Public holidays */}
toggle('holidays_enabled')} /> {plan.holidays_enabled && (
{regions.length > 0 && ( )}
)}
{/* Dissolve fusion */} {isFused && (

{t('vacay.dissolve')}

{t('vacay.dissolveHint')}

{users.map(u => (
{u.username}
))}
)}
) } function SettingToggle({ icon: Icon, label, hint, value, onChange }) { return (

{label}

{hint}

) }