import { useApi } from '@backstage/core-plugin-api';
import { compact, isEqual } from 'lodash';
import { useRef, useMemo } from 'react';
import useAsync from 'react-use/esm/useAsync';
import { catalogApiRef } from '../../api.esm.js';
import '../../hooks/useEntity.esm.js';
import { useEntityList } from '../../hooks/useEntityListProvider.esm.js';
import 'lodash/isEqual';
import 'lodash/sortBy';
import '@backstage/catalog-model';
import 'react-use/esm/useObservable';
import '../../apis/EntityPresentationApi/EntityPresentationApi.esm.js';
import 'lodash/get';
import '../../apis/StarredEntitiesApi/StarredEntitiesApi.esm.js';
import 'zen-observable';
import { reduceCatalogFilters } from '../../utils/filters.esm.js';

function useAllEntitiesCount() {
  const catalogApi = useApi(catalogApiRef);
  const { filters } = useEntityList();
  const prevRequest = useRef();
  const request = useMemo(() => {
    const { user, ...allFilters } = filters;
    const compacted = compact(Object.values(allFilters));
    const { orderFields, ...catalogFilters } = reduceCatalogFilters(compacted);
    const newRequest = {
      ...catalogFilters,
      limit: 0
    };
    if (Object.keys(catalogFilters.filter).length === 0) {
      prevRequest.current = void 0;
      return prevRequest.current;
    }
    if (isEqual(newRequest, prevRequest.current)) {
      return prevRequest.current;
    }
    prevRequest.current = newRequest;
    return newRequest;
  }, [filters]);
  const { value: count, loading } = useAsync(async () => {
    if (request === void 0) {
      return 0;
    }
    const { totalItems } = await catalogApi.queryEntities(request);
    return totalItems;
  }, [request]);
  return { count, loading };
}

export { useAllEntitiesCount };
//# sourceMappingURL=useAllEntitiesCount.esm.js.map
