export default function useDocument() {
  function getAll({
    page = 1,
    search = "",
    limit = 10,
    type = "all",
    site = "",
    parentId = "",
  }: {
    page?: number;
    search?: string;
    limit?: number;
    type?: string;
    site?: string;
    parentId?: string;
  } = {}) {
    const query: Record<string, any> = {
      page,
      search,
      limit,
      site,
      parentId,
    };

    return useNuxtApp().$api<Record<string, any>>(`/api/documents`, {
      method: "GET",
      query,
    });
  }

  function add(value: any) {
    return useNuxtApp().$api<Record<string, any>>("/api/documents", {
      method: "POST",
      body: value,
    });
  }

  function deleteById(id: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/documents/deleted/document`,
      {
        method: "PUT",
        query: { id },
      }
    );
  }

  function updateById(id: string, value: any) {
    return useNuxtApp().$api(`/api/documents/${id}`, {
      method: "PUT",
      body: value,
    });
  }

  function getById(id: string) {
    return useNuxtApp().$api<Record<string, any>>(`/api/documents/${id}`, {
      method: "GET",
    });
  }

  function getDocumentsBySiteId(siteId: string, type?: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/documents/site/${siteId}/type/${type}`,
      {
        method: "GET",
      }
    );
  }

  function create(value: any) {
    return add(value);
  }

  function createFolder(value: any) {
    return add({ ...value, type: "folder" });
  }

  return {
    getAll,
    getById,
    add,
    deleteById,
    updateById,
    getDocumentsBySiteId,
    create,
    createFolder,
  };
}
