/**
 * 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 } from "@tanstack/react-query";
import type {
  MutationFunction,
  QueryClient,
  UseMutationOptions,
  UseMutationResult,
} from "@tanstack/react-query";

import type {
  ChangeFlagRequestRequest,
  FlagCreate200,
  FlagCreate400,
  FlagCreate403,
} from ".././models";

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

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

/**
 * Change a specific flag (unread, starred, trashed, archived, spam) for multiple messages or all messages within multiple threads. Uses request body.
 */
export type flagCreateResponse200 = {
  data: FlagCreate200;
  status: 200;
};

export type flagCreateResponse400 = {
  data: FlagCreate400;
  status: 400;
};

export type flagCreateResponse403 = {
  data: FlagCreate403;
  status: 403;
};

export type flagCreateResponseSuccess = flagCreateResponse200 & {
  headers: Headers;
};
export type flagCreateResponseError = (
  | flagCreateResponse400
  | flagCreateResponse403
) & {
  headers: Headers;
};

export type flagCreateResponse =
  | flagCreateResponseSuccess
  | flagCreateResponseError;

export const getFlagCreateUrl = () => {
  return `/api/v1.0/flag/`;
};

export const flagCreate = async (
  changeFlagRequestRequest: ChangeFlagRequestRequest,
  options?: RequestInit,
): Promise<flagCreateResponse> => {
  return fetchAPI<flagCreateResponse>(getFlagCreateUrl(), {
    ...options,
    method: "POST",
    headers: { "Content-Type": "application/json", ...options?.headers },
    body: JSON.stringify(changeFlagRequestRequest),
  });
};

export const getFlagCreateMutationOptions = <
  TError = ErrorType<FlagCreate400 | FlagCreate403>,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof flagCreate>>,
    TError,
    { data: ChangeFlagRequestRequest },
    TContext
  >;
  request?: SecondParameter<typeof fetchAPI>;
}): UseMutationOptions<
  Awaited<ReturnType<typeof flagCreate>>,
  TError,
  { data: ChangeFlagRequestRequest },
  TContext
> => {
  const mutationKey = ["flagCreate"];
  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 flagCreate>>,
    { data: ChangeFlagRequestRequest }
  > = (props) => {
    const { data } = props ?? {};

    return flagCreate(data, requestOptions);
  };

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

export type FlagCreateMutationResult = NonNullable<
  Awaited<ReturnType<typeof flagCreate>>
>;
export type FlagCreateMutationBody = ChangeFlagRequestRequest;
export type FlagCreateMutationError = ErrorType<FlagCreate400 | FlagCreate403>;

export const useFlagCreate = <
  TError = ErrorType<FlagCreate400 | FlagCreate403>,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof flagCreate>>,
      TError,
      { data: ChangeFlagRequestRequest },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof flagCreate>>,
  TError,
  { data: ChangeFlagRequestRequest },
  TContext
> => {
  const mutationOptions = getFlagCreateMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
