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

import type { ThreadMentionableUser } from ".././models";

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

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

/**
 * List distinct users who have access to a thread (via ThreadAccess → Mailbox → MailboxAccess).
 */
export type threadsUsersListResponse200 = {
  data: ThreadMentionableUser[];
  status: 200;
};

export type threadsUsersListResponseSuccess = threadsUsersListResponse200 & {
  headers: Headers;
};
export type threadsUsersListResponse = threadsUsersListResponseSuccess;

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

export const threadsUsersList = async (
  threadId: string,
  options?: RequestInit,
): Promise<threadsUsersListResponse> => {
  return fetchAPI<threadsUsersListResponse>(getThreadsUsersListUrl(threadId), {
    ...options,
    method: "GET",
  });
};

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

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

  const queryKey =
    queryOptions?.queryKey ?? getThreadsUsersListQueryKey(threadId);

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

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

export type ThreadsUsersListQueryResult = NonNullable<
  Awaited<ReturnType<typeof threadsUsersList>>
>;
export type ThreadsUsersListQueryError = ErrorType<unknown>;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}
