import e from "./PlElementListItem.js";
import { moveElements as t } from "./utils.js";
import { Fragment as n, computed as r, createBlock as i, createCommentVNode as ee, createElementBlock as a, createElementVNode as te, createSlots as o, defineComponent as s, mergeModels as c, normalizeClass as l, openBlock as u, renderList as d, renderSlot as f, shallowRef as p, unref as m, useModel as h, useSlots as ne, watch as g, withCtx as _ } from "vue";
import { isFunction as v, shallowHash as y } from "@milaboratories/helpers";
import { useSortable as re } from "@vueuse/integrations/useSortable";
var b = /* @__PURE__ */ s({
	__name: "PlElementList",
	props: /* @__PURE__ */ c({
		getItemKey: {
			type: Function,
			default: (e) => JSON.stringify(e)
		},
		itemClass: {
			type: [
				String,
				Array,
				Function
			],
			default: void 0
		},
		itemClassTitle: {
			type: [
				String,
				Array,
				Function
			],
			default: void 0
		},
		itemClassBefore: {
			type: [
				String,
				Array,
				Function
			],
			default: void 0
		},
		itemClassAfter: {
			type: [
				String,
				Array,
				Function
			],
			default: void 0
		},
		itemClassContent: {
			type: [
				String,
				Array,
				Function
			],
			default: void 0
		},
		isActive: {
			type: Function,
			default: void 0
		},
		disableDragging: {
			type: Boolean,
			default: void 0
		},
		isDraggable: {
			type: Function,
			default: void 0
		},
		onDragEnd: {
			type: Function,
			default: void 0
		},
		onSort: {
			type: Function,
			default: void 0
		},
		disableRemoving: {
			type: Boolean,
			default: void 0
		},
		isRemovable: {
			type: Function,
			default: void 0
		},
		onRemove: {
			type: Function,
			default: void 0
		},
		disableExpanding: {
			type: Boolean,
			default: void 0
		},
		isExpandable: {
			type: Function,
			default: void 0
		},
		isExpanded: {
			type: Function,
			default: void 0
		},
		onExpand: {
			type: Function,
			default: void 0
		},
		disableToggling: {
			type: Boolean,
			default: void 0
		},
		isToggable: {
			type: Function,
			default: void 0
		},
		isToggled: {
			type: Function,
			default: void 0
		},
		onToggle: {
			type: Function,
			default: void 0
		},
		disablePinning: {
			type: Boolean,
			default: void 0
		},
		isPinnable: {
			type: Function,
			default: void 0
		},
		isPinned: {
			type: Function,
			default: void 0
		},
		onPin: {
			type: Function,
			default: void 0
		}
	}, {
		items: { required: !0 },
		itemsModifiers: {}
	}),
	emits: /* @__PURE__ */ c(["itemClick"], ["update:items"]),
	setup(s, { emit: c }) {
		let b = h(s, "items"), x = s, S = c, C = ne(), w = r(() => x.disableDragging !== !0), T = r(() => b.value.filter(V)), ie = r(() => T.value.length > 0), E = r(() => b.value.filter((e, t) => !V(e, t))), D = r(() => E.value.length > 0), O = p(), k = p(), A = p(), j = (e, t) => `${oe.value}-${x.getItemKey(e, t)}`, M = r(() => T.value.map(j)), ae = r(() => E.value.map(j)), oe = r((e) => {
			let t = b.value.map(x.getItemKey);
			if (O.value === void 0) return e ?? y(...t);
			if (t.length !== O.value.length) return y(...t);
			let n = O.value.map(x.getItemKey), r = new Set(n);
			for (let e = 0; e < t.length; e++) if (r.has(t[e]) && n[e] !== t[e]) return y(...t);
			return e ?? y(...t);
		});
		N(ie, k, T, () => 0), N(D, A, E, () => T.value.length);
		function N(e, t, n, r) {
			let i = re(t, n, {
				handle: "[data-draggable=\"true\"]",
				animation: 150,
				forceFallback: !0,
				fallbackOnBody: !0,
				scrollSensitivity: 80,
				forceAutoScrollFallback: !0,
				onUpdate: (e) => {
					if (e.oldIndex == null || e.newIndex == null) throw Error("Sortable event has no index");
					x.onDragEnd?.(e.oldIndex, e.newIndex) !== !1 && P(r() + e.oldIndex, r() + e.newIndex, !0);
				}
			});
			return g([
				t,
				() => x.disableDragging,
				e
			], ([e, t, n]) => {
				!e || t || !n ? i.stop() : i.start();
			}, { immediate: !0 }), i;
		}
		function P(e, n, r) {
			e !== n && (r && (O.value = t(b.value.slice(), e, n)), x.onSort?.(e, n) !== !1 && t(b.value, e, n));
		}
		function F(e, t) {
			return x.isActive?.(e, t) ?? !1;
		}
		function I(e, t) {
			return x.disableDragging === !0 ? !1 : x.isDraggable?.(e, t) ?? !0;
		}
		function L(e, t) {
			return x.disableRemoving === !0 ? !1 : x.isRemovable?.(e, t) ?? !0;
		}
		function R(e, t) {
			return x.disableToggling === !0 ? !1 : x.isToggable?.(e, t) ?? (v(x.isToggled) || v(x.onToggle));
		}
		function z(e, t) {
			return x.isToggled?.(e, t) ?? !1;
		}
		function B(e, t) {
			return x.disablePinning === !0 ? !1 : x.isPinnable?.(e, t) ?? (v(x.isPinned) || v(x.onPin));
		}
		function V(e, t) {
			return x.isPinned?.(e, t) ?? !1;
		}
		function H(e, t) {
			return x.disableExpanding === !0 ? !1 : x.isExpandable?.(e, t) ?? (v(x.isExpanded) || v(x.onExpand));
		}
		function U(e, t) {
			return x.isExpanded?.(e, t) ?? !1;
		}
		function W(e, t) {
			x.onExpand?.(e, t);
		}
		function G(e, t) {
			x.onToggle?.(e, t);
		}
		function K(e, t) {
			if (t === -1) throw Error("Pinnable item not found");
			let n = T.value.includes(e);
			x.onPin?.(e, t) !== !1 && P(t, T.value.length + (n ? 0 : -1), !1);
		}
		function q(e, t) {
			x.onRemove?.(e, t) !== !1 && b.value.splice(t, 1);
		}
		function J(e) {
			return (t, n) => typeof e == "function" ? e(t, n) : e ?? null;
		}
		let Y = J(x.itemClass), X = J(x.itemClassTitle), Z = J(x.itemClassContent), Q = J(x.itemClassBefore), $ = J(x.itemClassAfter);
		return (t, r) => (u(), a("div", { class: l(t.$style.root) }, [te("div", {
			ref_key: "pinnedContainerRef",
			ref: k,
			class: l(t.$style.list)
		}, [(u(!0), a(n, null, d(T.value, (n, r) => (u(), i(e, {
			key: M.value[r],
			class: l([t.$style.item, m(Y)(n, r)]),
			titleClass: m(X)(n, r),
			contentClass: m(Z)(n, r),
			beforeClass: m(Q)(n, r),
			afterClass: m($)(n, r),
			index: r,
			item: n,
			showDragHandle: w.value,
			isActive: F(n, r),
			isDraggable: I(n, r),
			isRemovable: L(n, r),
			isToggable: R(n, r),
			isToggled: z(n, r),
			isPinnable: B(n, r),
			isPinned: !0,
			isExpandable: H(n, r),
			isExpanded: U(n, r),
			onClick: (e) => S("itemClick", n),
			onRemove: q,
			onToggle: G,
			onPin: K,
			onExpand: W
		}, o({
			title: _(({ item: e, index: n }) => [f(t.$slots, "item-title", {
				index: n,
				item: e
			})]),
			_: 2
		}, [
			C["item-before"] ? {
				name: "before",
				fn: _(({ item: e, index: n }) => [f(t.$slots, "item-before", {
					index: n,
					item: e
				})]),
				key: "0"
			} : void 0,
			C["item-content"] ? {
				name: "content",
				fn: _(({ item: e, index: n }) => [f(t.$slots, "item-content", {
					index: n,
					item: e
				})]),
				key: "1"
			} : void 0,
			C["item-after"] ? {
				name: "after",
				fn: _(({ item: e, index: n }) => [f(t.$slots, "item-after", {
					index: n,
					item: e
				})]),
				key: "2"
			} : void 0
		]), 1032, [
			"class",
			"titleClass",
			"contentClass",
			"beforeClass",
			"afterClass",
			"index",
			"item",
			"showDragHandle",
			"isActive",
			"isDraggable",
			"isRemovable",
			"isToggable",
			"isToggled",
			"isPinnable",
			"isExpandable",
			"isExpanded",
			"onClick"
		]))), 128))], 2), D.value ? (u(), a("div", {
			key: 0,
			ref_key: "unpinnedContainerRef",
			ref: A,
			class: l(t.$style.list)
		}, [(u(!0), a(n, null, d(E.value, (n, r) => (u(), i(e, {
			key: ae.value[r],
			class: l([t.$style.item, m(Y)(n, r)]),
			titleClass: m(X)(n, r),
			contentClass: m(Z)(n, r),
			beforeClass: m(Q)(n, r),
			afterClass: m($)(n, r),
			index: r + (T.value?.length ?? 0),
			item: n,
			showDragHandle: w.value,
			isActive: F(n, r),
			isDraggable: I(n, r),
			isRemovable: L(n, r),
			isToggable: R(n, r),
			isToggled: z(n, r),
			isPinnable: B(n, r),
			isPinned: !1,
			isExpandable: H(n, r),
			isExpanded: U(n, r),
			onClick: (e) => S("itemClick", n),
			onRemove: q,
			onToggle: G,
			onPin: K,
			onExpand: W
		}, o({
			title: _(({ item: e, index: n }) => [f(t.$slots, "item-title", {
				index: n,
				item: e
			})]),
			_: 2
		}, [C["item-content"] ? {
			name: "content",
			fn: _(({ item: e, index: n }) => [f(t.$slots, "item-content", {
				index: n,
				item: e
			})]),
			key: "0"
		} : void 0, C["item-after"] ? {
			name: "after",
			fn: _(({ item: e, index: n }) => [f(t.$slots, "item-after", {
				index: n,
				item: e
			})]),
			key: "1"
		} : void 0]), 1032, [
			"class",
			"titleClass",
			"contentClass",
			"beforeClass",
			"afterClass",
			"index",
			"item",
			"showDragHandle",
			"isActive",
			"isDraggable",
			"isRemovable",
			"isToggable",
			"isToggled",
			"isPinnable",
			"isExpandable",
			"isExpanded",
			"onClick"
		]))), 128))], 2)) : ee("", !0)], 2));
	}
});
export { b as default };

//# sourceMappingURL=PlElementList.vue2.js.map