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

import type {
  PaginatedThreadList,
  Thread,
  ThreadsListParams,
  ThreadsRefreshSummaryCreate200,
  ThreadsStatsRetrieve200,
  ThreadsStatsRetrieve400,
  ThreadsStatsRetrieveParams,
  ThreadsSummaryRetrieve200,
} from ".././models";

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

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

/**
 * List threads with optional search functionality.
 */
export type threadsListResponse200 = {
  data: PaginatedThreadList;
  status: 200;
};

export type threadsListResponseSuccess = threadsListResponse200 & {
  headers: Headers;
};
export type threadsListResponse = threadsListResponseSuccess;

export const getThreadsListUrl = (params?: ThreadsListParams) => {
  const normalizedParams = new URLSearchParams();

  Object.entries(params || {}).forEach(([key, value]) => {
    if (value !== undefined) {
      normalizedParams.append(key, value === null ? "null" : value.toString());
    }
  });

  const stringifiedParams = normalizedParams.toString();

  return stringifiedParams.length > 0
    ? `/api/v1.0/threads/?${stringifiedParams}`
    : `/api/v1.0/threads/`;
};

export const threadsList = async (
  params?: ThreadsListParams,
  options?: RequestInit,
): Promise<threadsListResponse> => {
  return fetchAPI<threadsListResponse>(getThreadsListUrl(params), {
    ...options,
    method: "GET",
  });
};

export const getThreadsListInfiniteQueryKey = (params?: ThreadsListParams) => {
  return [
    "infinite",
    `/api/v1.0/threads/`,
    ...(params ? [params] : []),
  ] as const;
};

export const getThreadsListQueryKey = (params?: ThreadsListParams) => {
  return [`/api/v1.0/threads/`, ...(params ? [params] : [])] as const;
};

export const getThreadsListInfiniteQueryOptions = <
  TData = InfiniteData<
    Awaited<ReturnType<typeof threadsList>>,
    ThreadsListParams["page"]
  >,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseInfiniteQueryOptions<
        Awaited<ReturnType<typeof threadsList>>,
        TError,
        TData,
        QueryKey,
        ThreadsListParams["page"]
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
) => {
  const { query: queryOptions, request: requestOptions } = options ?? {};

  const queryKey =
    queryOptions?.queryKey ?? getThreadsListInfiniteQueryKey(params);

  const queryFn: QueryFunction<
    Awaited<ReturnType<typeof threadsList>>,
    QueryKey,
    ThreadsListParams["page"]
  > = ({ signal, pageParam }) =>
    threadsList(
      { ...params, page: pageParam || params?.["page"] },
      { signal, ...requestOptions },
    );

  return { queryKey, queryFn, ...queryOptions } as UseInfiniteQueryOptions<
    Awaited<ReturnType<typeof threadsList>>,
    TError,
    TData,
    QueryKey,
    ThreadsListParams["page"]
  > & { queryKey: DataTag<QueryKey, TData, TError> };
};

export type ThreadsListInfiniteQueryResult = NonNullable<
  Awaited<ReturnType<typeof threadsList>>
>;
export type ThreadsListInfiniteQueryError = unknown;

export function useThreadsListInfinite<
  TData = InfiniteData<
    Awaited<ReturnType<typeof threadsList>>,
    ThreadsListParams["page"]
  >,
  TError = unknown,
>(
  params: undefined | ThreadsListParams,
  options: {
    query: Partial<
      UseInfiniteQueryOptions<
        Awaited<ReturnType<typeof threadsList>>,
        TError,
        TData,
        QueryKey,
        ThreadsListParams["page"]
      >
    > &
      Pick<
        DefinedInitialDataOptions<
          Awaited<ReturnType<typeof threadsList>>,
          TError,
          Awaited<ReturnType<typeof threadsList>>,
          QueryKey
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): DefinedUseInfiniteQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useThreadsListInfinite<
  TData = InfiniteData<
    Awaited<ReturnType<typeof threadsList>>,
    ThreadsListParams["page"]
  >,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseInfiniteQueryOptions<
        Awaited<ReturnType<typeof threadsList>>,
        TError,
        TData,
        QueryKey,
        ThreadsListParams["page"]
      >
    > &
      Pick<
        UndefinedInitialDataOptions<
          Awaited<ReturnType<typeof threadsList>>,
          TError,
          Awaited<ReturnType<typeof threadsList>>,
          QueryKey
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseInfiniteQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useThreadsListInfinite<
  TData = InfiniteData<
    Awaited<ReturnType<typeof threadsList>>,
    ThreadsListParams["page"]
  >,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseInfiniteQueryOptions<
        Awaited<ReturnType<typeof threadsList>>,
        TError,
        TData,
        QueryKey,
        ThreadsListParams["page"]
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseInfiniteQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};

