import { useCreateWorkspace, useUpdateWorkspace } from "../../../hooks/useWorkspaces.js";
import { Button } from "../../../components/ui/button.js";
import { DialogHeader } from "../../../components/ui/dialog.js";
import { Input } from "../../../components/ui/input.js";
import { Label } from "../../../components/ui/label.js";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../components/ui/select.js";
import ErrorSnackbar_default from "../../../components/snackbars/ErrorSnackbar.js";
import { c } from "react/compiler-runtime";
import { useEffect, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Loader2, Star } from "lucide-react";
import { DialogTitle } from "@radix-ui/react-dialog";
var CreateWorkspace_default = (h) => {
	let g = c(52), { types: _, workspace: v, onSuccess: y } = h, b = useCreateWorkspace(), x = useUpdateWorkspace(), [S, C] = useState(_[0]?.blockName ?? ""), [w, T] = useState(v?.isFavourite ?? !1), E;
	g[0] === _[0]?.blockName ? E = g[1] : (E = () => {
		C(_[0]?.blockName ?? "");
	}, g[0] = _[0]?.blockName, g[1] = E);
	let D;
	g[2] === _ ? D = g[3] : (D = [_], g[2] = _, g[3] = D), useEffect(E, D);
	let O;
	g[4] === Symbol.for("react.memo_cache_sentinel") ? (O = (t) => {
		C(t);
	}, g[4] = O) : O = g[4];
	let k = O, A;
	g[5] !== w || g[6] !== y || g[7] !== x || g[8] !== v ? (A = (t) => {
		t.preventDefault();
		let m = new FormData(t.currentTarget).get("name");
		!m || !v || x.mutate({
			id: v.id,
			workspaceUpdateDto: {
				title: m,
				isFavourite: w
			}
		}, { onSuccess: () => {
			y();
		} });
	}, g[5] = w, g[6] = y, g[7] = x, g[8] = v, g[9] = A) : A = g[9];
	let j = A, M;
	g[10] !== b || g[11] !== w || g[12] !== y || g[13] !== S ? (M = (t) => {
		t.preventDefault();
		let m = new FormData(t.currentTarget).get("name");
		S && b.mutate({ workspaceCreateDto: {
			title: m || void 0,
			blockName: S,
			isFavourite: w || void 0
		} }, { onSuccess: () => {
			console.log("closing"), y();
		} });
	}, g[10] = b, g[11] = w, g[12] = y, g[13] = S, g[14] = M) : M = g[14];
	let N = M, P = b.isPending || x.isPending, F;
	g[15] === b.error ? F = g[16] : (F = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: b.error }), g[15] = b.error, g[16] = F);
	let I;
	g[17] === x.error ? I = g[18] : (I = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: x.error }), g[17] = x.error, g[18] = I);
	let L = v ? "Edit" : "Add", R;
	g[19] === L ? R = g[20] : (R = /* @__PURE__ */ jsx(DialogHeader, {
		className: "space-y-1",
		children: /* @__PURE__ */ jsxs(DialogTitle, {
			className: "mb-4 text-lg leading-none font-semibold",
			children: [L, " Workspace"]
		})
	}), g[19] = L, g[20] = R);
	let z = v ? j : N, B;
	g[21] === Symbol.for("react.memo_cache_sentinel") ? (B = /* @__PURE__ */ jsx(Label, {
		htmlFor: "name",
		children: "Workspace Name"
	}), g[21] = B) : B = g[21];
	let V = v?.title ?? "", H;
	g[22] === V ? H = g[23] : (H = /* @__PURE__ */ jsx(Input, {
		id: "name",
		name: "name",
		defaultValue: V,
		placeholder: "Enter workspace name (optional)",
		autoFocus: !0
	}), g[22] = V, g[23] = H);
	let U;
	g[24] === Symbol.for("react.memo_cache_sentinel") ? (U = () => T(_temp), g[24] = U) : U = g[24];
	let W = `h-4 w-4 ${w ? "fill-yellow-400 text-yellow-400" : "text-muted-foreground"}`, G;
	g[25] === W ? G = g[26] : (G = /* @__PURE__ */ jsx(Button, {
		type: "button",
		variant: "ghost",
		size: "icon",
		className: "shrink-0",
		onClick: U,
		children: /* @__PURE__ */ jsx(Star, { className: W })
	}), g[25] = W, g[26] = G);
	let K;
	g[27] !== H || g[28] !== G ? (K = /* @__PURE__ */ jsxs("div", {
		className: "space-y-2",
		children: [B, /* @__PURE__ */ jsxs("div", {
			className: "flex items-center gap-2",
			children: [H, G]
		})]
	}), g[27] = H, g[28] = G, g[29] = K) : K = g[29];
	let q;
	g[30] !== _ || g[31] !== v || g[32] !== S ? (q = !v && _.length > 1 && /* @__PURE__ */ jsxs("div", {
		className: "space-y-2",
		children: [/* @__PURE__ */ jsx(Label, {
			htmlFor: "blockName",
			children: "Type"
		}), /* @__PURE__ */ jsxs(Select, {
			name: "blockName",
			value: S,
			onValueChange: k,
			children: [/* @__PURE__ */ jsx(SelectTrigger, {
				id: "blockName",
				className: "w-full",
				children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Select a type" })
			}), /* @__PURE__ */ jsx(SelectContent, { children: _.map(_temp2) })]
		})]
	}), g[30] = _, g[31] = v, g[32] = S, g[33] = q) : q = g[33];
	let J;
	g[34] === P ? J = g[35] : (J = P && /* @__PURE__ */ jsx(Loader2, { className: "mr-2 h-4 w-4 animate-spin" }), g[34] = P, g[35] = J);
	let Y = v ? "Save" : "Create", X;
	g[36] !== P || g[37] !== J || g[38] !== Y ? (X = /* @__PURE__ */ jsxs(Button, {
		type: "submit",
		className: "w-full",
		disabled: P,
		children: [J, Y]
	}), g[36] = P, g[37] = J, g[38] = Y, g[39] = X) : X = g[39];
	let Z;
	g[40] !== z || g[41] !== K || g[42] !== q || g[43] !== X ? (Z = /* @__PURE__ */ jsxs("form", {
		onSubmit: z,
		className: "space-y-4",
		children: [
			K,
			q,
			X
		]
	}), g[40] = z, g[41] = K, g[42] = q, g[43] = X, g[44] = Z) : Z = g[44];
	let Q;
	g[45] !== Z || g[46] !== R ? (Q = /* @__PURE__ */ jsxs("div", {
		className: "mb-4",
		children: [R, Z]
	}), g[45] = Z, g[46] = R, g[47] = Q) : Q = g[47];
	let $;
	return g[48] !== Q || g[49] !== F || g[50] !== I ? ($ = /* @__PURE__ */ jsxs("div", { children: [
		F,
		I,
		Q
	] }), g[48] = Q, g[49] = F, g[50] = I, g[51] = $) : $ = g[51], $;
};
function _temp(t) {
	return !t;
}
function _temp2(t) {
	return /* @__PURE__ */ jsx(SelectItem, {
		value: t.blockName,
		children: t.title ?? t.blockName
	}, t.blockName);
}
export { CreateWorkspace_default as default };
