// This file is auto-generated by @hey-api/openapi-ts

import { queryOptions, type UseMutationOptions } from "@tanstack/react-query";
import type { AxiosError } from "axios";
import { client as _heyApiClient } from "../client.gen";
import {
    getBoards,
    getDeals,
    getProjects,
    getStatuses,
    updateBoard,
    updateDeal,
    updateStatus,
    type Options,
} from "../sdk.gen";
import type {
    GetBoardsData,
    GetDealsData,
    GetProjectsData,
    GetStatusesData,
    UpdateBoardData,
    UpdateBoardError,
    UpdateBoardResponse2,
    UpdateDealData,
    UpdateDealError,
    UpdateDealResponse2,
    UpdateStatusData,
    UpdateStatusError,
    UpdateStatusResponse2,
} from "../types.gen";

export type QueryKey<TOptions extends Options> = [
    Pick<TOptions, "baseURL" | "body" | "headers" | "path" | "query"> & {
        _id: string;
        _infinite?: boolean;
    },
];

const createQueryKey = <TOptions extends Options>(
    id: string,
    options?: TOptions,
    infinite?: boolean
): [QueryKey<TOptions>[0]] => {
    const params: QueryKey<TOptions>[0] = {
        _id: id,
        baseURL:
            options?.baseURL ||
            (options?.client ?? _heyApiClient).getConfig().baseURL,
    } as QueryKey<TOptions>[0];
    if (infinite) {
        params._infinite = infinite;
    }
    if (options?.body) {
        params.body = options.body;
    }
    if (options?.headers) {
        params.headers = options.headers;
    }
    if (options?.path) {
        params.path = options.path;
    }
    if (options?.query) {
        params.query = options.query;
    }
    return [params];
};

export const getProjectsQueryKey = (options?: Options<GetProjectsData>) =>
    createQueryKey("getProjects", options);

/**
 * Get Projects
 */
export const getProjectsOptions = (options?: Options<GetProjectsData>) => {
    return queryOptions({
        queryFn: async ({ queryKey, signal }) => {
            const { data } = await getProjects({
                ...options,
                ...queryKey[0],
                signal,
                throwOnError: true,
            });
            return data;
        },
        queryKey: getProjectsQueryKey(options),
    });
};

export const getBoardsQueryKey = (options: Options<GetBoardsData>) =>
    createQueryKey("getBoards", options);

/**
 * Get Boards
 */
export const getBoardsOptions = (options: Options<GetBoardsData>) => {
    return queryOptions({
        queryFn: async ({ queryKey, signal }) => {
            const { data } = await getBoards({
                ...options,
                ...queryKey[0],
                signal,
                throwOnError: true,
            });
            return data;
        },
        queryKey: getBoardsQueryKey(options),
    });
};

/**
 * Update Board
 */
export const updateBoardMutation = (
    options?: Partial<Options<UpdateBoardData>>
): UseMutationOptions<
    UpdateBoardResponse2,
    AxiosError<UpdateBoardError>,
    Options<UpdateBoardData>
> => {
    const mutationOptions: UseMutationOptions<
        UpdateBoardResponse2,
        AxiosError<UpdateBoardError>,
        Options<UpdateBoardData>
    > = {
        mutationFn: async localOptions => {
            const { data } = await updateBoard({
                ...options,
                ...localOptions,
                throwOnError: true,
            });
            return data;
        },
    };
    return mutationOptions;
};

export const getStatusesQueryKey = (options: Options<GetStatusesData>) =>
    createQueryKey("getStatuses", options);

/**
 * Get Statuses
 */
export const getStatusesOptions = (options: Options<GetStatusesData>) => {
    return queryOptions({
        queryFn: async ({ queryKey, signal }) => {
            const { data } = await getStatuses({
                ...options,
                ...queryKey[0],
                signal,
                throwOnError: true,
            });
            return data;
        },
        queryKey: getStatusesQueryKey(options),
    });
};

/**
 * Update Status
 */
export const updateStatusMutation = (
    options?: Partial<Options<UpdateStatusData>>
): UseMutationOptions<
    UpdateStatusResponse2,
    AxiosError<UpdateStatusError>,
    Options<UpdateStatusData>
> => {
    const mutationOptions: UseMutationOptions<
        UpdateStatusResponse2,
        AxiosError<UpdateStatusError>,
        Options<UpdateStatusData>
    > = {
        mutationFn: async localOptions => {
            const { data } = await updateStatus({
                ...options,
                ...localOptions,
                throwOnError: true,
            });
            return data;
        },
    };
    return mutationOptions;
};

export const getDealsQueryKey = (options: Options<GetDealsData>) =>
    createQueryKey("getDeals", options);

/**
 * Get Deals
 */
export const getDealsOptions = (options: Options<GetDealsData>) => {
    return queryOptions({
        queryFn: async ({ queryKey, signal }) => {
            const { data } = await getDeals({
                ...options,
                ...queryKey[0],
                signal,
                throwOnError: true,
            });
            return data;
        },
        queryKey: getDealsQueryKey(options),
    });
};

/**
 * Update Deal
 */
export const updateDealMutation = (
    options?: Partial<Options<UpdateDealData>>
): UseMutationOptions<
    UpdateDealResponse2,
    AxiosError<UpdateDealError>,
    Options<UpdateDealData>
> => {
    const mutationOptions: UseMutationOptions<
        UpdateDealResponse2,
        AxiosError<UpdateDealError>,
        Options<UpdateDealData>
    > = {
        mutationFn: async localOptions => {
            const { data } = await updateDeal({
                ...options,
                ...localOptions,
                throwOnError: true,
            });
            return data;
        },
    };
    return mutationOptions;
};
