import { defineStore } from 'pinia';

export interface ImageState {
  id: string;
  uploadedImage: string;
  imageType: string;
  imageName: string;
}

export interface ImgState {
  imgList: Array<ImageState>;
}

export const useImgStore = defineStore('img', {
  // 状态
  state(): ImgState {
    return {
      imgList: []
    };
  },
  // 动作
  actions: {
    saveOrUpdateImage(image: ImageState): boolean {
      const index = this.imgList.findIndex((item) => item.id === image.id);
      if (index !== -1) {
        // 更新现有图片
        this.imgList[index] = image;
      } else {
        // 添加新图片
        this.imgList.push(image);
      }
      return true;
    },
    getImageData(id: string): string {
      return this.imgList.find((item) => item.id === id)?.uploadedImage || '';
    }
  }
});
