import { useMemo } from 'react';
import { useAuthStore } from '../stores/authStore';
import { CategoryIds, CityIds } from '../types';

const allCategoryIds: number[] = CategoryIds;
const allCityIds: number[] = CityIds;

export function usePermissions() {
  const { user } = useAuthStore();
  const isAdmin = user?.permissions.isAdmin || false;

  const availableCategoryIds = useMemo(() => {
    if (!user) return [];
    if (isAdmin) return allCategoryIds;
    return allCategoryIds.filter(cat => user.permissions.categories[cat]?.create || user.permissions.categories[cat]?.edit);
  }, [user, isAdmin]);

  const availableCityIds = useMemo(() => {
    if (!user) return [];
    if (isAdmin) return allCityIds;
    return user.permissions.cities;
  }, [user, isAdmin]);

  const hasNoPermissions = availableCategoryIds.length === 0 || availableCityIds.length === 0;

  return { availableCategoryIds, availableCityIds, hasNoPermissions, isAdmin };
}