import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
    state: () => ({
        user: null,
        token: localStorage.getItem('token') || '',
    }),
    getters: {
        isLogin: (state) => !!state.token,
        getUser: (state) => state.user,
        getToken: (state) => state.token,
    },
    actions: {
        setUser(user) {
            this.user = user;
        },
        setToken(token) {
            this.token = token;
            if (token) {
                localStorage.setItem('token', token);
            } else {
                localStorage.removeItem('token');
            }
        },
        clearUser() {
            this.user = null;
            this.token = '';
            localStorage.removeItem('token');
            localStorage.removeItem('member_username');
        }
    }
});