import { useAuthStore } from '@/store/modules/auth';
import { useObjectContextStore } from '@/store/modules/object-context';

export function useAuth() {
  const authStore = useAuthStore();
  const objectContextStore = useObjectContextStore();

  function hasAuth(codes: string | string[]) {
    if (!authStore.isLogin) {
      return false;
    }

    if (typeof codes === 'string') {
      return authStore.userInfo.buttons.includes(codes);
    }

    return codes.some(code => authStore.userInfo.buttons.includes(code));
  }

  function hasObjectAuth(codes: string | string[]) {
    const targetCodes = typeof codes === 'string' ? [codes] : codes;

    return targetCodes.some(code => objectContextStore.buttonCodes.includes(code));
  }

  return {
    hasAuth,
    hasObjectAuth
  };
}
