import type { UniPopupOnChangeEvent } from '@uni-helper/uni-ui-types'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const usePopupStore = defineStore(
  'popup',
  () => {
    const show = ref(false)
    const type = ref<string | null>(null)

    const onChangePopupProps = (popupProps: UniPopupOnChangeEvent) => {
      show.value = popupProps.show
      type.value = popupProps.type
    }

    const resetPopupProps = () => {
      show.value = false
      type.value = ''
    }

    return {
      show,
      type,
      onChangePopupProps,
      resetPopupProps,
    }
  },
  {
    persist: {
      storage: {
        setItem(key: string, value: any) {
          return uni.setStorageSync(key, value)
        },
        getItem(key: string) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)
