'use client'

import cookies from 'js-cookie'
import {useLocale} from 'next-intl'

import {useRouter} from '@/i18n/routing'
import {Link} from '@/i18n/routing'
import {Label} from '@/ui/label'
import {Switch} from '@/ui/switch'

export default function LocaleSwitcher() {
	const router = useRouter()
	const locale = useLocale()
	const initialState = locale !== 'uk'
	//const [localeState, setLocaleState] = useState(initialState)

	const handleLocaleChange = (state: boolean) => {
		const locale = cookies.get('NEXT_LOCALE') === 'ru' ? 'uk' : 'ru'
		cookies.set('NEXT_LOCALE', locale, {
			expires: 7,
			path: '/',
			sameSite: 'Lax'
		})
		let path = window.location.pathname.replace(/^\/ru/, '')
		if (path === '') path = '/'
		router.replace(path, {locale})
	}

	return (
		<div className='flex items-center space-x-2'>
			<Label htmlFor='locale-switcher'>Укр</Label>
			<Switch
				className='bg-brand-violet-900'
				id='locale-switcher'
				defaultChecked={initialState}
				onCheckedChange={handleLocaleChange}
			/>
			<Label htmlFor='locale-switcher'>Рус</Label>
		</div>
	)
}
