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

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

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

/**
 * List contacts with optional filtering by mailbox and search query.
For a mailbox, it returns all contacts in the mailbox and all contacts
in the same domain as the mailbox.

Query parameters:
- mailbox_id: Optional UUID to filter contacts by mailbox
- q: Optional search query for name or email (case insensitive)
 */
export type contactsListResponse200 = {
  data: Contact[];
  status: 200;
};

export type contactsListResponseSuccess = contactsListResponse200 & {
  headers: Headers;
};
export type contactsListResponse = contactsListResponseSuccess;

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

export const contactsList = async (
  params?: ContactsListParams,
  options?: RequestInit,
): Promise<contactsListResponse> => {
  return fetchAPI<contactsListResponse>(getContactsListUrl(params), {
    ...options,
    method: "GET",
  });
};

export const getContactsListQueryKey = (params?: ContactsListParams) => {
  return [`/api/v1.0/contacts/`, ...(params ? [params] : [])] as const;
};

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

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

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

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

export type ContactsListQueryResult = NonNullable<
  Awaited<ReturnType<typeof contactsList>>
>;
export type ContactsListQueryError = ErrorType<unknown>;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

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

export type contactsRetrieveResponseSuccess = contactsRetrieveResponse200 & {
  headers: Headers;
};
export type contactsRetrieveResponse = contactsRetrieveResponseSuccess;

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

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

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

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

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

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

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

export type ContactsRetrieveQueryResult = NonNullable<
  Awaited<ReturnType<typeof contactsRetrieve>>
>;
export type ContactsRetrieveQueryError = ErrorType<unknown>;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}
