/**
 * Generated by orval 🍺
 * Do not edit manually.
 * messages API
 * This is the messages API schema.
 * OpenAPI spec version: 1.0.0 (v1.0)
 */
import { useMutation, useQuery } from "@tanstack/react-query";
import type {
  DataTag,
  DefinedInitialDataOptions,
  DefinedUseQueryResult,
  MutationFunction,
  QueryClient,
  QueryFunction,
  QueryKey,
  UndefinedInitialDataOptions,
  UseMutationOptions,
  UseMutationResult,
  UseQueryOptions,
  UseQueryResult,
} from "@tanstack/react-query";

import type { BlobUploadCreate201, BlobUploadCreateBody } from ".././models";

import { fetchAPI } from "../../fetch-api";
import type { ErrorType } from "../../fetch-api";

type SecondParameter<T extends (...args: never) => unknown> = Parameters<T>[1];

/**
 * Download binary data for the specified blob ID.

This endpoint returns the raw binary content of a blob. Access is controlled
by checking if the user has access to any mailbox that owns this blob.
 */
export type blobDownloadRetrieveResponse200 = {
  data: void;
  status: 200;
};

export type blobDownloadRetrieveResponseSuccess =
  blobDownloadRetrieveResponse200 & {
    headers: Headers;
  };
export type blobDownloadRetrieveResponse = blobDownloadRetrieveResponseSuccess;

export const getBlobDownloadRetrieveUrl = (id: string) => {
  return `/api/v1.0/blob/${id}/download/`;
};

export const blobDownloadRetrieve = async (
  id: string,
  options?: RequestInit,
): Promise<blobDownloadRetrieveResponse> => {
  return fetchAPI<blobDownloadRetrieveResponse>(
    getBlobDownloadRetrieveUrl(id),
    {
      ...options,
      method: "GET",
    },
  );
};

export const getBlobDownloadRetrieveQueryKey = (id?: string) => {
  return [`/api/v1.0/blob/${id}/download/`] as const;
};

export const getBlobDownloadRetrieveQueryOptions = <
  TData = Awaited<ReturnType<typeof blobDownloadRetrieve>>,
  TError = ErrorType<unknown>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobDownloadRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
) => {
  const { query: queryOptions, request: requestOptions } = options ?? {};

  const queryKey =
    queryOptions?.queryKey ?? getBlobDownloadRetrieveQueryKey(id);

  const queryFn: QueryFunction<
    Awaited<ReturnType<typeof blobDownloadRetrieve>>
  > = ({ signal }) => blobDownloadRetrieve(id, { signal, ...requestOptions });

  return {
    queryKey,
    queryFn,
    enabled: !!id,
    ...queryOptions,
  } as UseQueryOptions<
    Awaited<ReturnType<typeof blobDownloadRetrieve>>,
    TError,
    TData
  > & { queryKey: DataTag<QueryKey, TData, TError> };
};

export type BlobDownloadRetrieveQueryResult = NonNullable<
  Awaited<ReturnType<typeof blobDownloadRetrieve>>
>;
export type BlobDownloadRetrieveQueryError = ErrorType<unknown>;

export function useBlobDownloadRetrieve<
  TData = Awaited<ReturnType<typeof blobDownloadRetrieve>>,
  TError = ErrorType<unknown>,
>(
  id: string,
  options: {
    query: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobDownloadRetrieve>>,
        TError,
        TData
      >
    > &
      Pick<
        DefinedInitialDataOptions<
          Awaited<ReturnType<typeof blobDownloadRetrieve>>,
          TError,
          Awaited<ReturnType<typeof blobDownloadRetrieve>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): DefinedUseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useBlobDownloadRetrieve<
  TData = Awaited<ReturnType<typeof blobDownloadRetrieve>>,
  TError = ErrorType<unknown>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobDownloadRetrieve>>,
        TError,
        TData
      >
    > &
      Pick<
        UndefinedInitialDataOptions<
          Awaited<ReturnType<typeof blobDownloadRetrieve>>,
          TError,
          Awaited<ReturnType<typeof blobDownloadRetrieve>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useBlobDownloadRetrieve<
  TData = Awaited<ReturnType<typeof blobDownloadRetrieve>>,
  TError = ErrorType<unknown>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobDownloadRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};

