import { ref } from 'vue';
import { defineStore } from 'pinia';

export const useUserInfoStore = defineStore('userInfo', () => {
  const token = ref<string>();
  const permission = ref<string>();

  const saveToken = (val: string) => {
    token.value = val;
  };

  const resetToken = () => {
    token.value = undefined;
  };

  const savePermission = (val: number[]) => {
    permission.value = val.join(',');
  };

  const resetPermission = () => {
    permission.value = undefined;
  };

  const booleanPermission = (val: number) => {
    if (permission.value) {
      const data = permission.value.split(',').map(Number);
      return data.includes(val);
    }
  };

  return {
    token,
    permission,
    saveToken,
    resetToken,
    savePermission,
    resetPermission,
    booleanPermission,
  };
});
