/**
 * 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 {
  PatchedThreadAccessRequest,
  ThreadAccess,
  ThreadAccessRequest,
  ThreadsAccessesCreateParams,
  ThreadsAccessesDestroyParams,
  ThreadsAccessesListParams,
  ThreadsAccessesPartialUpdateParams,
  ThreadsAccessesUpdateParams,
} from ".././models";

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

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

/**
 * ViewSet for ThreadAccess model.
 */
export type threadsAccessesListResponse200 = {
  data: ThreadAccess[];
  status: 200;
};

export type threadsAccessesListResponseSuccess =
  threadsAccessesListResponse200 & {
    headers: Headers;
  };
export type threadsAccessesListResponse = threadsAccessesListResponseSuccess;

export const getThreadsAccessesListUrl = (
  threadId: string,
  params?: ThreadsAccessesListParams,
) => {
  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/threads/${threadId}/accesses/?${stringifiedParams}`
    : `/api/v1.0/threads/${threadId}/accesses/`;
};

export const threadsAccessesList = async (
  threadId: string,
  params?: ThreadsAccessesListParams,
  options?: RequestInit,
): Promise<threadsAccessesListResponse> => {
  return fetchAPI<threadsAccessesListResponse>(
    getThreadsAccessesListUrl(threadId, params),
    {
      ...options,
      method: "GET",
    },
  );
};

export const getThreadsAccessesListQueryKey = (
  threadId?: string,
  params?: ThreadsAccessesListParams,
) => {
  return [
    `/api/v1.0/threads/${threadId}/accesses/`,
    ...(params ? [params] : []),
  ] as const;
};

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

  const queryKey =
    queryOptions?.queryKey ?? getThreadsAccessesListQueryKey(threadId, params);

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

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

export type ThreadsAccessesListQueryResult = NonNullable<
  Awaited<ReturnType<typeof threadsAccessesList>>
>;
export type ThreadsAccessesListQueryError = ErrorType<unknown>;

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

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

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

  query.queryKey = queryOptions.queryKey;

  return query;
}

/**
 * Create a new thread access.
 */
export type threadsAccessesCreateResponse201 = {
  data: ThreadAccess;
  status: 201;
};

export type threadsAccessesCreateResponseSuccess =
  threadsAccessesCreateResponse201 & {
    headers: Headers;
  };
export type threadsAccessesCreateResponse =
  threadsAccessesCreateResponseSuccess;

export const getThreadsAccessesCreateUrl = (
  threadId: string,
  params?: ThreadsAccessesCreateParams,
) => {
  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/threads/${threadId}/accesses/?${stringifiedParams}`
    : `/api/v1.0/threads/${threadId}/accesses/`;
};

export const threadsAccessesCreate = async (
  threadId: string,
  threadAccessRequest: ThreadAccessRequest,
  params?: ThreadsAccessesCreateParams,
  options?: RequestInit,
): Promise<threadsAccessesCreateResponse> => {
  return fetchAPI<threadsAccessesCreateResponse>(
    getThreadsAccessesCreateUrl(threadId, params),
    {
      ...options,
      method: "POST",
      headers: { "Content-Type": "application/json", ...options?.headers },
      body: JSON.stringify(threadAccessRequest),
    },
  );
};

export const getThreadsAccessesCreateMutationOptions = <
  TError = ErrorType<unknown>,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof threadsAccessesCreate>>,
    TError,
    {
      threadId: string;
      data: ThreadAccessRequest;
      params?: ThreadsAccessesCreateParams;
    },
    TContext
  >;
  request?: SecondParameter<typeof fetchAPI>;
}): UseMutationOptions<
  Awaited<ReturnType<typeof threadsAccessesCreate>>,
  TError,
  {
    threadId: string;
    data: ThreadAccessRequest;
    params?: ThreadsAccessesCreateParams;
  },
  TContext