export function useBlobDownloadRetrieve<
  TData = Awaited<ReturnType<typeof blobDownloadRetrieve>>,
  TError = ErrorType<unknown>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobDownloadRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
} {
  const queryOptions = getBlobDownloadRetrieveQueryOptions(id, options);

  const query = useQuery(queryOptions, queryClient) as UseQueryResult<
    TData,
    TError
  > & { queryKey: DataTag<QueryKey, TData, TError> };

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * Serve a blob inline for the FilePreview viewer.

Sibling of ``download`` with the same authorization model but two
extra guarantees:

- the response Content-Type is the MIME type detected from the bytes
  (via ``python-magic``), not the value declared at upload time;
- the detected MIME must belong to ``PREVIEWABLE_MIME_TYPES``,
  otherwise the endpoint refuses with 415.

Returning 415 (rather than 200 with the raw payload) is the security
contract that lets the frontend render the response inline: any byte
we send back has been re-classified server-side as one of the safe
previewable types.
 */
export type blobPreviewRetrieveResponse200 = {
  data: Blob;
  status: 200;
};

export type blobPreviewRetrieveResponse400 = {
  data: void;
  status: 400;
};

export type blobPreviewRetrieveResponse403 = {
  data: void;
  status: 403;
};

export type blobPreviewRetrieveResponse404 = {
  data: void;
  status: 404;
};

export type blobPreviewRetrieveResponse415 = {
  data: void;
  status: 415;
};

export type blobPreviewRetrieveResponse500 = {
  data: void;
  status: 500;
};

export type blobPreviewRetrieveResponseSuccess =
  blobPreviewRetrieveResponse200 & {
    headers: Headers;
  };
export type blobPreviewRetrieveResponseError = (
  | blobPreviewRetrieveResponse400
  | blobPreviewRetrieveResponse403
  | blobPreviewRetrieveResponse404
  | blobPreviewRetrieveResponse415
  | blobPreviewRetrieveResponse500
) & {
  headers: Headers;
};

export type blobPreviewRetrieveResponse =
  | blobPreviewRetrieveResponseSuccess
  | blobPreviewRetrieveResponseError;

export const getBlobPreviewRetrieveUrl = (id: string) => {
  return `/api/v1.0/blob/${id}/preview/`;
};

export const blobPreviewRetrieve = async (
  id: string,
  options?: RequestInit,
): Promise<blobPreviewRetrieveResponse> => {
  return fetchAPI<blobPreviewRetrieveResponse>(getBlobPreviewRetrieveUrl(id), {
    ...options,
    method: "GET",
  });
};

export const getBlobPreviewRetrieveQueryKey = (id?: string) => {
  return [`/api/v1.0/blob/${id}/preview/`] as const;
};

export const getBlobPreviewRetrieveQueryOptions = <
  TData = Awaited<ReturnType<typeof blobPreviewRetrieve>>,
  TError = ErrorType<void>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobPreviewRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
) => {
  const { query: queryOptions, request: requestOptions } = options ?? {};

  const queryKey = queryOptions?.queryKey ?? getBlobPreviewRetrieveQueryKey(id);

  const queryFn: QueryFunction<
    Awaited<ReturnType<typeof blobPreviewRetrieve>>
  > = ({ signal }) => blobPreviewRetrieve(id, { signal, ...requestOptions });

  return {
    queryKey,
    queryFn,
    enabled: !!id,
    ...queryOptions,
  } as UseQueryOptions<
    Awaited<ReturnType<typeof blobPreviewRetrieve>>,
    TError,
    TData
  > & { queryKey: DataTag<QueryKey, TData, TError> };
};

export type BlobPreviewRetrieveQueryResult = NonNullable<
  Awaited<ReturnType<typeof blobPreviewRetrieve>>
>;
export type BlobPreviewRetrieveQueryError = ErrorType<void>;

export function useBlobPreviewRetrieve<
  TData = Awaited<ReturnType<typeof blobPreviewRetrieve>>,
  TError = ErrorType<void>,
>(
  id: string,
  options: {
    query: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobPreviewRetrieve>>,
        TError,
        TData
      >
    > &
      Pick<
        DefinedInitialDataOptions<
          Awaited<ReturnType<typeof blobPreviewRetrieve>>,
          TError,
          Awaited<ReturnType<typeof blobPreviewRetrieve>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): DefinedUseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useBlobPreviewRetrieve<
  TData = Awaited<ReturnType<typeof blobPreviewRetrieve>>,
  TError = ErrorType<void>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobPreviewRetrieve>>,
        TError,
        TData
      >
    > &
      Pick<
        UndefinedInitialDataOptions<
          Awaited<ReturnType<typeof blobPreviewRetrieve>>,
          TError,
          Awaited<ReturnType<typeof blobPreviewRetrieve>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useBlobPreviewRetrieve<
  TData = Awaited<ReturnType<typeof blobPreviewRetrieve>>,
  TError = ErrorType<void>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobPreviewRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};