export function useThreadsListInfinite<
  TData = InfiniteData<
    Awaited<ReturnType<typeof threadsList>>,
    ThreadsListParams["page"]
  >,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseInfiniteQueryOptions<
        Awaited<ReturnType<typeof threadsList>>,
        TError,
        TData,
        QueryKey,
        ThreadsListParams["page"]
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseInfiniteQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
} {
  const queryOptions = getThreadsListInfiniteQueryOptions(params, options);

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

export const getThreadsListQueryOptions = <
  TData = Awaited<ReturnType<typeof threadsList>>,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof threadsList>>, TError, TData>
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
) => {
  const { query: queryOptions, request: requestOptions } = options ?? {};

  const queryKey = queryOptions?.queryKey ?? getThreadsListQueryKey(params);

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

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

export type ThreadsListQueryResult = NonNullable<
  Awaited<ReturnType<typeof threadsList>>
>;
export type ThreadsListQueryError = unknown;

export function useThreadsList<
  TData = Awaited<ReturnType<typeof threadsList>>,
  TError = unknown,
>(
  params: undefined | ThreadsListParams,
  options: {
    query: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof threadsList>>, TError, TData>
    > &
      Pick<
        DefinedInitialDataOptions<
          Awaited<ReturnType<typeof threadsList>>,
          TError,
          Awaited<ReturnType<typeof threadsList>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): DefinedUseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useThreadsList<
  TData = Awaited<ReturnType<typeof threadsList>>,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof threadsList>>, TError, TData>
    > &
      Pick<
        UndefinedInitialDataOptions<
          Awaited<ReturnType<typeof threadsList>>,
          TError,
          Awaited<ReturnType<typeof threadsList>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useThreadsList<
  TData = Awaited<ReturnType<typeof threadsList>>,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof threadsList>>, TError, TData>
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};

export function useThreadsList<
  TData = Awaited<ReturnType<typeof threadsList>>,
  TError = unknown,
>(
  params?: ThreadsListParams,
  options?: {
    query?: Partial<
      UseQueryOptions<Awaited<ReturnType<typeof threadsList>>, TError, TData>
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
} {
  const queryOptions = getThreadsListQueryOptions(params, options);

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * ViewSet for Thread model.
 */
export type threadsRetrieveResponse200 = {
  data: Thread;
  status: 200;
};

export type threadsRetrieveResponseSuccess = threadsRetrieveResponse200 & {
  headers: Headers;
};
export type threadsRetrieveResponse = threadsRetrieveResponseSuccess;

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

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

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

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

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

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

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

export type ThreadsRetrieveQueryResult = NonNullable<
  Awaited<ReturnType<typeof threadsRetrieve>>
>;
export type ThreadsRetrieveQueryError = unknown;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * Delete a thread, requiring EDITOR role on the thread.
 */
export type threadsDestroyResponse204 = {
  data: void;
  status: 204;
};

export type threadsDestroyResponseSuccess = threadsDestroyResponse204 & {
  headers: Headers;
};
export type threadsDestroyResponse = threadsDestroyResponseSuccess;

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

export const threadsDestroy = async (
  id: string,
  options?: RequestInit,
): Promise<threadsDestroyResponse> => {
  return fetchAPI<threadsDestroyResponse>(getThreadsDestroyUrl(id), {
    ...options,
    method: "DELETE",
  });
};

export const getThreadsDestroyMutationOptions = <
  TError = unknown,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof threadsDestroy>>,
    TError,
    { id: string },
    TContext
  >;
  request?: SecondParameter<typeof fetchAPI>;
}): UseMutationOptions<
  Awaited<ReturnType<typeof threadsDestroy>>,
  TError,
  { id: string },
  TContext
> => {
  const mutationKey = ["threadsDestroy"];
  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 threadsDestroy>>,
    { id: string }
  > = (props) => {
    const { id } = props ?? {};

    return threadsDestroy(id, requestOptions);
  };

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

export type ThreadsDestroyMutationResult = NonNullable<
  Awaited<ReturnType<typeof threadsDestroy>>
>;

export type ThreadsDestroyMutationError = unknown;

export const useThreadsDestroy = <TError = unknown, TContext = unknown>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof threadsDestroy>>,
      TError,
      { id: string },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof threadsDestroy>>,
  TError,
  { id: string },
  TContext
> => {
  const mutationOptions = getThreadsDestroyMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
/**
 * Refresh the summary of a thread.
 */
export type threadsRefreshSummaryCreateResponse200 = {
  data: ThreadsRefreshSummaryCreate200;
  status: 200;
};

export type threadsRefreshSummaryCreateResponse403 = {
  data: unknown;
  status: 403;
};

export type threadsRefreshSummaryCreateResponseSuccess =
  threadsRefreshSummaryCreateResponse200 & {
    headers: Headers;
  };
export type threadsRefreshSummaryCreateResponseError =
  threadsRefreshSummaryCreateResponse403 & {
    headers: Headers;
  };

export type threadsRefreshSummaryCreateResponse =
  | threadsRefreshSummaryCreateResponseSuccess
  | threadsRefreshSummaryCreateResponseError;

export const getThreadsRefreshSummaryCreateUrl = (id: string) => {
  return `/api/v1.0/threads/${id}/refresh-summary/`;
};

export const threadsRefreshSummaryCreate = async (
  id: string,
  options?: RequestInit,
): Promise<threadsRefreshSummaryCreateResponse> => {
  return fetchAPI<threadsRefreshSummaryCreateResponse>(
    getThreadsRefreshSummaryCreateUrl(id),
    {
      ...options,
      method: "POST",
    },
  );
};

export const getThreadsRefreshSummaryCreateMutationOptions = <
  TError = unknown,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof threadsRefreshSummaryCreate>>,
    TError,
    { id: string },
    TContext
  >;
  request?: SecondParameter<typeof fetchAPI>;
}): UseMutationOptions<
  Awaited<ReturnType<typeof threadsRefreshSummaryCreate>>,
  TError,
  { id: string },
  TContext
> => {
  const mutationKey = ["threadsRefreshSummaryCreate"];
  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 threadsRefreshSummaryCreate>>,
    { id: string }
  > = (props) => {
    const { id } = props ?? {};

    return threadsRefreshSummaryCreate(id, requestOptions);
  };

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

export type ThreadsRefreshSummaryCreateMutationResult = NonNullable<
  Awaited<ReturnType<typeof threadsRefreshSummaryCreate>>
>;

export type ThreadsRefreshSummaryCreateMutationError = unknown;

export const useThreadsRefreshSummaryCreate = <
  TError = unknown,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof threadsRefreshSummaryCreate>>,
      TError,
      { id: string },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof threadsRefreshSummaryCreate>>,
  TError,
  { id: string },
  TContext
> => {
  const mutationOptions =
    getThreadsRefreshSummaryCreateMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
/**
 * Retrieve the summary of a thread.
 */
export type threadsSummaryRetrieveResponse200 = {
  data: ThreadsSummaryRetrieve200;
  status: 200;
};

export type threadsSummaryRetrieveResponse403 = {
  data: unknown;
  status: 403;
};

export type threadsSummaryRetrieveResponseSuccess =
  threadsSummaryRetrieveResponse200 & {
    headers: Headers;
  };
export type threadsSummaryRetrieveResponseError =
  threadsSummaryRetrieveResponse403 & {
    headers: Headers;
  };

export type threadsSummaryRetrieveResponse =
  | threadsSummaryRetrieveResponseSuccess
  | threadsSummaryRetrieveResponseError;

export const getThreadsSummaryRetrieveUrl = (id: string) => {
  return `/api/v1.0/threads/${id}/summary/`;
};

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

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

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

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

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

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

export type ThreadsSummaryRetrieveQueryResult = NonNullable<
  Awaited<ReturnType<typeof threadsSummaryRetrieve>>
>;
export type ThreadsSummaryRetrieveQueryError = unknown;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * Get aggregated statistics for threads based on filters.
 */
export type threadsStatsRetrieveResponse200 = {
  data: ThreadsStatsRetrieve200;
  status: 200;
};

export type threadsStatsRetrieveResponse400 = {
  data: ThreadsStatsRetrieve400;
  status: 400;
};

export type threadsStatsRetrieveResponseSuccess =
  threadsStatsRetrieveResponse200 & {
    headers: Headers;
  };
export type threadsStatsRetrieveResponseError =
  threadsStatsRetrieveResponse400 & {
    headers: Headers;
  };

export type threadsStatsRetrieveResponse =
  | threadsStatsRetrieveResponseSuccess
  | threadsStatsRetrieveResponseError;

export const getThreadsStatsRetrieveUrl = (
  params: ThreadsStatsRetrieveParams,
) => {
  const normalizedParams = new URLSearchParams();

  Object.entries(params || {}).forEach(([key, value]) => {
    if (value !== undefined) {
      normalizedParams.append(key, value === null ? "null" : value.toString());
    }
  });

  const stringifiedParams = normalizedParams.toString();

  return stringifiedParams.length > 0
    ? `/api/v1.0/threads/stats/?${stringifiedParams}`
    : `/api/v1.0/threads/stats/`;
};

export const threadsStatsRetrieve = async (
  params: ThreadsStatsRetrieveParams,
  options?: RequestInit,
): Promise<threadsStatsRetrieveResponse> => {
  return fetchAPI<threadsStatsRetrieveResponse>(
    getThreadsStatsRetrieveUrl(params),
    {
      ...options,
      method: "GET",
    },
  );
};

export const getThreadsStatsRetrieveQueryKey = (
  params?: ThreadsStatsRetrieveParams,
) => {
  return [`/api/v1.0/threads/stats/`, ...(params ? [params] : [])] as const;
};

export const getThreadsStatsRetrieveQueryOptions = <
  TData = Awaited<ReturnType<typeof threadsStatsRetrieve>>,
  TError = ThreadsStatsRetrieve400,
>(
  params: ThreadsStatsRetrieveParams,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof threadsStatsRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
) => {
  const { query: queryOptions, request: requestOptions } = options ?? {};

  const queryKey =
    queryOptions?.queryKey ?? getThreadsStatsRetrieveQueryKey(params);

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

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

export type ThreadsStatsRetrieveQueryResult = NonNullable<
  Awaited<ReturnType<typeof threadsStatsRetrieve>>
>;
export type ThreadsStatsRetrieveQueryError = ThreadsStatsRetrieve400;

export function useThreadsStatsRetrieve<
  TData = Awaited<ReturnType<typeof threadsStatsRetrieve>>,
  TError = ThreadsStatsRetrieve400,
>(
  params: ThreadsStatsRetrieveParams,
  options: {
    query: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof threadsStatsRetrieve>>,
        TError,
        TData
      >
    > &
      Pick<
        DefinedInitialDataOptions<
          Awaited<ReturnType<typeof threadsStatsRetrieve>>,
          TError,
          Awaited<ReturnType<typeof threadsStatsRetrieve>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): DefinedUseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useThreadsStatsRetrieve<
  TData = Awaited<ReturnType<typeof threadsStatsRetrieve>>,
  TError = ThreadsStatsRetrieve400,
>(
  params: ThreadsStatsRetrieveParams,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof threadsStatsRetrieve>>,
        TError,
        TData
      >
    > &
      Pick<
        UndefinedInitialDataOptions<
          Awaited<ReturnType<typeof threadsStatsRetrieve>>,
          TError,
          Awaited<ReturnType<typeof threadsStatsRetrieve>>
        >,
        "initialData"
      >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};
export function useThreadsStatsRetrieve<
  TData = Awaited<ReturnType<typeof threadsStatsRetrieve>>,
  TError = ThreadsStatsRetrieve400,
>(
  params: ThreadsStatsRetrieveParams,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof threadsStatsRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
};

export function useThreadsStatsRetrieve<
  TData = Awaited<ReturnType<typeof threadsStatsRetrieve>>,
  TError = ThreadsStatsRetrieve400,
>(
  params: ThreadsStatsRetrieveParams,
  options?: {
    query?: Partial<
      UseQueryOptions<
        Awaited<ReturnType<typeof threadsStatsRetrieve>>,
        TError,
        TData
      >
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseQueryResult<TData, TError> & {
  queryKey: DataTag<QueryKey, TData, TError>;
} {
  const queryOptions = getThreadsStatsRetrieveQueryOptions(params, options);

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

  query.queryKey = queryOptions.queryKey;

  return query;
}
