import { useQueryClient } from "@tanstack/react-query"

import api from "@shared/api"

const PRESETS_QUERY_KEY = ["get", "/api/captions/presets/"]

export const usePresetsQuery = () => {
	return api.useQuery("get", "/api/captions/presets/", {})
}

export const useCreatePreset = () => {
	const queryClient = useQueryClient()

	return api.useMutation("post", "/api/captions/presets/", {
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PRESETS_QUERY_KEY })
		},
	})
}

export const useUpdatePreset = () => {
	const queryClient = useQueryClient()

	return api.useMutation("patch", "/api/captions/presets/{preset_id}/", {
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PRESETS_QUERY_KEY })
		},
	})
}

export const useDeletePreset = () => {
	const queryClient = useQueryClient()

	return api.useMutation("delete", "/api/captions/presets/{preset_id}/", {
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: PRESETS_QUERY_KEY })
		},
	})
}
