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