import { defineStore } from 'pinia'
import ls from '@/utils/store2'
import { ref } from 'vue'
import crypt from '../composables/crypt'

const TRANSFER_INFO_KEY = 'transferInfo'
const useTransferStore = defineStore('transferStore', () => {
    const transferInfo = ref({})
    const initTransferInfo = () => {
        const encryptedTransferInfo = ls.get(TRANSFER_INFO_KEY)
        if (encryptedTransferInfo) {
            const decryptedTransferInfo = crypt.decrypt(encryptedTransferInfo)
            if (decryptedTransferInfo) {
                transferInfo.value = JSON.parse(decryptedTransferInfo)
            }
        }
    }
    const saveTransferInfo = (info: any) => {
        transferInfo.value = info
        const decryptedCard = crypt.encrypt(JSON.stringify(info))
        ls.set(TRANSFER_INFO_KEY, decryptedCard)
    }
    const clearTransferInfo = () => {
        transferInfo.value = {}
        ls.remove(TRANSFER_INFO_KEY)
    }
    initTransferInfo()
    return {
        transferInfo,
        saveTransferInfo,
        clearTransferInfo,
    }
})

export default useTransferStore
