'use client'

import {useLocale} from 'next-intl'
import {redirect} from 'next/navigation'

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

export default function LocaleSwitcher() {
	const router = useRouter()
	const pathname = usePathname()
	const locale = useLocale()
	const initialState = locale !== 'uk'

	const handler = (state: boolean) => {
		const newPath = `/${locale}${pathname}`
		//window.history.replaceState(null, '', newPath)
		const link = document.getElementById('lang-switch')
		if (link) {
			link.innerText = `${state ? '/ru' : ''}${pathname}`
			link.setAttribute('href', `${state ? '/ru' : ''}${pathname}`)
			link.click()
		}
	}
	// router.replace('/cabinet', {locale: checked ? 'ru' : 'uk'}
	return (
		<div className='flex items-center space-x-2'>
			<Link id='lang-switch' href='/' locale='uk'>
				LA
			</Link>
			<Label htmlFor='locale-switcher'>Укр</Label>
			<Switch
				id='locale-switcher'
				defaultChecked={initialState}
				onCheckedChange={handler}
			/>
			<Label htmlFor='locale-switcher'>Рус</Label>
		</div>
	)
}
