import type { FilesCardProps } from 'vue-element-plus-x/types/FilesCard';
// 对话聊天的文件上传列表
import { defineStore } from 'pinia';

export interface FileItem extends FilesCardProps {
  file: File;
  fileId?: string; // 上传后返回的文件ID
  isUploaded?: boolean; // 是否已上传
  uploadProgress?: number; // 上传进度
  base64?: string; // 图片的base64编码
  fileContent?: string; // 文本文件的内容
  fileType?: 'image' | 'text'; // 文件类型
}

export const useFilesStore = defineStore('files', () => {
  const filesList = ref<FileItem[]>([]);

  // 设置文件列表
  const setFilesList = (list: FileItem[]) => {
    filesList.value = list;
  };

  // 根据索引删除 文件
  const deleteFileByIndex = (index: number) => {
    filesList.value.splice(index, 1);
  };

  // 更新文件上传状态
  const updateFileUploadStatus = (index: number, fileId: string) => {
    if (filesList.value[index]) {
      filesList.value[index].fileId = fileId;
      filesList.value[index].isUploaded = true;
    }
  };

  // 清空文件列表
  const clearFilesList = () => {
    filesList.value = [];
  };

  return {
    filesList,
    setFilesList,
    deleteFileByIndex,
    updateFileUploadStatus,
    clearFilesList,
  };
});
