import api from 'src/config/api';
import {
  LibrariesResponse,
  Article,
  ArticleTree,
  LibrarySearchListItem,
  LibraryData,
} from 'src/types/libraries';
import { PaginatedResponse, PaginationParams } from 'src/utils/pagination';

// GET
export const getLibraries = (pagination, parentId?: number | string) =>
  api.get<LibrariesResponse>('knowledge-libraries', {
    params: { ...pagination, parentId },
  });

export const getArticle = (id: number | string) =>
  api.get<Article>(`knowledge-libraries/${id}`);

export const getArticleTree = (id: number | string) =>
  api.get<ArticleTree>(`knowledge-libraries/${id}/tree`);

export const searchLibraries = (params: PaginationParams) =>
  api.get<PaginatedResponse<LibrarySearchListItem>>(
    'knowledge-libraries/search',
    {
      params,
    },
  );

export const getEditors = (id: number | string) =>
  api.get(`knowledge-libraries/${id}/users/editors`);

// DELETE
export const deleteLibrary = (id: number) =>
  api.delete(`knowledge-libraries/${id}`);

// PATCH
export const updateLibrary = (id: number, data: LibraryData) =>
  api.patch(`knowledge-libraries/${id}`, data);
