import { useAvailableEnvironments as e } from "../../../hooks/useConfig.js";
import { useCreateWorkspace as t, useUpdateWorkspace as n } from "../../../hooks/useWorkspaces.js";
import { Button as r } from "../../../components/ui/button.js";
import { DialogHeader as i } from "../../../components/ui/dialog.js";
import { Input as a } from "../../../components/ui/input.js";
import { Label as o } from "../../../components/ui/label.js";
import { Select as s, SelectContent as c, SelectItem as l, SelectTrigger as u, SelectValue as d } from "../../../components/ui/select.js";
import f from "../../../components/feedback/ErrorSnackbar.js";
import { EnvironmentSlotSelector as p } from "./EnvironmentSlotSelector.js";
import { useCallback as m, useEffect as h, useMemo as g, useState as _ } from "react";
import { jsx as v, jsxs as y } from "react/jsx-runtime";
import { Loader2 as b, Star as x } from "lucide-react";
import { DialogTitle as S } from "@radix-ui/react-dialog";
//#region src/features/workspaces/components/CreateWorkspace.tsx
var C = ({ types: C, workspace: w, onSuccess: T }) => {
	let E = t(), D = n(), [O, k] = _(C[0]?.className ?? ""), [A, j] = _(w?.isFavourite ?? !1), [M, N] = _({});
	h(() => {
		k(C[0]?.className ?? "");
	}, [C]);
	let P = g(() => C.find((e) => e.className === O), [C, O])?.environments ?? [], { data: F } = e({ enabled: P.length > 0 }), I = g(() => F?.map((e) => ({
		...e,
		id: e.type
	})) ?? [], [F]), L = I.length > 0 && P.length > 0;
	h(() => {
		if (I.length === 0 || P.length === 0) return;
		let e = {};
		for (let t of P) {
			if (w?.environments) {
				let n = w.environments.find((e) => e.slotId === t.id);
				if (n) {
					e[t.id] = n.remoteEnvironmentId;
					continue;
				}
			}
			let n = I.find((e) => !t.type || e.type === t.type);
			n && (e[t.id] = n.id);
		}
		N(e);
	}, [
		I,
		P,
		w?.environments
	]);
	let R = (e) => {
		k(e), N({});
	}, z = m((e, t) => {
		N((n) => ({
			...n,
			[e]: t
		}));
	}, []), B = m(() => {
		if (!L) return;
		let e = [];
		for (let t of P) {
			let n = M[t.id];
			if (n && n !== "__none__") {
				let r = I.find((e) => e.id === n), i = F?.find((e) => e.type === n);
				r && i && e.push({
					slotId: t.id,
					type: r.type,
					remoteEnvironmentId: r.id,
					envName: r.name,
					connectionUrl: i.connectionUrl,
					agentUrl: i.agentUrl,
					workerUrl: i.connectionUrl,
					local: i.local
				});
			}
		}
		return e.length > 0 ? e : void 0;
	}, [
		L,
		F,
		I,
		P,
		M
	]), V = (e) => {
		e.preventDefault();
		let t = new FormData(e.currentTarget).get("name");
		!t || !w || D.mutate({
			id: w.id,
			workspaceUpdateDto: {
				title: t,
				isFavourite: A,
				environments: B()
			}
		}, { onSuccess: () => {
			T();
		} });
	}, H = (e) => {
		e.preventDefault();
		let t = new FormData(e.currentTarget).get("name");
		O && E.mutate({ workspaceCreateDto: {
			title: t || void 0,
			className: O,
			isFavourite: A || void 0,
			environments: B()
		} }, { onSuccess: () => {
			console.log("closing"), T();
		} });
	}, U = E.isPending || D.isPending;
	return /* @__PURE__ */ y("div", { children: [
		/* @__PURE__ */ v(f, { error: E.error }),
		/* @__PURE__ */ v(f, { error: D.error }),
		/* @__PURE__ */ y("div", {
			className: "mb-4",
			children: [/* @__PURE__ */ v(i, {
				className: "space-y-1",
				children: /* @__PURE__ */ y(S, {
					className: "mb-4 text-lg leading-none font-semibold",
					children: [w ? "Edit" : "Add", " Workspace"]
				})
			}), /* @__PURE__ */ y("form", {
				onSubmit: w ? V : H,
				className: "space-y-4",
				children: [
					/* @__PURE__ */ y("div", {
						className: "space-y-2",
						children: [/* @__PURE__ */ v(o, {
							htmlFor: "name",
							children: "Workspace Name"
						}), /* @__PURE__ */ y("div", {
							className: "flex items-center gap-2",
							children: [/* @__PURE__ */ v(a, {
								id: "name",
								name: "name",
								defaultValue: w?.title ?? "",
								placeholder: "Enter workspace name (optional)",
								autoFocus: !0
							}), /* @__PURE__ */ v(r, {
								type: "button",
								variant: "ghost",
								size: "icon",
								className: "shrink-0",
								onClick: () => j((e) => !e),
								children: /* @__PURE__ */ v(x, { className: `h-4 w-4 ${A ? "fill-yellow-400 text-yellow-400" : "text-muted-foreground"}` })
							})]
						})]
					}),
					!w && C.length > 1 && /* @__PURE__ */ y("div", {
						className: "space-y-2",
						children: [/* @__PURE__ */ v(o, {
							htmlFor: "className",
							children: "Type"
						}), /* @__PURE__ */ y(s, {
							name: "className",
							value: O,
							onValueChange: R,
							children: [/* @__PURE__ */ v(u, {
								id: "className",
								className: "w-full",
								children: /* @__PURE__ */ v(d, { placeholder: "Select a type" })
							}), /* @__PURE__ */ v(c, { children: C.map((e) => /* @__PURE__ */ v(l, {
								value: e.className,
								children: e.title ?? e.className
							}, e.className)) })]
						})]
					}),
					L && P.map((e) => /* @__PURE__ */ v(p, {
						slot: e,
						environments: I,
						selectedEnvironmentId: M[e.id],
						onSelect: (t) => z(e.id, t)
					}, e.id)),
					/* @__PURE__ */ y(r, {
						type: "submit",
						className: "w-full",
						disabled: U,
						children: [U && /* @__PURE__ */ v(b, { className: "mr-2 h-4 w-4 animate-spin" }), w ? "Save" : "Create"]
					})
				]
			})]
		})
	] });
};
//#endregion
export { C as default };