> => {
  const mutationKey = ["threadsAccessesCreate"];
  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 threadsAccessesCreate>>,
    {
      threadId: string;
      data: ThreadAccessRequest;
      params?: ThreadsAccessesCreateParams;
    }
  > = (props) => {
    const { threadId, data, params } = props ?? {};

    return threadsAccessesCreate(threadId, data, params, requestOptions);
  };

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

export type ThreadsAccessesCreateMutationResult = NonNullable<
  Awaited<ReturnType<typeof threadsAccessesCreate>>
>;
export type ThreadsAccessesCreateMutationBody = ThreadAccessRequest;
export type ThreadsAccessesCreateMutationError = ErrorType<unknown>;

export const useThreadsAccessesCreate = <
  TError = ErrorType<unknown>,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof threadsAccessesCreate>>,
      TError,
      {
        threadId: string;
        data: ThreadAccessRequest;
        params?: ThreadsAccessesCreateParams;
      },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof threadsAccessesCreate>>,
  TError,
  {
    threadId: string;
    data: ThreadAccessRequest;
    params?: ThreadsAccessesCreateParams;
  },
  TContext
> => {
  const mutationOptions = getThreadsAccessesCreateMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
/**
 * ViewSet for ThreadAccess model.
 */
export type threadsAccessesUpdateResponse200 = {
  data: ThreadAccess;
  status: 200;
};

export type threadsAccessesUpdateResponseSuccess =
  threadsAccessesUpdateResponse200 & {
    headers: Headers;
  };
export type threadsAccessesUpdateResponse =
  threadsAccessesUpdateResponseSuccess;

export const getThreadsAccessesUpdateUrl = (
  threadId: string,
  id: string,
  params?: ThreadsAccessesUpdateParams,
) => {
  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/threads/${threadId}/accesses/${id}/?${stringifiedParams}`
    : `/api/v1.0/threads/${threadId}/accesses/${id}/`;
};

export const threadsAccessesUpdate = async (
  threadId: string,
  id: string,
  threadAccessRequest: ThreadAccessRequest,
  params?: ThreadsAccessesUpdateParams,
  options?: RequestInit,
): Promise<threadsAccessesUpdateResponse> => {
  return fetchAPI<threadsAccessesUpdateResponse>(
    getThreadsAccessesUpdateUrl(threadId, id, params),
    {
      ...options,
      method: "PUT",
      headers: { "Content-Type": "application/json", ...options?.headers },
      body: JSON.stringify(threadAccessRequest),
    },
  );
};

export const getThreadsAccessesUpdateMutationOptions = <
  TError = ErrorType<unknown>,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof threadsAccessesUpdate>>,
    TError,
    {
      threadId: string;
      id: string;
      data: ThreadAccessRequest;
      params?: ThreadsAccessesUpdateParams;
    },
    TContext
  >;
  request?: SecondParameter<typeof fetchAPI>;
}): UseMutationOptions<
  Awaited<ReturnType<typeof threadsAccessesUpdate>>,
  TError,
  {
    threadId: string;
    id: string;
    data: ThreadAccessRequest;
    params?: ThreadsAccessesUpdateParams;
  },
  TContext
> => {
  const mutationKey = ["threadsAccessesUpdate"];
  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 threadsAccessesUpdate>>,
    {
      threadId: string;
      id: string;
      data: ThreadAccessRequest;
      params?: ThreadsAccessesUpdateParams;
    }
  > = (props) => {
    const { threadId, id, data, params } = props ?? {};

    return threadsAccessesUpdate(threadId, id, data, params, requestOptions);
  };

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

export type ThreadsAccessesUpdateMutationResult = NonNullable<
  Awaited<ReturnType<typeof threadsAccessesUpdate>>
>;
export type ThreadsAccessesUpdateMutationBody = ThreadAccessRequest;
export type ThreadsAccessesUpdateMutationError = ErrorType<unknown>;

export const useThreadsAccessesUpdate = <
  TError = ErrorType<unknown>,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof threadsAccessesUpdate>>,
      TError,
      {
        threadId: string;
        id: string;
        data: ThreadAccessRequest;
        params?: ThreadsAccessesUpdateParams;
      },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof threadsAccessesUpdate>>,
  TError,
  {
    threadId: string;
    id: string;
    data: ThreadAccessRequest;
    params?: ThreadsAccessesUpdateParams;
  },
  TContext
> => {
  const mutationOptions = getThreadsAccessesUpdateMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
/**
 * ViewSet for ThreadAccess model.
 */
export type threadsAccessesPartialUpdateResponse200 = {
  data: ThreadAccess;
  status: 200;
};

export type threadsAccessesPartialUpdateResponseSuccess =
  threadsAccessesPartialUpdateResponse200 & {
    headers: Headers;
  };
export type threadsAccessesPartialUpdateResponse =
  threadsAccessesPartialUpdateResponseSuccess;

export const getThreadsAccessesPartialUpdateUrl = (
  threadId: string,
  id: string,
  params?: ThreadsAccessesPartialUpdateParams,
) => {
  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/threads/${threadId}/accesses/${id}/?${stringifiedParams}`
    : `/api/v1.0/threads/${threadId}/accesses/${id}/`;
};

export const threadsAccessesPartialUpdate = async (
  threadId: string,
  id: string,
  patchedThreadAccessRequest: PatchedThreadAccessRequest,
  params?: ThreadsAccessesPartialUpdateParams,
  options?: RequestInit,
): Promise<threadsAccessesPartialUpdateResponse> => {
  return fetchAPI<threadsAccessesPartialUpdateResponse>(
    getThreadsAccessesPartialUpdateUrl(threadId, id, params),
    {
      ...options,
      method: "PATCH",
      headers: { "Content-Type": "application/json", ...options?.headers },
      body: JSON.stringify(patchedThreadAccessRequest),
    },
  );
};

export const getThreadsAccessesPartialUpdateMutationOptions = <
  TError = ErrorType<unknown>,
  TContext = unknown,
>(options?: {
  mutation?: UseMutationOptions<
    Awaited<ReturnType<typeof threadsAccessesPartialUpdate>>,
    TError,
    {
      threadId: string;
      id: string;
      data: PatchedThreadAccessRequest;
      params?: ThreadsAccessesPartialUpdateParams;
    },
    TContext
  >;
  request?: SecondParameter<typeof fetchAPI>;
}): UseMutationOptions<
  Awaited<ReturnType<typeof threadsAccessesPartialUpdate>>,
  TError,
  {
    threadId: string;
    id: string;
    data: PatchedThreadAccessRequest;
    params?: ThreadsAccessesPartialUpdateParams;
  },
  TContext
> => {
  const mutationKey = ["threadsAccessesPartialUpdate"];
  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 threadsAccessesPartialUpdate>>,
    {
      threadId: string;
      id: string;
      data: PatchedThreadAccessRequest;
      params?: ThreadsAccessesPartialUpdateParams;
    }
  > = (props) => {
    const { threadId, id, data, params } = props ?? {};

    return threadsAccessesPartialUpdate(
      threadId,
      id,
      data,
      params,
      requestOptions,
    );
  };

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

export type ThreadsAccessesPartialUpdateMutationResult = NonNullable<
  Awaited<ReturnType<typeof threadsAccessesPartialUpdate>>
>;
export type ThreadsAccessesPartialUpdateMutationBody =
  PatchedThreadAccessRequest;
export type ThreadsAccessesPartialUpdateMutationError = ErrorType<unknown>;

export const useThreadsAccessesPartialUpdate = <
  TError = ErrorType<unknown>,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof threadsAccessesPartialUpdate>>,
      TError,
      {
        threadId: string;
        id: string;
        data: PatchedThreadAccessRequest;
        params?: ThreadsAccessesPartialUpdateParams;
      },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof threadsAccessesPartialUpdate>>,
  TError,
  {
    threadId: string;
    id: string;
    data: PatchedThreadAccessRequest;
    params?: ThreadsAccessesPartialUpdateParams;
  },
  TContext
> => {
  const mutationOptions =
    getThreadsAccessesPartialUpdateMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
/**
 * ViewSet for ThreadAccess model.
 */
export type threadsAccessesDestroyResponse204 = {
  data: void;
  status: 204;
};

export type threadsAccessesDestroyResponseSuccess =
  threadsAccessesDestroyResponse204 & {
    headers: Headers;
  };
export type threadsAccessesDestroyResponse =
  threadsAccessesDestroyResponseSuccess;

export const getThreadsAccessesDestroyUrl = (
  threadId: string,
  id: string,
  params?: ThreadsAccessesDestroyParams,
) => {
  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/threads/${threadId}/accesses/${id}/?${stringifiedParams}`
    : `/api/v1.0/threads/${threadId}/accesses/${id}/`;
};

export const threadsAccessesDestroy = async (
  threadId: string,
  id: string,
  params?: ThreadsAccessesDestroyParams,
  options?: RequestInit,
): Promise<threadsAccessesDestroyResponse> => {
  return fetchAPI<threadsAccessesDestroyResponse>(
    getThreadsAccessesDestroyUrl(threadId, id, params),
    {
      ...options,
      method: "DELETE",
    },
  );
};

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

    return threadsAccessesDestroy(threadId, id, params, requestOptions);
  };

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

export type ThreadsAccessesDestroyMutationResult = NonNullable<
  Awaited<ReturnType<typeof threadsAccessesDestroy>>
>;

export type ThreadsAccessesDestroyMutationError = ErrorType<unknown>;

export const useThreadsAccessesDestroy = <
  TError = ErrorType<unknown>,
  TContext = unknown,
>(
  options?: {
    mutation?: UseMutationOptions<
      Awaited<ReturnType<typeof threadsAccessesDestroy>>,
      TError,
      { threadId: string; id: string; params?: ThreadsAccessesDestroyParams },
      TContext
    >;
    request?: SecondParameter<typeof fetchAPI>;
  },
  queryClient?: QueryClient,
): UseMutationResult<
  Awaited<ReturnType<typeof threadsAccessesDestroy>>,
  TError,
  { threadId: string; id: string; params?: ThreadsAccessesDestroyParams },
  TContext
> => {
  const mutationOptions = getThreadsAccessesDestroyMutationOptions(options);

  return useMutation(mutationOptions, queryClient);
};
