import {computed} from 'vue';
import {useUserStore} from "@/hooks/useUserStore";
import {injectLocal, provideLocal, toValue} from "@vueuse/core";

const key = Symbol('USER_SNAPSHOT')

export const useProvideUserSnapshot = function (userSnapshotRefOrGetter) {
    // 有时志愿表会存储一部分当前用户信息，这部分信息命名为userSnapshot
    // 使用时，用快照信息去覆盖用户信息，即可得到当时保存志愿表时的用户信息。
    const {currentUser} = useUserStore()

    const userSnapshot = computed(() => {
        const snapshot = toValue(userSnapshotRefOrGetter)
        return {
            ...currentUser.value,
            ...snapshot
        }
    })

    provideLocal(key, {userSnapshot})
    return userSnapshot
}

export const useInjectUserSnapshot = function () {
    // 默认值用currentUser填充
    const {currentUser} = useUserStore()
    return injectLocal(key, {userSnapshot: currentUser})
}