import { getWorkspaceCacheKey as e, getWorkspacesCacheKey as t } from "./query-keys.js";
import { useApiClient as n } from "./useApi.js";
import { useMutation as r, useQuery as i, useQueryClient as a } from "@tanstack/react-query";
//#region src/hooks/useWorkspaces.ts
function o(t) {
	let { envKey: r, api: a } = n();
	return i({
		queryKey: e(r, t),
		queryFn: () => a.workspaces.getById({ id: t }),
		enabled: !!t
	});
}
function s(e, r, a = "id", o = "DESC", s = 0, c = 10) {
	let { envKey: l, api: u } = n(), d = Object.keys(r).length > 0 ? JSON.stringify(r) : void 0, f = {
		...d && { filter: d },
		sortBy: JSON.stringify([{
			field: a,
			order: o
		}]),
		page: s,
		limit: c,
		...e && {
			search: e,
			searchColumns: JSON.stringify(["title"])
		}
	};
	return i({
		queryKey: [
			...t(l),
			"list",
			e ?? "",
			d ?? "",
			a,
			o,
			s,
			c
		],
		queryFn: () => u.workspaces.getAll(f)
	});
}
function c() {
	let { envKey: e, api: i } = n(), o = a();
	return r({
		mutationFn: (e) => i.workspaces.create(e),
		onSuccess: () => {
			o.invalidateQueries({ queryKey: t(e) });
		}
	});
}
function l() {
	let { envKey: i, api: o } = n(), s = a();
	return r({
		mutationFn: (e) => o.workspaces.update(e),
		onSuccess: (n, r) => {
			s.invalidateQueries({ queryKey: e(i, r.id) }), s.invalidateQueries({ queryKey: t(i) });
		}
	});
}
function u() {
	let { envKey: i, api: o } = n(), s = a();
	return r({
		mutationFn: (e) => o.workspaces.delete({ id: e }),
		onSuccess: (n, r) => {
			s.removeQueries({ queryKey: e(i, r) }), s.invalidateQueries({ queryKey: t(i) });
		}
	});
}
function d() {
	let { envKey: e, api: i } = n(), o = a();
	return r({
		mutationFn: ({ id: e, isFavourite: t }) => i.workspaces.setFavourite({
			id: e,
			workspaceFavouriteDto: { isFavourite: t }
		}),
		onSuccess: () => {
			o.invalidateQueries({ queryKey: t(e) });
		}
	});
}
function f() {
	let { envKey: e, api: i } = n(), o = a();
	return r({
		mutationFn: (e) => i.workspaces.batchDelete({ ids: e }),
		onSuccess: () => {
			o.invalidateQueries({ queryKey: t(e) });
		}
	});
}
//#endregion
export { f as useBatchDeleteWorkspaces, c as useCreateWorkspace, u as useDeleteWorkspace, s as useFilterWorkspaces, d as useSetFavouriteWorkspace, l as useUpdateWorkspace, o as useWorkspace };
