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

import { type DefaultError, queryOptions, type UseMutationOptions } from '@tanstack/react-query';

import { client } from '../client.gen';
import { bullXWebApiSpecServiceDesc, checkChannelConnectivity, createChannel, deleteChannel, getChannel, listChannelAdapters, listChannels, type Options, updateChannel } from '../sdk.gen';
import type { BullXWebApiSpecServiceDescData, BullXWebApiSpecServiceDescResponse, CheckChannelConnectivityData, CheckChannelConnectivityError, CheckChannelConnectivityResponse, CreateChannelData, CreateChannelError, CreateChannelResponse, DeleteChannelData, DeleteChannelError, DeleteChannelResponse, GetChannelData, GetChannelError, GetChannelResponse, ListChannelAdaptersData, ListChannelAdaptersResponse, ListChannelsData, ListChannelsResponse, UpdateChannelData, UpdateChannelError, UpdateChannelResponse } from '../types.gen';

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

const createQueryKey = <TOptions extends Options>(id: string, options?: TOptions, infinite?: boolean, tags?: ReadonlyArray<string>): [
    QueryKey<TOptions>[0]
] => {
    const params: QueryKey<TOptions>[0] = { _id: id, baseUrl: options?.baseUrl || (options?.client ?? client).getConfig().baseUrl } as QueryKey<TOptions>[0];
    if (infinite) {
        params._infinite = infinite;
    }
    if (tags) {
        params.tags = tags;
    }
    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 listChannelAdaptersQueryKey = (options?: Options<ListChannelAdaptersData>) => createQueryKey('listChannelAdapters', options);

/**
 * List channel adapters
 *
 * Lists installed channel adapters and the schema describing their config form.
 */
export const listChannelAdaptersOptions = (options?: Options<ListChannelAdaptersData>) => queryOptions<ListChannelAdaptersResponse, DefaultError, ListChannelAdaptersResponse, ReturnType<typeof listChannelAdaptersQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await listChannelAdapters({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: listChannelAdaptersQueryKey(options)
});

export const listChannelsQueryKey = (options?: Options<ListChannelsData>) => createQueryKey('listChannels', options);

/**
 * List channels
 *
 * Lists every configured channel source across all enabled adapters.
 */
export const listChannelsOptions = (options?: Options<ListChannelsData>) => queryOptions<ListChannelsResponse, DefaultError, ListChannelsResponse, ReturnType<typeof listChannelsQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await listChannels({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: listChannelsQueryKey(options)
});

/**
 * Create a channel
 */
export const createChannelMutation = (options?: Partial<Options<CreateChannelData>>): UseMutationOptions<CreateChannelResponse, CreateChannelError, Options<CreateChannelData>> => {
    const mutationOptions: UseMutationOptions<CreateChannelResponse, CreateChannelError, Options<CreateChannelData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await createChannel({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

/**
 * Check channel connectivity
 *
 * Validates a source payload and probes the adapter without persisting anything.
 */
export const checkChannelConnectivityMutation = (options?: Partial<Options<CheckChannelConnectivityData>>): UseMutationOptions<CheckChannelConnectivityResponse, CheckChannelConnectivityError, Options<CheckChannelConnectivityData>> => {
    const mutationOptions: UseMutationOptions<CheckChannelConnectivityResponse, CheckChannelConnectivityError, Options<CheckChannelConnectivityData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await checkChannelConnectivity({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

/**
 * Delete a channel
 */
export const deleteChannelMutation = (options?: Partial<Options<DeleteChannelData>>): UseMutationOptions<DeleteChannelResponse, DeleteChannelError, Options<DeleteChannelData>> => {
    const mutationOptions: UseMutationOptions<DeleteChannelResponse, DeleteChannelError, Options<DeleteChannelData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await deleteChannel({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

export const getChannelQueryKey = (options: Options<GetChannelData>) => createQueryKey('getChannel', options);

/**
 * Get a channel
 */
export const getChannelOptions = (options: Options<GetChannelData>) => queryOptions<GetChannelResponse, GetChannelError, GetChannelResponse, ReturnType<typeof getChannelQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await getChannel({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: getChannelQueryKey(options)
});

/**
 * Update a channel
 */
export const updateChannelMutation = (options?: Partial<Options<UpdateChannelData>>): UseMutationOptions<UpdateChannelResponse, UpdateChannelError, Options<UpdateChannelData>> => {
    const mutationOptions: UseMutationOptions<UpdateChannelResponse, UpdateChannelError, Options<UpdateChannelData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await updateChannel({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

export const bullXWebApiSpecServiceDescQueryKey = (options?: Options<BullXWebApiSpecServiceDescData>) => createQueryKey('bullXWebApiSpecServiceDesc', options);

/**
 * Well-known service description
 */
export const bullXWebApiSpecServiceDescOptions = (options?: Options<BullXWebApiSpecServiceDescData>) => queryOptions<BullXWebApiSpecServiceDescResponse, DefaultError, BullXWebApiSpecServiceDescResponse, ReturnType<typeof bullXWebApiSpecServiceDescQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await bullXWebApiSpecServiceDesc({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: bullXWebApiSpecServiceDescQueryKey(options)
});
