/**
 * 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 { UserWithoutAbilities, UsersListParams } from ".././models";

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

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

/**
 * List users.
Search users by email, full name or maildomain.
A search query of at least 3 characters is required.
 */
export type usersListResponse200 = {
  data: UserWithoutAbilities[];
  status: 200;
};

export type usersListResponseSuccess = usersListResponse200 & {
  headers: Headers;
};
export type usersListResponse = usersListResponseSuccess;

export const getUsersListUrl = (params?: UsersListParams) => {
  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/users/?${stringifiedParams}`
    : `/api/v1.0/users/`;
};

export const usersList = async (
  params?: UsersListParams,
  options?: RequestInit,
): Promise<usersListResponse> => {
  return fetchAPI<usersListResponse>(getUsersListUrl(params), {
    ...options,
    method: "GET",
  });
};

export const getUsersListQueryKey = (params?: UsersListParams) => {
  return [`/api/v1.0/users/`, ...(params ? [params] : [])] as const;
};

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

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

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

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

export type UsersListQueryResult = NonNullable<
  Awaited<ReturnType<typeof usersList>>
>;
export type UsersListQueryError = unknown;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}
