import { H as e, L as t, M as n, P as r, V as ee, Z as i, a as te, b as a, ct as o, f as s, g as c, h as ne, it as l, l as u, m as d, o as re, p as f, st as ie, u as p, x as m, y as h, z as ae } from "./draggable-BwWMFq33.js";
import { Ft as oe, Zt as se, at as ce, ct as le, i as ue, nn as g, qt as de, tt as fe } from "./features-CUYy5kZQ.js";
import { b as pe, j as _ } from "./icons-C4zY5-I3.js";
import { t as v } from "./blockTypeIcons-eVGGIdne.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__ */ m({
	__name: "ModuleBrowserModal",
	props: { visible: { type: Boolean } },
	emits: ["close", "insert"],
	setup(m, { emit: B }) {
		let Te = m, V = B, Ee = a(() => import("./ModulePreviewCanvas-BkcPcF5C.js")), { t: De } = oe(), { t: H } = fe(), U = g(se, "ModuleBrowserModal"), W = g(de, "ModuleBrowserModal"), G = i(""), K = i(null), q = i(null), J = i("end"), Y = s(() => {
			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 = s(() => K.value ? U.modules.value.find((e) => e.id === K.value) ?? null : null), Oe = s(() => {
			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 = s(() => {
			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;
		});
		ae(() => 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 (i, a) => (n(), d(ue, {
			visible: m.visible,
			onClose: $,
			onKeydown: Me
		}, {
			default: ee(() => [f("div", me, [
				f("div", he, [f("h3", y, o(l(H).modules.browse), 1), f("button", {
					"aria-label": l(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: $
				}, [h(l(ce), {
					size: 16,
					"stroke-width": 2
				})], 8, b)]),
				f("div", x, [f("div", S, [f("div", C, [f("div", w, [h(l(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)]"
				}), e(f("input", {
					"onUpdate:modelValue": a[0] ||= (e) => G.value = e,
					type: "text",
					placeholder: l(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), [[re, G.value]])])]), f("div", E, [Y.value.length > 0 ? (n(), c("div", D, [(n(!0), c(p, null, r(Y.value, (e) => (n(), c("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: ie({
						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
				}, [f("div", k, [f("span", A, o(e.name), 1), f("span", j, o(l(H).modules.blockCount.replace("{count}", String(e.content.length))), 1)]), f("div", M, [
					(n(!0), c(p, null, r(Ae(e), (e) => (n(), d(t(e.icon), {
						key: e.type,
						size: 14,
						"stroke-width": 1.5,
						class: "tpl:text-[var(--tpl-text-dim)]"
					}))), 128)),
					Z(e) > 0 ? (n(), c("span", N, " +" + o(Z(e)), 1)) : ne("", !0),
					q.value === e.id ? (n(), c("button", {
						key: 1,
						"aria-label": l(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: u((t) => je(e.id), ["stop"])
					}, o(l(H).modules.deleteConfirm), 9, P)) : (n(), c("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": l(H).modules.delete,
						title: l(H).modules.delete,
						onClick: u((t) => q.value = e.id, ["stop"])
					}, [h(l(le), {
						size: 12,
						"stroke-width": 1.5
					})], 8, F))
				])], 12, O))), 128))])) : (n(), c("div", I, [h(l(_), {
					size: 32,
					"stroke-width": 1,
					class: "tpl:text-[var(--tpl-text-dim)]"
				}), f("p", L, o(G.value ? l(H).modules.noModules : l(H).modules.noModulesHint), 1)]))])]), f("div", R, [X.value ? (n(), c("div", ge, [f("div", z, [h(l(Ee), { blocks: X.value.content }, null, 8, ["blocks"])])])) : (n(), c("div", _e, [h(l(_), {
					size: 32,
					"stroke-width": 1,
					class: "tpl:text-[var(--tpl-text-dim)]"
				}), f("p", ve, o(l(H).modules.selectToPreview), 1)]))])]),
				f("div", ye, [f("div", be, [f("label", xe, o(l(H).modules.insertPosition), 1), e(f("select", {
					"onUpdate:modelValue": a[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)]"
				}, [(n(!0), c(p, null, r(Oe.value, (e) => (n(), c("option", {
					key: e.value,
					value: e.value
				}, o(e.label), 9, Se))), 128))], 512), [[te, J.value]])]), f("div", Ce, [f("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: $
				}, o(l(H).modules.close), 1), f("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
				}, o(l(H).modules.insert), 9, we)])])
			])]),
			_: 1
		}, 8, ["visible"]));
	}
});
//#endregion
export { B as default };

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