<script setup lang="ts">
import { createTextVNode, defineComponent, ref } from 'vue';
import { useDialog, useLoadingBar, useMessage, useNotification } from 'naive-ui';
import ReauthModal from './reauth-modal.vue';

defineOptions({
  name: 'AppProvider'
});

const showReauthModal = ref(false);

const ContextHolder = defineComponent({
  name: 'ContextHolder',
  setup() {
    function register() {
      window.$loadingBar = useLoadingBar();
      window.$dialog = useDialog();
      window.$message = useMessage();
      window.$notification = useNotification();

      // 全局重新认证函数
      window.$reauth = {
        show: () => {
          showReauthModal.value = true;
        },
        hide: () => {
          showReauthModal.value = false;
        }
      };
    }

    register();

    return () => createTextVNode();
  }
});

function handleReauthSuccess() {
  showReauthModal.value = false;
}

function handleReauthCancel() {
  showReauthModal.value = false;
}
</script>

<template>
  <NLoadingBarProvider>
    <NDialogProvider>
      <NNotificationProvider>
        <NMessageProvider>
          <ContextHolder />
          <slot></slot>
          <ReauthModal v-model:show="showReauthModal" @success="handleReauthSuccess" @cancel="handleReauthCancel" />
        </NMessageProvider>
      </NNotificationProvider>
    </NDialogProvider>
  </NLoadingBarProvider>
</template>

<style scoped></style>
