import { useStudio } from "../../providers/StudioProvider.js";
import { useBatchDeleteWorkspaces, useDeleteWorkspace, useFilterWorkspaces, useSetFavouriteWorkspace } from "../../hooks/useWorkspaces.js";
import { useComponentOverrides } from "../../providers/ComponentOverridesProvider.js";
import { Badge } from "../../components/ui/badge.js";
import { Dialog, DialogContent } from "../../components/ui/dialog.js";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "../../components/ui/tooltip.js";
import ListView_default from "../../components/lists/ListView.js";
import { useWorkspaceConfig } from "../../hooks/useConfig.js";
import { useDebounce } from "../../hooks/useDebounce.js";
import CreateWorkspace_default from "./components/CreateWorkspace.js";
import { c } from "react/compiler-runtime";
import { useEffect, useState } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { useSearchParams } from "react-router-dom";
import { Star } from "lucide-react";
var Workspaces_default = () => {
	let n = c(65), { CreateWorkspace: Oe, EditWorkspace: ke } = useComponentOverrides(), r = Oe ?? CreateWorkspace_default, i = ke ?? CreateWorkspace_default, { router: Ae } = useStudio(), [a] = useSearchParams(), [o, je] = useState(0), [s, Me] = useState(10), [l, Ne] = useState("createdAt"), [u, Pe] = useState("ASC"), [d, Fe] = useState(), f;
	n[0] === Symbol.for("react.memo_cache_sentinel") ? (f = {}, n[0] = f) : f = n[0];
	let [p, Ie] = useState(f), Le = useDebounce(d, 500), [m, h] = useState(!1), [g, Re] = useState(void 0), _, v;
	n[1] === a ? (_ = n[2], v = n[3]) : (_ = () => {
		a.get("create") === "true" && h(!0);
	}, v = [a], n[1] = a, n[2] = _, n[3] = v), useEffect(_, v);
	let y = useWorkspaceConfig(), b = useFilterWorkspaces(Le, p, l, u, o, s), x = useDeleteWorkspace(), ze = useBatchDeleteWorkspaces(), S = useSetFavouriteWorkspace(), C;
	n[4] === x ? C = n[5] : (C = (e) => {
		x.mutate(e);
	}, n[4] = x, n[5] = C);
	let w = C, T;
	n[6] === ze ? T = n[7] : (T = (e) => {
		ze.mutate(e);
	}, n[6] = ze, n[7] = T);
	let E = T, D;
	n[8] === Symbol.for("react.memo_cache_sentinel") ? (D = () => {
		h(!0);
	}, n[8] = D) : D = n[8];
	let Be = D, O;
	n[9] === Symbol.for("react.memo_cache_sentinel") ? (O = () => {
		h(!1);
	}, n[9] = O) : O = n[9];
	let Ve = O, k;
	n[10] === Ae ? k = n[11] : (k = (e) => {
		Ae.navigateToWorkspace(e);
	}, n[10] = Ae, n[11] = k);
	let A = k, j;
	n[12] === Symbol.for("react.memo_cache_sentinel") ? (j = (e) => {
		Re(e);
	}, n[12] = j) : j = n[12];
	let He = j, M;
	n[13] === Symbol.for("react.memo_cache_sentinel") ? (M = () => {
		h(!1), Re(void 0);
	}, n[13] = M) : M = n[13];
	let Ue = M, N = y.isPending || b.isPending, P = b.error ?? y.error ?? null, F;
	n[14] === b.data?.data ? F = n[15] : (F = b.data?.data ?? [], n[14] = b.data?.data, n[15] = F);
	let I = b.data?.total ?? 0, L;
	n[16] === Symbol.for("react.memo_cache_sentinel") ? (L = {
		id: "id",
		label: "ID",
		minWidth: 30,
		format: _temp
	}, n[16] = L) : L = n[16];
	let R;
	n[17] === Symbol.for("react.memo_cache_sentinel") ? (R = [
		L,
		{
			id: "title",
			label: "Title",
			minWidth: 100,
			format: (e) => /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, {
				asChild: !0,
				children: /* @__PURE__ */ jsx(Badge, {
					variant: "outline",
					className: "hover:bg-primary/10 cursor-pointer",
					onClick: () => Ie((t) => ({
						...t,
						blockName: e
					})),
					children: e.length > 25 ? e.slice(0, 25) + "..." : e
				})
			}), /* @__PURE__ */ jsx(TooltipContent, { children: /* @__PURE__ */ jsx("p", { children: e }) })] }) })
		},
		{
			id: "environments",
			label: "Environments",
			minWidth: 150,
			format: _temp3
		},
		{
			id: "createdAt",
			label: "Date Created",
			minWidth: 100,
			format: _temp4
		}
	], n[17] = R) : R = n[17];
	let z;
	n[18] === Symbol.for("react.memo_cache_sentinel") ? (z = {}, n[18] = z) : z = n[18];
	let B;
	n[19] === Symbol.for("react.memo_cache_sentinel") ? (B = /* @__PURE__ */ jsx(Star, { className: "h-4 w-4" }), n[19] = B) : B = n[19];
	let V;
	n[20] === S ? V = n[21] : (V = {
		id: "add-favourite",
		label: "Add to favourites",
		icon: B,
		condition: _temp5,
		action: (e) => {
			S.mutate({
				id: e.id,
				isFavourite: !0
			});
		}
	}, n[20] = S, n[21] = V);
	let H;
	n[22] === Symbol.for("react.memo_cache_sentinel") ? (H = /* @__PURE__ */ jsx(Star, { className: "h-4 w-4 fill-current" }), n[22] = H) : H = n[22];
	let U;
	n[23] === S ? U = n[24] : (U = {
		id: "remove-favourite",
		label: "Remove from favourites",
		icon: H,
		condition: _temp6,
		action: (e) => {
			S.mutate({
				id: e.id,
				isFavourite: !1
			});
		}
	}, n[23] = S, n[24] = U);
	let W;
	n[25] !== V || n[26] !== U ? (W = [V, U], n[25] = V, n[26] = U, n[27] = W) : W = n[27];
	let We = W, G;
	n[28] !== p || n[29] !== E || n[30] !== w || n[31] !== A || n[32] !== u || n[33] !== l || n[34] !== o || n[35] !== s || n[36] !== d || n[37] !== N || n[38] !== P || n[39] !== F || n[40] !== I || n[41] !== We ? (G = /* @__PURE__ */ jsx(ListView_default, {
		loading: N,
		error: P,
		items: F,
		totalItems: I,
		setPage: je,
		setRowsPerPage: Me,
		setOrderBy: Ne,
		setOrder: Pe,
		setSearchTerm: Fe,
		setFilters: Ie,
		orderBy: l,
		order: u,
		searchTerm: d,
		filters: p,
		page: o,
		rowsPerPage: s,
		deleteItem: w,
		onClick: A,
		handleNew: Be,
		handleEdit: He,
		enableBatchActions: !0,
		batchDelete: E,
		columns: R,
		filterConfig: z,
		rowActions: We
	}), n[28] = p, n[29] = E, n[30] = w, n[31] = A, n[32] = u, n[33] = l, n[34] = o, n[35] = s, n[36] = d, n[37] = N, n[38] = P, n[39] = F, n[40] = I, n[41] = We, n[42] = G) : G = n[42];
	let K;
	n[43] === y.data ? K = n[44] : (K = y.data ?? [], n[43] = y.data, n[44] = K);
	let q;
	n[45] !== r || n[46] !== K ? (q = /* @__PURE__ */ jsx(DialogContent, {
		className: "max-w-2xl",
		children: /* @__PURE__ */ jsx(r, {
			types: K,
			onSuccess: Ve
		})
	}), n[45] = r, n[46] = K, n[47] = q) : q = n[47];
	let J;
	n[48] !== m || n[49] !== q ? (J = /* @__PURE__ */ jsx(Dialog, {
		open: m,
		onOpenChange: h,
		children: q
	}), n[48] = m, n[49] = q, n[50] = J) : J = n[50];
	let Ge = !!g, Y;
	n[51] === Symbol.for("react.memo_cache_sentinel") ? (Y = (e) => !e && Ue(), n[51] = Y) : Y = n[51];
	let X;
	n[52] !== i || n[53] !== y.data || n[54] !== g ? (X = g && /* @__PURE__ */ jsx(i, {
		types: y.data ?? [],
		workspace: g,
		onSuccess: Ue
	}), n[52] = i, n[53] = y.data, n[54] = g, n[55] = X) : X = n[55];
	let Z;
	n[56] === X ? Z = n[57] : (Z = /* @__PURE__ */ jsx(DialogContent, {
		className: "max-w-2xl",
		children: X
	}), n[56] = X, n[57] = Z);
	let Q;
	n[58] !== Ge || n[59] !== Z ? (Q = /* @__PURE__ */ jsx(Dialog, {
		open: Ge,
		onOpenChange: Y,
		children: Z
	}), n[58] = Ge, n[59] = Z, n[60] = Q) : Q = n[60];
	let $;
	return n[61] !== G || n[62] !== J || n[63] !== Q ? ($ = /* @__PURE__ */ jsxs(Fragment$1, { children: [
		G,
		J,
		Q
	] }), n[61] = G, n[62] = J, n[63] = Q, n[64] = $) : $ = n[64], $;
};
function _temp(e) {
	return e.slice(0, 6);
}
function _temp2(e) {
	return /* @__PURE__ */ jsxs(Badge, {
		variant: "secondary",
		children: [e.providerAppName ?? e.remoteEnvironmentId.slice(0, 8), e.type && /* @__PURE__ */ jsxs("span", {
			className: "text-muted-foreground ml-1",
			children: [
				"(",
				e.type,
				")"
			]
		})]
	}, e.remoteEnvironmentId);
}
function _temp3(e) {
	let t = e;
	return !t || t.length === 0 ? "—" : /* @__PURE__ */ jsx("div", {
		className: "flex flex-wrap gap-1",
		children: t.map(_temp2)
	});
}
function _temp4(e) {
	return new Date(e).toLocaleDateString();
}
function _temp5(e) {
	return !e.isFavourite;
}
function _temp6(e) {
	return !!e.isFavourite;
}
export { Workspaces_default as default };
