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

import {
	type Options,
	listVolumes,
	createVolume,
	testConnection,
	deleteVolume,
	getVolume,
	updateVolume,
	getContainersUsingVolume,
	mountVolume,
	unmountVolume,
	healthCheckVolume,
} from "../sdk.gen";
import { queryOptions, type UseMutationOptions, type DefaultError } from "@tanstack/react-query";
import type {
	ListVolumesData,
	CreateVolumeData,
	CreateVolumeResponse,
	TestConnectionData,
	TestConnectionResponse,
	DeleteVolumeData,
	DeleteVolumeResponse,
	GetVolumeData,
	UpdateVolumeData,
	UpdateVolumeResponse,
	GetContainersUsingVolumeData,
	MountVolumeData,
	MountVolumeResponse,
	UnmountVolumeData,
	UnmountVolumeResponse,
	HealthCheckVolumeData,
	HealthCheckVolumeResponse,
} from "../types.gen";
import { client as _heyApiClient } from "../client.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 ?? _heyApiClient).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 listVolumesQueryKey = (options?: Options<ListVolumesData>) => createQueryKey("listVolumes", options);

/**
 * List all volumes
 */
export const listVolumesOptions = (options?: Options<ListVolumesData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await listVolumes({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: listVolumesQueryKey(options),
	});
};

export const createVolumeQueryKey = (options?: Options<CreateVolumeData>) => createQueryKey("createVolume", options);

/**
 * Create a new volume
 */
export const createVolumeOptions = (options?: Options<CreateVolumeData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await createVolume({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: createVolumeQueryKey(options),
	});
};

/**
 * Create a new volume
 */
export const createVolumeMutation = (
	options?: Partial<Options<CreateVolumeData>>,
): UseMutationOptions<CreateVolumeResponse, DefaultError, Options<CreateVolumeData>> => {
	const mutationOptions: UseMutationOptions<CreateVolumeResponse, DefaultError, Options<CreateVolumeData>> = {
		mutationFn: async (localOptions) => {
			const { data } = await createVolume({
				...options,
				...localOptions,
				throwOnError: true,
			});
			return data;
		},
	};
	return mutationOptions;
};

export const testConnectionQueryKey = (options?: Options<TestConnectionData>) =>
	createQueryKey("testConnection", options);

/**
 * Test connection to backend
 */
export const testConnectionOptions = (options?: Options<TestConnectionData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await testConnection({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: testConnectionQueryKey(options),
	});
};

/**
 * Test connection to backend
 */
export const testConnectionMutation = (
	options?: Partial<Options<TestConnectionData>>,
): UseMutationOptions<TestConnectionResponse, DefaultError, Options<TestConnectionData>> => {
	const mutationOptions: UseMutationOptions<TestConnectionResponse, DefaultError, Options<TestConnectionData>> = {
		mutationFn: async (localOptions) => {
			const { data } = await testConnection({
				...options,
				...localOptions,
				throwOnError: true,
			});
			return data;
		},
	};
	return mutationOptions;
};

/**
 * Delete a volume
 */
export const deleteVolumeMutation = (
	options?: Partial<Options<DeleteVolumeData>>,
): UseMutationOptions<DeleteVolumeResponse, DefaultError, Options<DeleteVolumeData>> => {
	const mutationOptions: UseMutationOptions<DeleteVolumeResponse, DefaultError, Options<DeleteVolumeData>> = {
		mutationFn: async (localOptions) => {
			const { data } = await deleteVolume({
				...options,
				...localOptions,
				throwOnError: true,
			});
			return data;
		},
	};
	return mutationOptions;
};

export const getVolumeQueryKey = (options: Options<GetVolumeData>) => createQueryKey("getVolume", options);

/**
 * Get a volume by name
 */
export const getVolumeOptions = (options: Options<GetVolumeData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await getVolume({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: getVolumeQueryKey(options),
	});
};

/**
 * Update a volume's configuration
 */
