import { defineStore } from 'pinia'

export const useLastImgStore = defineStore('lastImg', {
  state: () => ({
    imgUrl: null,
  }),
  actions: {
    setLastImgUrl(lastImgUrl) {
      this.imgUrl = lastImgUrl

      // 添加持久化存储
      if (lastImgUrl) {
        localStorage.setItem('lastImgUrl', lastImgUrl)
      } else {
        localStorage.removeItem('lastImgUrl')
      }
    },

    // 添加初始化方法
    initFromStorage() {
      const stored = localStorage.getItem('lastImgUrl')
      if (stored) {
        this.imgUrl = stored
      }
    },

    clearCache() {
      this.imgUrl = null
      localStorage.removeItem('lastImgUrl')
    },
  },

  // 可选：添加持久化插件配置
  persist: {
    key: 'lastImgUrl',
    storage: localStorage,
  },
})
