<script setup lang="ts">
import type { SupportedLanguagesType } from '@velofex/locales';

import { SUPPORT_LANGUAGES } from '@velofex/constants';
import { Languages } from '@velofex/icons';
import { loadLocaleMessages } from '@velofex/locales';
import { preferences, updatePreferences } from '@velofex/preferences';
import { VbenDropdownRadioMenu, VbenIconButton } from '@velofex-core/shadcn-ui';

defineOptions({
  name: 'LanguageToggle',
});

async function handleUpdate(value: string) {
  const locale = value as SupportedLanguagesType;
  updatePreferences({
    app: {
      locale,
    },
  });
  await loadLocaleMessages(locale);
}
</script>

<template>
  <div>
    <VbenDropdownRadioMenu
      :menus="SUPPORT_LANGUAGES"
      :model-value="preferences.app.locale"
      @update:model-value="handleUpdate"
    >
      <VbenIconButton>
        <Languages class="text-foreground size-4" />
      </VbenIconButton>
    </VbenDropdownRadioMenu>
  </div>
</template>
