import {defineStore} from "pinia";
import {ref} from "vue";


export const useAccountStore = defineStore("accountStore", () => {
        const accounts = ref([])

        const addAccount = (username, password) => {
            // 如果不存在则新建
            if (!accounts.value.some(account => account.username === username)) {
                accounts.value.push({
                    username: username,
                    password: password,
                })
                // 如果已经存在则更新密码
            } else {
                accounts.value.find(account => account.username === username).password = password
            }
        }

        const deleteAccount = (username) => {
            accounts.value = accounts.value.filter(account => account.username !== username)
        }

        return {
            accounts,
            addAccount, deleteAccount
        }
    },
    {
        persist: true
    }
)
