<script setup>
import { provide, reactive } from "vue";
import { AI_PROVIDER_KEY, createInterceptors } from "./ai-provider";
defineOptions({
  name: "YhAiProvider"
});
const props = defineProps({
  baseUrl: { type: String, required: false, default: "" },
  token: { type: String, required: false, default: "" },
  headers: { type: Object, required: false, default: () => ({}) },
  userAvatar: { type: String, required: false, default: "" },
  assistantAvatar: { type: String, required: false, default: "" },
  userName: { type: String, required: false, default: "User" },
  assistantName: { type: String, required: false, default: "Assistant" },
  systemPrompt: { type: String, required: false, default: "" },
  typewriter: { type: Object, required: false, default: () => ({ enabled: true, charsPerFrame: 1 }) },
  timeout: { type: Number, required: false, default: 3e4 },
  withCredentials: { type: Boolean, required: false, default: false },
  mode: { type: String, required: false, default: "cors" },
  cache: { type: String, required: false, default: "default" }
});
const interceptors = createInterceptors();
const config = reactive({
  get baseUrl() {
    return props.baseUrl;
  },
  get token() {
    return props.token;
  },
  get headers() {
    return props.headers;
  },
  get userAvatar() {
    return props.userAvatar;
  },
  get assistantAvatar() {
    return props.assistantAvatar;
  },
  get userName() {
    return props.userName;
  },
  get assistantName() {
    return props.assistantName;
  },
  get systemPrompt() {
    return props.systemPrompt;
  },
  get typewriter() {
    return props.typewriter;
  },
  get timeout() {
    return props.timeout;
  },
  get withCredentials() {
    return props.withCredentials;
  },
  get mode() {
    return props.mode;
  },
  get cache() {
    return props.cache;
  },
  // 暴露拦截器管理器
  requestInterceptors: interceptors.request,
  responseInterceptors: interceptors.response
});
provide(AI_PROVIDER_KEY, config);
defineExpose({
  /**
   * 添加请求拦截器
   */
  addRequestInterceptor: (interceptor) => {
    return interceptors.request.use(interceptor);
  },
  /**
   * 添加响应拦截器
   */
  addResponseInterceptor: (interceptor) => {
    return interceptors.response.use(interceptor);
  },
  /**
   * 移除请求拦截器
   */
  removeRequestInterceptor: (id) => {
    interceptors.request.eject(id);
  },
  /**
   * 移除响应拦截器
   */
  removeResponseInterceptor: (id) => {
    interceptors.response.eject(id);
  },
  /**
   * 清空所有拦截器
   */
  clearInterceptors: () => {
    interceptors.request.clear();
    interceptors.response.clear();
  }
});
</script>

<template>
  <slot></slot>
</template>
