import { Button as e } from "../../../components/ui/button.js";
import { Input as t } from "../../../components/ui/input.js";
import { Label as n } from "../../../components/ui/label.js";
import { useCreateSecret as r, useDeleteSecret as i, useUpdateSecret as a, useWorkspaceSecrets as o } from "../../../hooks/useSecrets.js";
import { useWorkbenchLayout as s } from "../providers/WorkbenchLayoutProvider.js";
import { useState as c } from "react";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
import { KeyRound as d, Loader2 as f, Plus as p, Trash2 as m } from "lucide-react";
//#region src/features/workbench/components/WorkbenchSecretsPanel.tsx
function h() {
	let { workflow: h } = s(), g = h?.workspaceId, { data: _, isLoading: v } = o(g), y = r(), b = a(), x = i(), [S, C] = c(""), [w, T] = c(""), [E, D] = c(null), [O, k] = c(""), A = async () => {
		!g || !S.trim() || !w.trim() || (await y.mutateAsync({
			workspaceId: g,
			key: S.trim(),
			value: w
		}), C(""), T(""));
	}, j = async (e) => {
		!g || !O || (await b.mutateAsync({
			workspaceId: g,
			id: e,
			value: O
		}), D(null), k(""));
	}, M = async (e) => {
		g && await x.mutateAsync({
			workspaceId: g,
			id: e
		});
	};
	return /* @__PURE__ */ u("div", {
		className: "flex h-full flex-col",
		children: [
			/* @__PURE__ */ u("div", {
				className: "border-b px-4 py-3",
				children: [/* @__PURE__ */ u("div", {
					className: "flex items-center gap-2",
					children: [/* @__PURE__ */ l(d, { className: "h-4 w-4" }), /* @__PURE__ */ l("h3", {
						className: "text-sm font-medium",
						children: "Secrets"
					})]
				}), /* @__PURE__ */ l("p", {
					className: "text-muted-foreground mt-1 text-xs",
					children: "Manage environment variables for this workspace."
				})]
			}),
			/* @__PURE__ */ l("div", {
				className: "flex-1 overflow-y-auto px-4 py-3",
				children: v ? /* @__PURE__ */ u("div", {
					className: "flex items-center gap-2 py-4",
					children: [/* @__PURE__ */ l(f, { className: "h-4 w-4 animate-spin" }), /* @__PURE__ */ l("span", {
						className: "text-muted-foreground text-sm",
						children: "Loading..."
					})]
				}) : /* @__PURE__ */ u("div", {
					className: "space-y-2",
					children: [_?.map((n) => /* @__PURE__ */ u("div", {
						className: "bg-muted/50 flex items-center gap-2 rounded-md px-3 py-2",
						children: [/* @__PURE__ */ u("div", {
							className: "flex-1",
							children: [/* @__PURE__ */ l("div", {
								className: "font-mono text-sm",
								children: n.key
							}), /* @__PURE__ */ l("div", {
								className: "text-muted-foreground text-xs",
								children: n.hasValue ? "Value set" : "No value"
							})]
						}), E === n.id ? /* @__PURE__ */ u("div", {
							className: "flex items-center gap-1",
							children: [
								/* @__PURE__ */ l(t, {
									type: "password",
									value: O,
									onChange: (e) => k(e.target.value),
									placeholder: "New value",
									className: "h-7 w-32 text-xs"
								}),
								/* @__PURE__ */ l(e, {
									size: "sm",
									variant: "outline",
									className: "h-7 text-xs",
									onClick: () => void j(n.id),
									children: "Save"
								}),
								/* @__PURE__ */ l(e, {
									size: "sm",
									variant: "ghost",
									className: "h-7 text-xs",
									onClick: () => {
										D(null), k("");
									},
									children: "Cancel"
								})
							]
						}) : /* @__PURE__ */ u("div", {
							className: "flex items-center gap-1",
							children: [/* @__PURE__ */ l(e, {
								size: "sm",
								variant: "ghost",
								className: "h-7 text-xs",
								onClick: () => {
									D(n.id), k("");
								},
								children: "Update"
							}), /* @__PURE__ */ l(e, {
								size: "sm",
								variant: "ghost",
								className: "h-7",
								onClick: () => void M(n.id),
								children: /* @__PURE__ */ l(m, { className: "h-3 w-3" })
							})]
						})]
					}, n.id)), _?.length === 0 && /* @__PURE__ */ l("p", {
						className: "text-muted-foreground py-4 text-center text-sm",
						children: "No secrets yet."
					})]
				})
			}),
			/* @__PURE__ */ l("div", {
				className: "border-t px-4 py-3",
				children: /* @__PURE__ */ u("div", {
					className: "space-y-2",
					children: [
						/* @__PURE__ */ l(n, {
							className: "text-xs",
							children: "Add Secret"
						}),
						/* @__PURE__ */ l(t, {
							value: S,
							onChange: (e) => C(e.target.value),
							placeholder: "KEY_NAME",
							className: "font-mono text-sm"
						}),
						/* @__PURE__ */ l(t, {
							type: "password",
							value: w,
							onChange: (e) => T(e.target.value),
							placeholder: "Value",
							className: "text-sm"
						}),
						/* @__PURE__ */ u(e, {
							size: "sm",
							className: "w-full",
							disabled: !S.trim() || !w.trim() || y.isPending,
							onClick: () => void A(),
							children: [y.isPending ? /* @__PURE__ */ l(f, { className: "mr-1 h-3 w-3 animate-spin" }) : /* @__PURE__ */ l(p, { className: "mr-1 h-3 w-3" }), "Add Secret"]
						})
					]
				})
			})
		]
	});
}
//#endregion
export { h as WorkbenchSecretsPanel };
