import { queryClient } from 'src/config/react-query';
import { PaginationParams } from 'src/utils/pagination';

export const librariesKeys = {
  all: () => ['libraries'],
  list: () => [...librariesKeys.all(), 'list'],
  allDetail: () => [...librariesKeys.all(), 'detail'],
  detail: (id: number | string) => [...librariesKeys.allDetail(), Number(id)],
  allTree: () => [...librariesKeys.all(), 'tree'],
  tree: (id: number | string) => [...librariesKeys.allTree(), Number(id)],
  search: {
    all: () => [...librariesKeys.all(), 'search'],
    list: (pagination: PaginationParams) => [
      ...librariesKeys.search.all(),
      pagination,
    ],
  },
  editors: {
    all: () => [...librariesKeys.all(), 'editors'],
    byId: (id: number | string) => [...librariesKeys.editors.all(), Number(id)],
  },
};

export const updateLibraryStatus = () =>
  queryClient.invalidateQueries(librariesKeys.list());

export const invalidateDeletedLibrary = (libraryId: number) => {
  queryClient.removeQueries(librariesKeys.detail(libraryId));
  queryClient.removeQueries(librariesKeys.tree(libraryId));
  queryClient.invalidateQueries(librariesKeys.list());
};
