import Avatar from './social/Avatar'
import { useSocial } from './social/SocialProvider'
import { t } from '../i18n'
import { AlertTriangle, Check, CloudOff, KeyRound, Loader2, LogOut, RefreshCw, UserRound } from 'lucide-react'
import { useState, type FormEvent } from 'react'
import { PASSWORD_MIN, updatePassword } from '../lib/cloud/auth'
import { timeAgo } from '../lib/timeAgo'
import type { AccountApi } from '../store'
import ConfirmDialog from './ConfirmDialog'
import { SectionTitle } from './ui'
import { cx } from '../lib/utils'
/** Réglages → Mon compte : mot de passe, déconnexion. La synchro tourne en arrière-plan. */
export default function AccountSection({ account, onMessage }: { account: AccountApi; onMessage: (kind: 'ok' | 'error', text: string) => void }) {
const { sync } = account
const social = useSocial()
const [pwOpen, setPwOpen] = useState(false)
const [pw, setPw] = useState('')
const [busy, setBusy] = useState<'pw' | 'logout' | 'import' | null>(null)
const [confirmLogout, setConfirmLogout] = useState(false)
const changePassword = async (e: FormEvent) => {
e.preventDefault()
setBusy('pw')
try {
await updatePassword(pw)
setPw('')
setPwOpen(false)
onMessage('ok', t('account.passwordChanged'))
} catch (err) {
onMessage('error', (err as Error).message)
} finally {
setBusy(null)
}
}
const logout = async (force = false) => {
setBusy('logout')
try {
const r = await account.logout(force)
if (r === 'pending') setConfirmLogout(true)
} catch (err) {
onMessage('error', (err as Error).message)
} finally {
setBusy(null)
}
}
const importDevice = async () => {
setBusy('import')
try {
const n = await account.importDeviceData()
onMessage('ok', t('account.imported', { count: n }))
} catch {
onMessage('error', t('account.importFailed'))
} finally {
setBusy(null)
}
}
return (
<>
{account.deviceData.lists > 0 ? t('account.deviceDataWithLists', { titles: t('account.nTitles', { count: account.deviceData.items }), lists: t('account.nLists', { count: account.deviceData.lists }) }) : t('account.deviceData', { titles: t('account.nTitles', { count: account.deviceData.items }) })}
{t('account.deviceHint')}