<script lang="ts" setup>
import { computed } from 'vue';

import { useAntdDesignTokens } from '@velofex/hooks';
import { preferences, usePreferences } from '@velofex/preferences';

import { App, ConfigProvider, theme } from 'antdv-next';

import { Login } from '#/components/login';
import { antdLocale } from '#/locales';
import { useLoginModalStore } from '#/store';

defineOptions({ name: 'App' });

const { isDark } = usePreferences();
const { tokens } = useAntdDesignTokens();
const loginModalStore = useLoginModalStore();

const tokenTheme = computed(() => {
  const algorithm = isDark.value
    ? [theme.darkAlgorithm]
    : [theme.defaultAlgorithm];

  // antd 紧凑模式算法
  if (preferences.app.compact) {
    algorithm.push(theme.compactAlgorithm);
  }

  return {
    algorithm,
    token: tokens,
  };
});

function handleSubmit(_values: any) {
  loginModalStore.close();
}
</script>

<template>
  <ConfigProvider :locale="antdLocale" :theme="tokenTheme">
    <App>
      <RouterView />
    </App>
    <Login v-model:open="loginModalStore.isOpen" @submit="handleSubmit" />
  </ConfigProvider>
</template>
