'use client';

/**
 * I18n - section 7 of /design-system.
 *
 * Live LanguageToggle demo + showing the current <html lang> + dir attributes.
 */

import { LanguageToggle } from '@/components/ui/i18n/LanguageToggle';
import { Pill } from '@/components/ui/primitives/Pill';
import { Stack } from '@/components/ui/layout/Stack';
import { Row } from '@/components/ui/layout/Row';
import { useT, useLocale } from '@/lib/i18n/react';
import { dirForLocale } from '@/lib/i18n';
import { ComponentEntry } from '../../components/ComponentEntry';
import { SectionShell } from '../../components/SectionShell';

/**
 * Display the current <html lang> and <html dir> values. Derives them from
 * the locale store directly instead of reading `document.documentElement`
 * inside an effect - this satisfies react-hooks/set-state-in-effect AND is
 * always in sync (the LocaleProvider maintains the DOM attributes).
 */
function HtmlAttrs() {
  const { locale } = useLocale();
  const lang = locale;
  const dir = dirForLocale(locale);

  return (
    <Row gap="3" wrap>
      <Pill tone="info" size="md">
        &lt;html lang=&quot;{lang}&quot;&gt;
      </Pill>
      <Pill tone="info" size="md">
        &lt;html dir=&quot;{dir}&quot;&gt;
      </Pill>
    </Row>
  );
}

export function I18n() {
  const t = useT('design_system');

  return (
    <SectionShell id="i18n" title={t('sidebar_i18n')} description={t('section_intro')}>
      <ComponentEntry
        name="LanguageToggle"
        id="languagetoggle-i18n"
        path="src/components/ui/i18n/LanguageToggle"
        description="Segmented control for he ↔ en. Writes to the persisted prefs store; live-updates <html lang dir>."
        tokens={['radius-md', 'color-border-default']}
        a11yNotes={[
          'role=group with aria-label',
          'Each option is a real <button>',
          'aria-pressed reflects active locale',
        ]}
        importSnippet={`import { LanguageToggle } from '@/components/ui/i18n/LanguageToggle';`}
      >
        <Stack gap="4">
          <LanguageToggle />
          <HtmlAttrs />
          <p className="text-text-secondary text-sm">
            Toggling the language updates the entire page direction live and re-runs every{' '}
            <code>useT()</code> hook.
          </p>
        </Stack>
      </ComponentEntry>
    </SectionShell>
  );
}
