/**
 * 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 {
  MaildomainAccessRead,
  MaildomainAccessWrite,
  MaildomainAccessWriteRequest,
} from ".././models";

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

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

/**
 * ViewSet for managing MaildomainAccess records for a specific Maildomain.
Access is allowed if the user is maildomain admin or a super user.
 */
export type maildomainsAccessesListResponse200 = {
  data: MaildomainAccessRead[];
  status: 200;
};

export type maildomainsAccessesListResponseSuccess =
  maildomainsAccessesListResponse200 & {
    headers: Headers;
  };
export type maildomainsAccessesListResponse =
  maildomainsAccessesListResponseSuccess;

export const getMaildomainsAccessesListUrl = (maildomainPk: string) => {
  return `/api/v1.0/maildomains/${maildomainPk}/accesses/`;
};

export const maildomainsAccessesList = async (
  maildomainPk: string,
  options?: RequestInit,
): Promise<maildomainsAccessesListResponse> => {
  return fetchAPI<maildomainsAccessesListResponse>(
    getMaildomainsAccessesListUrl(maildomainPk),
    {
      ...options,
      method: "GET",
    },
  );
};

export const getMaildomainsAccessesListQueryKey = (maildomainPk?: string) => {
  return [`/api/v1.0/maildomains/${maildomainPk}/accesses/`] as const;
};

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

  const queryKey =
    queryOptions?.queryKey ?? getMaildomainsAccessesListQueryKey(maildomainPk);

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

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

export type MaildomainsAccessesListQueryResult = NonNullable<
  Awaited<ReturnType<typeof maildomainsAccessesList>>
>;
export type MaildomainsAccessesListQueryError = unknown;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * ViewSet for managing MaildomainAccess records for a specific Maildomain.
Access is allowed if the user is maildomain admin or a super user.
 */
export type maildomainsAccessesCreateResponse201 = {
  data: MaildomainAccessWrite;
  status: 201;
};

export type maildomainsAccessesCreateResponseSuccess =
  maildomainsAccessesCreateResponse201 & {
    headers: Headers;
  };
export type maildomainsAccessesCreateResponse =
  maildomainsAccessesCreateResponseSuccess;

export const getMaildomainsAccessesCreateUrl = (maildomainPk: string) => {
  return `/api/v1.0/maildomains/${maildomainPk}/accesses/`;
};

export const maildomainsAccessesCreate = async (
  maildomainPk: string,
  maildomainAccessWriteRequest: MaildomainAccessWriteRequest,
  options?: RequestInit,
): Promise<maildomainsAccessesCreateResponse> => {
  return fetchAPI<maildomainsAccessesCreateResponse>(
    getMaildomainsAccessesCreateUrl(maildomainPk),
    {
      ...options,
      method: "POST",
      headers: { "Content-Type": "application/json", ...options?.headers },
      body: JSON.stringify(maildomainAccessWriteRequest),
    },
  );
};

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

    return maildomainsAccessesCreate(maildomainPk, data, requestOptions);
  };

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

export type MaildomainsAccessesCreateMutationResult = NonNullable<
  Awaited<ReturnType<typeof maildomainsAccessesCreate>>
>;
export type MaildomainsAccessesCreateMutationBody =
  MaildomainAccessWriteRequest;
export type MaildomainsAccessesCreateMutationError = unknown;

export const useMaildomainsAccessesCreate = <
  TError = unknown,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof maildomainsAccessesCreate>>,
      TError,
      { maildomainPk: string; data: MaildomainAccessWriteRequest },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof maildomainsAccessesCreate>>,
  TError,
  { maildomainPk: string; data: MaildomainAccessWriteRequest },
  TContext
> => {
  const mutationOptions = getMaildomainsAccessesCreateMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
/**
 * ViewSet for managing MaildomainAccess records for a specific Maildomain.
Access is allowed if the user is maildomain admin or a super user.
 */
export type maildomainsAccessesRetrieveResponse200 = {
  data: MaildomainAccessRead;
  status: 200;
};

export type maildomainsAccessesRetrieveResponseSuccess =
  maildomainsAccessesRetrieveResponse200 & {
    headers: Headers;
  };
export type maildomainsAccessesRetrieveResponse =
  maildomainsAccessesRetrieveResponseSuccess;

export const getMaildomainsAccessesRetrieveUrl = (
  maildomainPk: string,
  id: string,
) => {
  return `/api/v1.0/maildomains/${maildomainPk}/accesses/${id}/`;
};

export const maildomainsAccessesRetrieve = async (
  maildomainPk: string,
  id: string,
  options?: RequestInit,
): Promise<maildomainsAccessesRetrieveResponse> => {
  return fetchAPI<maildomainsAccessesRetrieveResponse>(
    getMaildomainsAccessesRetrieveUrl(maildomainPk, id),
    {
      ...options,
      method: "GET",
    },
  );
};

export const getMaildomainsAccessesRetrieveQueryKey = (
  maildomainPk?: string,
  id?: string,
) => {
  return [`/api/v1.0/maildomains/${maildomainPk}/accesses/${id}/`] as const;
};

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

  const queryKey =
    queryOptions?.queryKey ??
    getMaildomainsAccessesRetrieveQueryKey(maildomainPk, id);

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

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

export type MaildomainsAccessesRetrieveQueryResult = NonNullable<
  Awaited<ReturnType<typeof maildomainsAccessesRetrieve>>
>;
export type MaildomainsAccessesRetrieveQueryError = unknown;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * ViewSet for managing MaildomainAccess records for a specific Maildomain.
Access is allowed if the user is maildomain admin or a super user.
 */
export type maildomainsAccessesDestroyResponse204 = {
  data: void;
  status: 204;
};

export type maildomainsAccessesDestroyResponseSuccess =
  maildomainsAccessesDestroyResponse204 & {
    headers: Headers;
  };
export type maildomainsAccessesDestroyResponse =
  maildomainsAccessesDestroyResponseSuccess;

export const getMaildomainsAccessesDestroyUrl = (
  maildomainPk: string,
  id: string,
) => {
  return `/api/v1.0/maildomains/${maildomainPk}/accesses/${id}/`;
};

export const maildomainsAccessesDestroy = async (
  maildomainPk: string,
  id: string,
  options?: RequestInit,
): Promise<maildomainsAccessesDestroyResponse> => {
  return fetchAPI<maildomainsAccessesDestroyResponse>(
    getMaildomainsAccessesDestroyUrl(maildomainPk, id),
    {
      ...options,
      method: "DELETE",
    },
  );
};

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

    return maildomainsAccessesDestroy(maildomainPk, id, requestOptions);
  };

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

export type MaildomainsAccessesDestroyMutationResult = NonNullable<
  Awaited<ReturnType<typeof maildomainsAccessesDestroy>>
>;

export type MaildomainsAccessesDestroyMutationError = unknown;

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

  return useMutation(mutationOptions, queryClient);
};