export const updateVolumeMutation = (
	options?: Partial<Options<UpdateVolumeData>>,
): UseMutationOptions<UpdateVolumeResponse, DefaultError, Options<UpdateVolumeData>> => {
	const mutationOptions: UseMutationOptions<UpdateVolumeResponse, DefaultError, Options<UpdateVolumeData>> = {
		mutationFn: async (localOptions) => {
			const { data } = await updateVolume({
				...options,
				...localOptions,
				throwOnError: true,
			});
			return data;
		},
	};
	return mutationOptions;
};

export const getContainersUsingVolumeQueryKey = (options: Options<GetContainersUsingVolumeData>) =>
	createQueryKey("getContainersUsingVolume", options);

/**
 * Get containers using a volume by name
 */
export const getContainersUsingVolumeOptions = (options: Options<GetContainersUsingVolumeData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await getContainersUsingVolume({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: getContainersUsingVolumeQueryKey(options),
	});
};

export const mountVolumeQueryKey = (options: Options<MountVolumeData>) => createQueryKey("mountVolume", options);

/**
 * Mount a volume
 */
export const mountVolumeOptions = (options: Options<MountVolumeData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await mountVolume({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: mountVolumeQueryKey(options),
	});
};

/**
 * Mount a volume
 */
export const mountVolumeMutation = (
	options?: Partial<Options<MountVolumeData>>,
): UseMutationOptions<MountVolumeResponse, DefaultError, Options<MountVolumeData>> => {
	const mutationOptions: UseMutationOptions<MountVolumeResponse, DefaultError, Options<MountVolumeData>> = {
		mutationFn: async (localOptions) => {
			const { data } = await mountVolume({
				...options,
				...localOptions,
				throwOnError: true,
			});
			return data;
		},
	};
	return mutationOptions;
};

export const unmountVolumeQueryKey = (options: Options<UnmountVolumeData>) => createQueryKey("unmountVolume", options);

/**
 * Unmount a volume
 */
export const unmountVolumeOptions = (options: Options<UnmountVolumeData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await unmountVolume({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: unmountVolumeQueryKey(options),
	});
};

/**
 * Unmount a volume
 */
export const unmountVolumeMutation = (
	options?: Partial<Options<UnmountVolumeData>>,
): UseMutationOptions<UnmountVolumeResponse, DefaultError, Options<UnmountVolumeData>> => {
	const mutationOptions: UseMutationOptions<UnmountVolumeResponse, DefaultError, Options<UnmountVolumeData>> = {
		mutationFn: async (localOptions) => {
			const { data } = await unmountVolume({
				...options,
				...localOptions,
				throwOnError: true,
			});
			return data;
		},
	};
	return mutationOptions;
};

export const healthCheckVolumeQueryKey = (options: Options<HealthCheckVolumeData>) =>
	createQueryKey("healthCheckVolume", options);

/**
 * Perform a health check on a volume
 */
export const healthCheckVolumeOptions = (options: Options<HealthCheckVolumeData>) => {
	return queryOptions({
		queryFn: async ({ queryKey, signal }) => {
			const { data } = await healthCheckVolume({
				...options,
				...queryKey[0],
				signal,
				throwOnError: true,
			});
			return data;
		},
		queryKey: healthCheckVolumeQueryKey(options),
	});
};

/**
 * Perform a health check on a volume
 */
export const healthCheckVolumeMutation = (
	options?: Partial<Options<HealthCheckVolumeData>>,
): UseMutationOptions<HealthCheckVolumeResponse, DefaultError, Options<HealthCheckVolumeData>> => {
	const mutationOptions: UseMutationOptions<HealthCheckVolumeResponse, DefaultError, Options<HealthCheckVolumeData>> = {
		mutationFn: async (localOptions) => {
			const { data } = await healthCheckVolume({
				...options,
				...localOptions,
				throwOnError: true,
			});
			return data;
		},
	};
	return mutationOptions;
};
