import {useUserStore} from "@/store/userStore";
import {VHS_USER_SNAPSHOT} from "@/types/injectionSymbols";
import {UserInfo} from "@/types/user";

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

    const userSnapshot = computed(() => {
        const snapshot = toValue(userSnapshotRefOrGetter)
        return {
            ...userStore.userInfo,
            ...snapshot
        }
    })

    provide(VHS_USER_SNAPSHOT, userSnapshot)
    return userSnapshot
}

export const useInjectUserSnapshot = function () {
    // 默认值用currentUser填充
    const {userInfo} = storeToRefs(useUserStore())
    return inject(VHS_USER_SNAPSHOT, userInfo)
}