import { A as e, B as t, F as n, L as r, M as i, X as a, a as ee, b as o, c as s, d as c, f as l, h as u, i as te, l as d, m as ne, ot as re, p as f, rt as p, st as m, v as h, y as ie, z as ae } from "./vue.runtime.esm-bundler-Bxqkjqhc.js";
import "./useEditorCore-DT3VrvZS.js";
import { A as g, p as _, w as oe } from "./keys-CY3tkCsv.js";
import { t as se } from "./useI18n-CWX9ZTCW.js";
import { t as v } from "./createLucideIcon-Di4mqmGn.js";
import { n as y, t as b } from "./blockTypeIcons-CJNuGjLX.js";
import { t as ce } from "./trash-2-424iqbpN.js";
import { t as le } from "./x-Dlaenqta.js";
import { n as ue } from "./useCloudI18n-BPUHj2CZ.js";
import { t as de } from "./TplModal-CVq2BAIW.js";
var fe = v("search", [["path", {
	d: "m21 21-4.34-4.34",
	key: "14j7rj"
}], ["circle", {
	cx: "11",
	cy: "11",
	r: "8",
	key: "4ej97u"
}]]), pe = {
	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"
	}
}, me = { class: "tpl:flex tpl:items-center tpl:justify-between tpl:border-b tpl:px-5 tpl:py-4 tpl:border-[var(--tpl-border)]" }, x = {
	id: "tpl-module-browser-title",
	class: "tpl:text-sm tpl:font-semibold tpl:text-[var(--tpl-text)]"
}, S = ["aria-label"], C = { class: "tpl:flex tpl:min-h-0 tpl:flex-1 tpl:overflow-hidden" }, w = { class: "tpl:flex tpl:w-[300px] tpl:shrink-0 tpl:flex-col tpl:overflow-hidden" }, T = { class: "tpl:px-4 tpl:pt-4 tpl:pb-3" }, E = { class: "tpl:relative" }, D = ["placeholder"], O = { class: "tpl:flex-1 tpl:overflow-y-auto tpl:px-4 tpl:pb-4" }, k = {
	key: 0,
	class: "tpl:flex tpl:flex-col tpl:gap-1"
}, A = ["aria-pressed", "onClick"], j = { class: "tpl:flex tpl:items-center tpl:gap-2" }, M = { class: "tpl:flex-1 tpl:truncate tpl:text-xs tpl:font-semibold tpl:text-[var(--tpl-text)]" }, N = { 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)]" }, P = { class: "tpl:mt-1 tpl:flex tpl:items-center tpl:gap-1" }, F = {
	key: 0,
	class: "tpl:text-[10px] tpl:text-[var(--tpl-text-dim)]"
}, I = ["aria-label", "onClick"], L = [
	"aria-label",
	"title",
	"onClick"
], R = {
	key: 1,
	class: "tpl:flex tpl:flex-col tpl:items-center tpl:justify-center tpl:py-12"
}, z = { class: "tpl:mt-2 tpl:text-xs tpl:text-[var(--tpl-text-dim)]" }, he = { 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"
}, _e = { class: "tpl:flex-1 tpl:overflow-y-auto tpl:p-4" }, ve = {
	key: 1,
	class: "tpl:flex tpl:flex-1 tpl:flex-col tpl:items-center tpl:justify-center tpl:px-4"
}, ye = { class: "tpl:mt-2 tpl:text-center tpl:text-xs tpl:text-[var(--tpl-text-dim)]" }, be = { class: "tpl:flex tpl:items-center tpl:justify-between tpl:border-t tpl:px-5 tpl:py-3 tpl:border-[var(--tpl-border)]" }, xe = { class: "tpl:flex tpl:items-center tpl:gap-2" }, Se = { class: "tpl:shrink-0 tpl:text-xs tpl:text-[var(--tpl-text-dim)]" }, Ce = ["value"], we = { class: "tpl:flex tpl:gap-2" }, Te = ["disabled"], B = /* @__PURE__ */ o({
	__name: "ModuleBrowserModal",
	props: { visible: { type: Boolean } },
	emits: ["close", "insert"],
	setup(o, { emit: v }) {
		let B = o, V = v, Ee = ie(() => import("./ModulePreviewCanvas-CDg4MnJP.js")), { t: De } = se(), { t: H } = ue(), U = g(oe, "ModuleBrowserModal"), W = g(_, "ModuleBrowserModal"), G = a(""), K = a(null), q = a(null), J = a("end"), Y = c(() => {
			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 = c(() => K.value ? U.modules.value.find((e) => e.id === K.value) ?? null : null), Oe = c(() => {
			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], i = r.type, a = De.blocks[i] ?? r.type;
				e.push({
					value: r.id,
					label: H.modules.insertAfterBlock.replace("{block}", `${a} ${n + 1}`)
				});
			}
			return e.push({
				value: "end",
				label: H.modules.insertAtEnd
			}), e;
		}), ke = c(() => {
			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;
		});
		r(() => B.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) && b[r.type] && (n.add(r.type), t.push({
				type: r.type,
				icon: b[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 (r, a) => (e(), f(de, {
			visible: o.visible,
			onClose: $,
			onKeydown: Me
		}, {
			default: ae(() => [l("div", pe, [
				l("div", me, [l("h3", x, m(p(H).modules.browse), 1), l("button", {
					"aria-label": p(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(p(le), {
					size: 16,
					"stroke-width": 2
				})], 8, S)]),
				l("div", C, [l("div", w, [l("div", T, [l("div", E, [h(p(fe), {
					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)]"
				}), t(l("input", {
					"onUpdate:modelValue": a[0] ||= (e) => G.value = e,
					type: "text",
					placeholder: p(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, D), [[ee, G.value]])])]), l("div", O, [Y.value.length > 0 ? (e(), u("div", k, [(e(!0), u(d, null, i(Y.value, (t) => (e(), u("button", {
					key: t.id,
					type: "button",
					"aria-pressed": K.value === t.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: re({
						borderColor: K.value === t.id ? "var(--tpl-primary)" : "var(--tpl-border)",
						backgroundColor: K.value === t.id ? "var(--tpl-primary-light)" : "transparent"
					}),
					onClick: (e) => K.value = t.id
				}, [l("div", j, [l("span", M, m(t.name), 1), l("span", N, m(p(H).modules.blockCount.replace("{count}", String(t.content.length))), 1)]), l("div", P, [
					(e(!0), u(d, null, i(Ae(t), (t) => (e(), f(n(t.icon), {
						key: t.type,
						size: 14,
						"stroke-width": 1.5,
						class: "tpl:text-[var(--tpl-text-dim)]"
					}))), 128)),
					Z(t) > 0 ? (e(), u("span", F, " +" + m(Z(t)), 1)) : ne("", !0),
					q.value === t.id ? (e(), u("button", {
						key: 1,
						"aria-label": p(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: s((e) => je(t.id), ["stop"])
					}, m(p(H).modules.deleteConfirm), 9, I)) : (e(), u("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": p(H).modules.delete,
						title: p(H).modules.delete,
						onClick: s((e) => q.value = t.id, ["stop"])
					}, [h(p(ce), {
						size: 12,
						"stroke-width": 1.5
					})], 8, L))
				])], 12, A))), 128))])) : (e(), u("div", R, [h(p(y), {
					size: 32,
					"stroke-width": 1,
					class: "tpl:text-[var(--tpl-text-dim)]"
				}), l("p", z, m(G.value ? p(H).modules.noModules : p(H).modules.noModulesHint), 1)]))])]), l("div", he, [X.value ? (e(), u("div", ge, [l("div", _e, [h(p(Ee), { blocks: X.value.content }, null, 8, ["blocks"])])])) : (e(), u("div", ve, [h(p(y), {
					size: 32,
					"stroke-width": 1,
					class: "tpl:text-[var(--tpl-text-dim)]"
				}), l("p", ye, m(p(H).modules.selectToPreview), 1)]))])]),
				l("div", be, [l("div", xe, [l("label", Se, m(p(H).modules.insertPosition), 1), t(l("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)]"
				}, [(e(!0), u(d, null, i(Oe.value, (t) => (e(), u("option", {
					key: t.value,
					value: t.value
				}, m(t.label), 9, Ce))), 128))], 512), [[te, J.value]])]), l("div", we, [l("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: $
				}, m(p(H).modules.close), 1), l("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
				}, m(p(H).modules.insert), 9, Te)])])
			])]),
			_: 1
		}, 8, ["visible"]));
	}
});
//#endregion
export { B as default };