export function useBlobPreviewRetrieve<
  TData = Awaited<ReturnType<typeof blobPreviewRetrieve>>,
  TError = ErrorType<void>,
>(
  id: string,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof blobPreviewRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
} {
  const queryOptions = getBlobPreviewRetrieveQueryOptions(id, options);

  const query = useQuery(queryOptions, queryClient) as UseQueryResult<
    TData,
    TError
  > & { queryKey: DataTag<QueryKey, TData, TError> };

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * Upload binary data and create a Blob record.
        This endpoint accepts multipart/form-data containing a file and returns a
        blob ID and other metadata. The blob is associated with the specified mailbox.
        
 */
export type blobUploadCreateResponse201 = {
  data: BlobUploadCreate201;
  status: 201;
};

export type blobUploadCreateResponse400 = {
  data: void;
  status: 400;
};

export type blobUploadCreateResponse403 = {
  data: void;
  status: 403;
};

export type blobUploadCreateResponse404 = {
  data: void;
  status: 404;
};

export type blobUploadCreateResponse413 = {
  data: void;
  status: 413;
};

export type blobUploadCreateResponse500 = {
  data: void;
  status: 500;
};

export type blobUploadCreateResponseSuccess = blobUploadCreateResponse201 & {
  headers: Headers;
};
export type blobUploadCreateResponseError = (
  | blobUploadCreateResponse400
  | blobUploadCreateResponse403
  | blobUploadCreateResponse404
  | blobUploadCreateResponse413
  | blobUploadCreateResponse500
) & {
  headers: Headers;
};

export type blobUploadCreateResponse =
  | blobUploadCreateResponseSuccess
  | blobUploadCreateResponseError;

export const getBlobUploadCreateUrl = (mailboxId: string) => {
  return `/api/v1.0/blob/upload/${mailboxId}/`;
};

export const blobUploadCreate = async (
  mailboxId: string,
  blobUploadCreateBody: BlobUploadCreateBody,
  options?: RequestInit,
): Promise<blobUploadCreateResponse> => {
  const formData = new FormData();
  formData.append(`file`, blobUploadCreateBody.file);

  return fetchAPI<blobUploadCreateResponse>(getBlobUploadCreateUrl(mailboxId), {
    ...options,
    method: "POST",
    body: formData,
  });
};

export const getBlobUploadCreateMutationOptions = <
  TError = ErrorType<void>,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof blobUploadCreate>>,
    TError,
    { mailboxId: string; data: BlobUploadCreateBody },
    TContext
  >;
  request?: SecondParameter<typeof fetchAPI>;
}): UseMutationOptions<
  Awaited<ReturnType<typeof blobUploadCreate>>,
  TError,
  { mailboxId: string; data: BlobUploadCreateBody },
  TContext
> => {
  const mutationKey = ["blobUploadCreate"];
  const { mutation: mutationOptions, request: requestOptions } = options
    ? options.mutation &&
      "mutationKey" in options.mutation &&
      options.mutation.mutationKey
      ? options
      : { ...options, mutation: { ...options.mutation, mutationKey } }
    : { mutation: { mutationKey }, request: undefined };

  const mutationFn: MutationFunction<
    Awaited<ReturnType<typeof blobUploadCreate>>,
    { mailboxId: string; data: BlobUploadCreateBody }
  > = (props) => {
    const { mailboxId, data } = props ?? {};

    return blobUploadCreate(mailboxId, data, requestOptions);
  };

  return { mutationFn, ...mutationOptions };
};

export type BlobUploadCreateMutationResult = NonNullable<
  Awaited<ReturnType<typeof blobUploadCreate>>
>;
export type BlobUploadCreateMutationBody = BlobUploadCreateBody;
export type BlobUploadCreateMutationError = ErrorType<void>;

export const useBlobUploadCreate = <
  TError = ErrorType<void>,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof blobUploadCreate>>,
      TError,
      { mailboxId: string; data: BlobUploadCreateBody },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof blobUploadCreate>>,
  TError,
  { mailboxId: string; data: BlobUploadCreateBody },
  TContext
> => {
  const mutationOptions = getBlobUploadCreateMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
