import { $ as e, B as t, F as n, H as r, N as i, R as ee, S as a, U as o, _ as s, at as c, b as l, ct as te, d as u, g as ne, h as d, lt as f, m as p, o as re, p as m, s as ie, u as h, x as ae } from "./draggable-Bcb86AsV.js";
import { $ as oe, Jt as se, Mt as ce, Qt as g, Wt as le, at as ue, i as de, rt as fe } from "./features-BnG-dhzy.js";
import { A as _, y as pe } from "./icons-B_185qj_.js";
import { t as v } from "./blockTypeIcons-Bs9B-yft.js";
//#region src/cloud/components/ModuleBrowserModal.vue?vue&type=script&setup=true&lang.ts
var me = {
	role: "dialog",
	"aria-modal": "true",
	"aria-labelledby": "tpl-module-browser-title",
	class: "tpl-scale-in tpl:mx-4 tpl:flex tpl:w-full tpl:max-w-[1000px] tpl:flex-col tpl:rounded-[var(--tpl-radius-lg)]",
	style: {
		"background-color": "var(--tpl-bg-elevated)",
		"box-shadow": "var(--tpl-shadow-xl)",
		"max-height": "90vh"
	}
}, he = { class: "tpl:flex tpl:items-center tpl:justify-between tpl:border-b tpl:px-5 tpl:py-4 tpl:border-[var(--tpl-border)]" }, y = {
	id: "tpl-module-browser-title",
	class: "tpl:text-sm tpl:font-semibold tpl:text-[var(--tpl-text)]"
}, b = ["aria-label"], x = { class: "tpl:flex tpl:min-h-0 tpl:flex-1 tpl:overflow-hidden" }, S = { class: "tpl:flex tpl:w-[300px] tpl:shrink-0 tpl:flex-col tpl:overflow-hidden" }, C = { class: "tpl:px-4 tpl:pt-4 tpl:pb-3" }, w = { class: "tpl:relative" }, T = ["placeholder"], E = { class: "tpl:flex-1 tpl:overflow-y-auto tpl:px-4 tpl:pb-4" }, D = {
	key: 0,
	class: "tpl:flex tpl:flex-col tpl:gap-1"
}, O = ["aria-pressed", "onClick"], k = { class: "tpl:flex tpl:items-center tpl:gap-2" }, A = { class: "tpl:flex-1 tpl:truncate tpl:text-xs tpl:font-semibold tpl:text-[var(--tpl-text)]" }, j = { class: "tpl:shrink-0 tpl:rounded-full tpl:px-1.5 tpl:py-0.5 tpl:text-[10px] tpl:font-medium tpl:bg-[var(--tpl-bg-hover)] tpl:text-[var(--tpl-text-muted)]" }, M = { class: "tpl:mt-1 tpl:flex tpl:items-center tpl:gap-1" }, N = {
	key: 0,
	class: "tpl:text-[10px] tpl:text-[var(--tpl-text-dim)]"
}, P = ["aria-label", "onClick"], F = [
	"aria-label",
	"title",
	"onClick"
], I = {
	key: 1,
	class: "tpl:flex tpl:flex-col tpl:items-center tpl:justify-center tpl:py-12"
}, L = { class: "tpl:mt-2 tpl:text-xs tpl:text-[var(--tpl-text-dim)]" }, R = { class: "tpl:flex tpl:flex-1 tpl:flex-col tpl:overflow-hidden tpl:border-l tpl:border-[var(--tpl-border)]" }, ge = {
	key: 0,
	class: "tpl:flex tpl:flex-1 tpl:flex-col tpl:overflow-hidden"
}, z = { class: "tpl:flex-1 tpl:overflow-y-auto tpl:p-4" }, _e = {
	key: 1,
	class: "tpl:flex tpl:flex-1 tpl:flex-col tpl:items-center tpl:justify-center tpl:px-4"
}, ve = { class: "tpl:mt-2 tpl:text-center tpl:text-xs tpl:text-[var(--tpl-text-dim)]" }, ye = { class: "tpl:flex tpl:items-center tpl:justify-between tpl:border-t tpl:px-5 tpl:py-3 tpl:border-[var(--tpl-border)]" }, be = { class: "tpl:flex tpl:items-center tpl:gap-2" }, xe = { class: "tpl:shrink-0 tpl:text-xs tpl:text-[var(--tpl-text-dim)]" }, Se = ["value"], Ce = { class: "tpl:flex tpl:gap-2" }, we = ["disabled"], B = /* @__PURE__ */ a({
	__name: "ModuleBrowserModal",
	props: { visible: { type: Boolean } },
	emits: ["close", "insert"],
	setup(a, { emit: B }) {
		let Te = a, V = B, Ee = ae(() => import("./ModulePreviewCanvas-VHhQwuFP.js")), { t: De } = ce(), { t: H } = oe(), U = g(se, "ModuleBrowserModal"), W = g(le, "ModuleBrowserModal"), G = e(""), K = e(null), q = e(null), J = e("end"), Y = m(() => {
			let e = U.modules.value;
			if (!G.value) return e;
			let t = G.value.toLowerCase();
			return e.filter((e) => e.name.toLowerCase().includes(t));
		}), X = m(() => K.value ? U.modules.value.find((e) => e.id === K.value) ?? null : null), Oe = m(() => {
			let e = [{
				value: "beginning",
				label: H.modules.insertAtBeginning
			}], t = W.content.value.blocks;
			for (let n = 0; n < t.length; n++) {
				let r = t[n];
				e.push({
					value: r.id,
					label: H.modules.insertAfterBlock.replace("{block}", `${De.blocks[r.type] ?? r.type} ${n + 1}`)
				});
			}
			return e.push({
				value: "end",
				label: H.modules.insertAtEnd
			}), e;
		}), ke = m(() => {
			if (J.value === "end") return;
			if (J.value === "beginning") return 0;
			let e = W.content.value.blocks.findIndex((e) => e.id === J.value);
			if (e !== -1) return e + 1;
		});
		t(() => Te.visible, (e) => {
			if (e) {
				G.value = "", K.value = null, q.value = null;
				let e = W.state.selectedBlockId;
				e ? J.value = W.content.value.blocks.findIndex((t) => t.id === e) === -1 ? "end" : e : J.value = "end";
			}
		});
		function Ae(e) {
			let t = [], n = /* @__PURE__ */ new Set();
			for (let r of e.content) if (!n.has(r.type) && v[r.type] && (n.add(r.type), t.push({
				type: r.type,
				icon: v[r.type]
			})), t.length >= 5) break;
			return t;
		}
		function Z(e) {
			let t = new Set(e.content.map((e) => e.type));
			return Math.max(0, t.size - 5);
		}
		async function je(e) {
			try {
				await U.deleteModule(e), K.value === e && (K.value = null);
			} finally {
				q.value = null;
			}
		}
		function Q() {
			X.value && V("insert", X.value, ke.value);
		}
		function $() {
			V("close");
		}
		function Me(e) {
			e.key === "Escape" && $(), e.key === "Enter" && X.value && (e.preventDefault(), Q());
		}
		return (e, t) => (i(), d(de, {
			visible: a.visible,
			onClose: $,
			onKeydown: Me
		}, {
			default: r(() => [p("div", me, [
				p("div", he, [p("h3", y, f(c(H).modules.browse), 1), p("button", {
					"aria-label": c(H).modules.close,
					class: "tpl:cursor-pointer tpl:rounded-md tpl:border-none tpl:bg-transparent tpl:p-1 tpl:transition-colors tpl:duration-100 tpl:text-[var(--tpl-text-dim)]",
					onClick: $
				}, [l(c(fe), {
					size: 16,
					"stroke-width": 2
				})], 8, b)]),
				p("div", x, [p("div", S, [p("div", C, [p("div", w, [l(c(pe), {
					size: 14,
					"stroke-width": 2,
					class: "tpl:pointer-events-none tpl:absolute tpl:left-3 tpl:top-1/2 tpl:-translate-y-1/2 tpl:text-[var(--tpl-text-dim)]"
				}), o(p("input", {
					"onUpdate:modelValue": t[0] ||= (e) => G.value = e,
					type: "text",
					placeholder: c(H).modules.search,
					class: "tpl:h-9 tpl:w-full tpl:rounded-md tpl:border tpl:pl-9 tpl:pr-3 tpl:text-sm tpl:outline-none tpl:border-[var(--tpl-border)] tpl:bg-[var(--tpl-bg)] tpl:text-[var(--tpl-text)]"
				}, null, 8, T), [[ie, G.value]])])]), p("div", E, [Y.value.length > 0 ? (i(), s("div", D, [(i(!0), s(u, null, n(Y.value, (e) => (i(), s("button", {
					key: e.id,
					type: "button",
					"aria-pressed": K.value === e.id,
					class: "tpl:group/card tpl:w-full tpl:cursor-pointer tpl:rounded-[var(--tpl-radius-md)] tpl:border tpl:bg-transparent tpl:px-3 tpl:py-2 tpl:text-left tpl:transition-all tpl:duration-[120ms]",
					style: te({
						borderColor: K.value === e.id ? "var(--tpl-primary)" : "var(--tpl-border)",
						backgroundColor: K.value === e.id ? "var(--tpl-primary-light)" : "transparent"
					}),
					onClick: (t) => K.value = e.id
				}, [p("div", k, [p("span", A, f(e.name), 1), p("span", j, f(c(H).modules.blockCount.replace("{count}", String(e.content.length))), 1)]), p("div", M, [
					(i(!0), s(u, null, n(Ae(e), (e) => (i(), d(ee(e.icon), {
						key: e.type,
						size: 14,
						"stroke-width": 1.5,
						class: "tpl:text-[var(--tpl-text-dim)]"
					}))), 128)),
					Z(e) > 0 ? (i(), s("span", N, " +" + f(Z(e)), 1)) : ne("", !0),
					q.value === e.id ? (i(), s("button", {
						key: 1,
						"aria-label": c(H).modules.deleteConfirm,
						class: "tpl:ml-auto tpl:cursor-pointer tpl:rounded-md tpl:border tpl:px-2 tpl:py-0.5 tpl:text-[10px] tpl:font-medium tpl:transition-colors tpl:duration-100 tpl:border-[var(--tpl-danger)] tpl:text-[var(--tpl-danger)]",
						style: { "background-color": "transparent" },
						onClick: h((t) => je(e.id), ["stop"])
					}, f(c(H).modules.deleteConfirm), 9, P)) : (i(), s("button", {
						key: 2,
						class: "tpl-module-delete-btn tpl:ml-auto tpl:cursor-pointer tpl:rounded-md tpl:border-none tpl:bg-transparent tpl:p-0.5 tpl:transition-colors tpl:duration-100 tpl:text-[var(--tpl-text-dim)]",
						"aria-label": c(H).modules.delete,
						title: c(H).modules.delete,
						onClick: h((t) => q.value = e.id, ["stop"])
					}, [l(c(ue), {
						size: 12,
						"stroke-width": 1.5
					})], 8, F))
				])], 12, O))), 128))])) : (i(), s("div", I, [l(c(_), {
					size: 32,
					"stroke-width": 1,
					class: "tpl:text-[var(--tpl-text-dim)]"
				}), p("p", L, f(G.value ? c(H).modules.noModules : c(H).modules.noModulesHint), 1)]))])]), p("div", R, [X.value ? (i(), s("div", ge, [p("div", z, [l(c(Ee), { blocks: X.value.content }, null, 8, ["blocks"])])])) : (i(), s("div", _e, [l(c(_), {
					size: 32,
					"stroke-width": 1,
					class: "tpl:text-[var(--tpl-text-dim)]"
				}), p("p", ve, f(c(H).modules.selectToPreview), 1)]))])]),
				p("div", ye, [p("div", be, [p("label", xe, f(c(H).modules.insertPosition), 1), o(p("select", {
					"onUpdate:modelValue": t[1] ||= (e) => J.value = e,
					class: "tpl:h-7 tpl:max-w-[220px] tpl:rounded-md tpl:border tpl:px-2 tpl:text-xs tpl:outline-none tpl:border-[var(--tpl-border)] tpl:bg-[var(--tpl-bg)] tpl:text-[var(--tpl-text)]"
				}, [(i(!0), s(u, null, n(Oe.value, (e) => (i(), s("option", {
					key: e.value,
					value: e.value
				}, f(e.label), 9, Se))), 128))], 512), [[re, J.value]])]), p("div", Ce, [p("button", {
					type: "button",
					class: "tpl:cursor-pointer tpl:rounded-md tpl:border tpl:px-3 tpl:py-1.5 tpl:text-sm tpl:font-medium tpl:shadow-xs tpl:transition-all tpl:duration-150 tpl:border-[var(--tpl-border)] tpl:text-[var(--tpl-text)] tpl:bg-[var(--tpl-bg)]",
					onClick: $
				}, f(c(H).modules.close), 1), p("button", {
					type: "button",
					class: "tpl:cursor-pointer tpl:rounded-md tpl:px-3 tpl:py-1.5 tpl:text-sm tpl:font-medium tpl:shadow-xs tpl:transition-all tpl:duration-150 tpl:hover:opacity-90 tpl:disabled:cursor-not-allowed tpl:disabled:opacity-50 tpl:bg-[var(--tpl-primary)] tpl:text-[var(--tpl-bg)]",
					disabled: !X.value,
					onClick: Q
				}, f(c(H).modules.insert), 9, we)])])
			])]),
			_: 1
		}, 8, ["visible"]));
	}
});
//#endregion
export { B as default };

//# sourceMappingURL=ModuleBrowserModal-B70ZM4KG.js.map