import { getChildWorkflowsCacheKey as e, getWorkflowCacheKey as t, getWorkflowConfigCacheKey as n, getWorkflowSourceCacheKey as r, getWorkflowsCacheKey as i } from "./query-keys.js";
import { useApiClient as a } from "./useApi.js";
import { useMutation as o, useQuery as s, useQueryClient as c } from "@tanstack/react-query";
//#region src/hooks/useWorkflows.ts
function l(e) {
	let { envKey: n, api: r } = a();
	return s({
		queryKey: t(n, e),
		queryFn: () => r.workflows.getById({ id: e }),
		enabled: !!e
	});
}
function u(e, t, n = "id", r = "DESC", o = 0, c = 10) {
	let { envKey: l, api: u } = a(), d = Object.keys(t).length > 0 ? JSON.stringify(t) : void 0, f = {
		...d && { filter: d },
		sortBy: JSON.stringify([{
			field: n,
			order: r
		}]),
		page: o,
		limit: c,
		...e && {
			search: e,
			searchColumns: JSON.stringify(["title", "model"])
		}
	};
	return s({
		queryKey: [
			...i(l),
			"list",
			e ?? "",
			d ?? "",
			n,
			r,
			o,
			c
		],
		queryFn: () => u.workflows.getAll(f)
	});
}
function d() {
	let { envKey: e, api: t } = a(), n = c();
	return o({
		mutationFn: (e) => t.workflows.create(e),
		onSuccess: () => {
			n.invalidateQueries({ queryKey: i(e) });
		}
	});
}
function f() {
	let { envKey: e, api: n } = a(), r = c();
	return o({
		mutationFn: (e) => n.workflows.update(e),
		onSuccess: (n, a) => {
			r.invalidateQueries({ queryKey: t(e, a.id) }), r.invalidateQueries({ queryKey: i(e) });
		}
	});
}
function p() {
	let { envKey: e, api: n } = a(), r = c();
	return o({
		mutationFn: (e) => n.workflows.delete({ id: e }),
		onSuccess: (n, a) => {
			r.removeQueries({ queryKey: t(e, a) }), r.invalidateQueries({ queryKey: i(e) });
		}
	});
}
function m() {
	let { envKey: e, api: t } = a(), n = c();
	return o({
		mutationFn: (e) => t.workflows.batchDelete({ ids: e }),
		onSuccess: () => {
			n.invalidateQueries({ queryKey: i(e) });
		}
	});
}
function h(t, n = !0) {
	let { envKey: r, api: i } = a();
	return s({
		queryKey: e(r, t),
		queryFn: () => i.workflows.getAll({
			filter: JSON.stringify({ parentId: t }),
			sortBy: JSON.stringify([{
				field: "createdAt",
				order: "ASC"
			}]),
			page: 0,
			limit: 100
		}),
		select: (e) => e.data,
		enabled: n && !!t
	});
}
function g(e) {
	let { envKey: t, api: r } = a();
	return s({
		queryKey: n(t, e),
		queryFn: () => r.config.getWorkflowConfig({ alias: e }),
		enabled: !!e
	});
}
function _(e) {
	let { envKey: t, api: n } = a();
	return s({
		queryKey: r(t, e),
		queryFn: () => n.config.getWorkflowSource({ alias: e }),
		enabled: !!e
	});
}
function v(e) {
	let { envKey: n, api: r } = a();
	return s({
		queryKey: [...t(n, e), "checkpoints"],
		queryFn: () => r.workflows.getCheckpoints({ id: e }),
		enabled: !!e
	});
}
//#endregion
export { m as useBatchDeleteWorkflows, h as useChildWorkflows, d as useCreateWorkflow, p as useDeleteWorkflow, u as useFilterWorkflows, f as useUpdateWorkflow, l as useWorkflow, v as useWorkflowCheckpoints, g as useWorkflowConfigByName, _ as useWorkflowSource };
