import { reactive, ref } from 'vue';

const state = reactive({
	token: uni.getStorageSync('token') || '',
	assetToken: '',
	identifier: uni.getStorageSync('identifier') || '',
	isDevice: false,
	realNameStatus: 0,
	userInfo: uni.getStorageSync('userInfo') || {
		avatar: '',
		nickname: ''
	}
});

export const useUserStore = () => {
	const setToken = (token) => {
		state.token = token;
		uni.setStorageSync('token', token);
	};

	const setAssetToken = (assetToken) => {
		state.assetToken = assetToken;
	};

	const setIdentifier = (identifier) => {
		state.identifier = identifier;
		uni.setStorageSync('identifier', identifier);
	};

	const setIsDevice = (isDevice) => {
		state.isDevice = isDevice;
	};

	const setRealNameStatus = (status) => {
		state.realNameStatus = status;
	};

	const setUserInfo = (userInfo) => {
		Object.assign(state.userInfo, userInfo);
		uni.setStorageSync('userInfo', userInfo);
	};

	const clearUser = () => {
		state.token = '';
		state.assetToken = '';
		state.userInfo = { avatar: '', nickname: '' };
		uni.removeStorageSync('token');
		uni.removeStorageSync('identifier');
		uni.removeStorageSync('userInfo');
	};

	return {
		state,
		setToken,
		setAssetToken,
		setIdentifier,
		setIsDevice,
		setRealNameStatus,
		setUserInfo,
		clearUser
	};
};