import { useApiClient } from "./useApi.js";
import { c } from "react/compiler-runtime";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
function getWorkflowsCacheKey(e, t) {
	return [
		"workflows",
		e,
		t
	];
}
function getWorkflowCacheKey(e, t) {
	return [
		"workflow",
		e,
		t
	];
}
function getWorkflowsByPipelineCacheKey(e, t) {
	return [
		"workflows-by-pipeline",
		e,
		t
	];
}
function useWorkflow(n) {
	let i = c(10), { envKey: a, api: s } = useApiClient(), l;
	i[0] !== a || i[1] !== n ? (l = getWorkflowCacheKey(a, n), i[0] = a, i[1] = n, i[2] = l) : l = i[2];
	let d;
	i[3] !== s || i[4] !== n ? (d = () => {
		if (!s) throw Error("API not available");
		return s.ApiV1WorkflowsApi.workflowControllerGetWorkflowById({ id: n });
	}, i[3] = s, i[4] = n, i[5] = d) : d = i[5];
	let f = !!n, p;
	return i[6] !== l || i[7] !== d || i[8] !== f ? (p = {
		queryKey: l,
		queryFn: d,
		enabled: f,
		select: _temp
	}, i[6] = l, i[7] = d, i[8] = f, i[9] = p) : p = i[9], useQuery(p);
}
function _temp(e) {
	return e.data;
}
function useFetchWorkflowsByPipeline(n) {
	let i = c(14), { envKey: a, api: o } = useApiClient(), l;
	i[0] === n ? l = i[1] : (l = JSON.stringify({ pipelineId: n }), i[0] = n, i[1] = l);
	let u;
	i[2] === Symbol.for("react.memo_cache_sentinel") ? (u = JSON.stringify([{
		field: "index",
		order: "ASC"
	}]), i[2] = u) : u = i[2];
	let d;
	i[3] === l ? d = i[4] : (d = {
		filter: l,
		sortBy: u
	}, i[3] = l, i[4] = d);
	let p = d, m;
	i[5] !== a || i[6] !== n ? (m = getWorkflowsByPipelineCacheKey(a, n), i[5] = a, i[6] = n, i[7] = m) : m = i[7];
	let h;
	i[8] !== o || i[9] !== p ? (h = () => {
		if (!o) throw Error("API not available");
		return o.ApiV1WorkflowsApi.workflowControllerGetWorkflows(p);
	}, i[8] = o, i[9] = p, i[10] = h) : h = i[10];
	let g;
	return i[11] !== m || i[12] !== h ? (g = {
		queryKey: m,
		queryFn: h,
		select: _temp2,
		enabled: !0
	}, i[11] = m, i[12] = h, i[13] = g) : g = i[13], useQuery(g);
}
function _temp2(e) {
	return e.data.data;
}
function useFetchWorkflowsByNamespace(n) {
	let i = c(14), { envKey: o, api: s } = useApiClient(), l;
	i[0] === n ? l = i[1] : (l = JSON.stringify({ namespaceId: n }), i[0] = n, i[1] = l);
	let u;
	i[2] === Symbol.for("react.memo_cache_sentinel") ? (u = JSON.stringify([{
		field: "index",
		order: "ASC"
	}]), i[2] = u) : u = i[2];
	let d;
	i[3] === l ? d = i[4] : (d = {
		filter: l,
		sortBy: u
	}, i[3] = l, i[4] = d);
	let f = d, p;
	i[5] !== o || i[6] !== n ? (p = getWorkflowsCacheKey(o, n), i[5] = o, i[6] = n, i[7] = p) : p = i[7];
	let h;
	i[8] !== s || i[9] !== f ? (h = () => {
		if (!s) throw Error("API not available");
		return s.ApiV1WorkflowsApi.workflowControllerGetWorkflows(f);
	}, i[8] = s, i[9] = f, i[10] = h) : h = i[10];
	let g;
	return i[11] !== p || i[12] !== h ? (g = {
		queryKey: p,
		queryFn: h,
		select: _temp3,
		enabled: !0
	}, i[11] = p, i[12] = h, i[13] = g) : g = i[13], useQuery(g);
}
function _temp3(e) {
	return e.data.data;
}
function useDeleteWorkflow() {
	let r = c(8), { envKey: l, api: u } = useApiClient(), d = useQueryClient(), f;
	r[0] === u ? f = r[1] : (f = (e) => {
		if (!u) throw Error("API not available");
		return u.ApiV1WorkflowsApi.workflowControllerDeleteWorkflow({ id: e.id });
	}, r[0] = u, r[1] = f);
	let p;
	r[2] !== l || r[3] !== d ? (p = (e, t) => {
		d.removeQueries({ queryKey: getWorkflowCacheKey(l, t.id) }), d.invalidateQueries({ queryKey: getWorkflowsCacheKey(l, t.namespaceId) }), d.invalidateQueries({ queryKey: getWorkflowsByPipelineCacheKey(l, t.pipelineId) });
	}, r[2] = l, r[3] = d, r[4] = p) : p = r[4];
	let m;
	return r[5] !== f || r[6] !== p ? (m = {
		mutationFn: f,
		onSuccess: p
	}, r[5] = f, r[6] = p, r[7] = m) : m = r[7], useMutation(m);
}
export { getWorkflowCacheKey, getWorkflowsByPipelineCacheKey, getWorkflowsCacheKey, useDeleteWorkflow, useFetchWorkflowsByNamespace, useFetchWorkflowsByPipeline, useWorkflow };
