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

import { SUPPORT_LANGUAGES } from '@easyflow/constants';
import { Languages } from '@easyflow/icons';
import { loadLocaleMessages } from '@easyflow/locales';
import { preferences, updatePreferences } from '@easyflow/preferences';

import {
  EasyFlowDropdownRadioMenu,
  EasyFlowIconButton,
} from '@easyflow-core/shadcn-ui';

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

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

<template>
  <div>
    <EasyFlowDropdownRadioMenu
      :menus="SUPPORT_LANGUAGES"
      :model-value="preferences.app.locale"
      @update:model-value="handleUpdate"
    >
      <EasyFlowIconButton class="hover:animate-[shrink_0.3s_ease-in-out]">
        <Languages class="text-foreground size-4" />
      </EasyFlowIconButton>
    </EasyFlowDropdownRadioMenu>
  </div>
</template>
