import { createStore } from '../base'
import { sessionData } from '../storage'

/**
 * 登录存储对象
 */
export const loginStore = createStore({
    state() {
        return {
            logining: false,
            loginInfo: sessionData.getRef('loginInfo'),
        }
    },
    getters: {
        // 登录令牌
        token() {
            return this.state.loginInfo.Token
        },
        // 用户ID
        userId() {
            return this.state.loginInfo.UserID
        },
        // 登录ID
        loginId() {
            return this.state.loginInfo.LoginID
        },
        // 首个资金账户ID
        firstAccountId() {
            const accounts = this.state.loginInfo.AccountIDs
            return accounts[0] ?? 0
        },
    },
    actions: {
        // 获取用户登录信息
        getLoginInfo<K extends keyof Proto.LoginRsp>(key: K) {
            return this.state.loginInfo[key]
        },
    }
})