import { defineComponent as z, computed as L, ref as J, onMounted as K, watch as Q, resolveComponent as I, createElementBlock as v, openBlock as i, Fragment as x, createElementVNode as D, withDirectives as X, renderSlot as T, createBlock as w, createCommentVNode as b, renderList as A, normalizeClass as M, toDisplayString as O, createVNode as Y, withCtx as W, createSlots as U, normalizeProps as q, guardReactiveProps as F, vShow as Z, unref as N } from "vue";
import { d as P } from "./index-DU6yeDGn.js";
import { W as _, _ as R } from "./install-BuMO14Rb.js";
const ee = { class: "wt-tree-line" }, te = { class: "wt-tree-line__icon-wrapper" }, ae = { class: "wt-tree-line__label typo-body-1" }, le = /* @__PURE__ */ z({
  __name: "wt-tree-line",
  props: {
    modelValue: {},
    data: {},
    itemLabel: {},
    itemData: {},
    childrenProp: { default: "children" },
    nestedLevel: { default: 0 },
    lastChild: { type: Boolean, default: !1 },
    selectedParent: { type: Boolean, default: !1 },
    nestedIcons: {},
    nextElement: { type: Boolean, default: !1 },
    multiple: { type: Boolean, default: !1 },
    allowParent: { type: Boolean, default: !1 },
    searchedProp: { default: "searched" },
    rootData: {}
  },
  emits: ["openParent", "update:modelValue"],
  setup(t, { emit: E }) {
    const e = t, u = E, $ = L(
      () => e.itemLabel ? e.data[e.itemLabel] : e.data
    ), B = L(() => {
      const a = e.nestedIcons ? [
        ...e.nestedIcons
      ] : [];
      return e.nestedLevel === 0 || a.push({
        icon: "tree-line",
        hidden: !e.nextElement
      }), a;
    }), g = () => Array.isArray(e.modelValue) ? e.itemData ? e.modelValue.includes(e.data[e.itemData]) : !!e.modelValue.find((l) => P(l, e.data)) : !1, n = L(() => e.multiple ? g() : e.itemData ? e.data[e.itemData] === e.modelValue : P(e.modelValue, e.data)), f = L(() => e.multiple ? n.value : n.value || e.selectedParent), m = (a, l, d) => {
      const r = e.itemData ? a[e.itemData] : a;
      if (l)
        d.some((o) => P(o, r)) || d.push(r);
      else {
        const o = d.findIndex((c) => P(c, r));
        o !== -1 && d.splice(o, 1);
      }
      if (a && Array.isArray(a[e.childrenProp]) && a[e.childrenProp].length)
        for (const o of a[e.childrenProp])
          m(o, l, d);
    }, s = (a, l, d) => {
      const r = (o, c) => {
        if (o === a) {
          if (c) {
            const h = e.itemData ? c[e.itemData] : c, y = d.findIndex((H) => P(H, h));
            y !== -1 && d.splice(y, 1);
          }
          return !0;
        }
        if (o && Array.isArray(o[e.childrenProp]) && o[e.childrenProp].length) {
          for (const h of o[e.childrenProp])
            if (r(h, o))
              return !0;
        }
        return !1;
      };
      r(l, null);
    }, k = () => {
      const a = n.value, l = [
        ...e.modelValue
      ];
      m(e.data, !a, l), a && s(e.data, e.rootData, l), u("update:modelValue", l);
    }, V = () => {
      const a = e.itemData ? e.data[e.itemData] : e.data;
      let l;
      if (e.itemData ? l = e.modelValue.indexOf(e.data[e.itemData]) : l = e.modelValue.findIndex(
        (r) => P(r, e.data)
      ), l === -1) {
        const r = [
          ...e.modelValue
        ];
        r.push(a), u("update:modelValue", r);
        return;
      }
      const d = [
        ...e.modelValue
      ];
      d.splice(l, 1), u("update:modelValue", d);
    }, j = () => {
      if (e.multiple) {
        e.data[e.childrenProp]?.length ? k() : V();
        return;
      }
      if (e.allowParent && !e.data[e.childrenProp])
        return u(
          "update:modelValue",
          e.itemData ? e.data[e.itemData] : e.data
        );
      if (e.data[e.childrenProp]?.length) {
        p.value = !p.value;
        return;
      }
      u(
        "update:modelValue",
        e.itemData ? e.data[e.itemData] : e.data
      );
    }, p = J(!0), C = () => {
      e.nestedLevel > 0 && u("openParent");
    }, G = () => {
      p.value = !1, C();
    };
    return K(() => {
      n.value && C(), e.searchedProp && e.data[e.searchedProp] && C();
    }), Q(
      () => e.modelValue,
      () => {
        n.value && C();
      }
    ), (a, l) => {
      const d = I("wt-icon"), r = I("wt-icon-btn"), o = I("wt-tree-line", !0);
      return i(), v(x, null, [
        D("div", ee, [
          D("div", te, [
            (i(!0), v(x, null, A(t.nestedIcons, (c, h) => (i(), w(d, {
              key: h,
              icon: c.icon,
              class: M({ hidden: c.hidden })
            }, null, 8, ["icon", "class"]))), 128)),
            t.nestedLevel >= 1 ? (i(), w(d, {
              key: 0,
              icon: t.lastChild ? "tree-corner" : "tree-cross"
            }, null, 8, ["icon"])) : b("", !0),
            t.data[t.childrenProp] && t.data[t.childrenProp].length ? (i(), w(r, {
              key: 1,
              icon: p.value ? "plus" : "minus",
              onClick: l[0] || (l[0] = (c) => p.value = !p.value)
            }, null, 8, ["icon"])) : b("", !0)
          ]),
          T(a.$slots, "item-prefix", {
            class: "mr-1",
            data: t.data,
            rootData: t.rootData || t.data,
            nestedLevel: t.nestedLevel,
            active: f.value,
            hasChildren: !!(t.data[t.childrenProp] && t.data[t.childrenProp].length),
            selectedParent: t.selectedParent
          }, void 0, !0),
          D("div", {
            class: M([{
              active: f.value,
              searched: e.data[e.searchedProp]
            }, "wt-tree-line__label-wrapper"]),
            onClick: j
          }, [
            D("p", ae, O($.value), 1),
            f.value ? (i(), w(d, {
              key: 0,
              icon: "chat-message-status-sent"
            })) : b("", !0)
          ], 2)
        ]),
        X(Y(_, null, {
          default: W(() => [
            D("div", null, [
              (i(!0), v(x, null, A(t.data[t.childrenProp], (c, h) => (i(), w(o, {
                key: h,
                "model-value": t.modelValue,
                data: c,
                "children-prop": t.childrenProp,
                "item-label": t.itemLabel,
                "item-data": t.itemData,
                "nested-level": t.nestedLevel + 1,
                "selected-parent": n.value || t.selectedParent,
                "next-element": !!t.data[t.childrenProp][h + 1],
                "nested-icons": B.value,
                "last-child": h === t.data[t.childrenProp].length - 1,
                multiple: t.multiple,
                "allow-parent": t.allowParent,
                "root-data": t.rootData || t.data,
                onOpenParent: G,
                "onUpdate:modelValue": l[1] || (l[1] = (y) => u("update:modelValue", y))
              }, U({ _: 2 }, [
                a.$slots["item-prefix"] ? {
                  name: "item-prefix",
                  fn: W((y) => [
                    T(a.$slots, "item-prefix", q(F(y)), void 0, !0)
                  ]),
                  key: "0"
                } : void 0
              ]), 1032, ["model-value", "data", "children-prop", "item-label", "item-data", "nested-level", "selected-parent", "next-element", "nested-icons", "last-child", "multiple", "allow-parent", "root-data"]))), 128))
            ])
          ], void 0, !0),
          _: 3
        }, 512), [
          [Z, !p.value]
        ])
      ], 64);
    };
  }
}), ne = /* @__PURE__ */ R(le, [["__scopeId", "data-v-ced3d83b"]]), S = {
  Tree: "tree",
  List: "list"
}, ie = { class: "wt-tree" }, de = {
  key: 0,
  class: "wt-tree__content"
}, oe = {
  key: 1,
  class: "wt-tree__list-content"
}, re = ["onClick"], se = /* @__PURE__ */ z({
  __name: "wt-tree",
  props: {
    modelValue: {},
    data: {},
    itemLabel: {},
    itemData: {},
    mode: { default: S.Tree },
    childrenProp: { default: "children" },
    multiple: { type: Boolean },
    allowParent: { type: Boolean, default: !1 }
  },
  emits: ["select", "update:modelValue"],
  setup(t, { emit: E }) {
    const e = t, u = E, $ = L(() => {
      const n = [], f = (m) => {
        n.push(m), m[e.childrenProp] && m[e.childrenProp].forEach((s) => {
          f(s);
        });
      };
      return e.data.forEach((m) => {
        f(m);
      }), n;
    }), B = (n) => {
      u("update:modelValue", e.itemData ? n[e.itemData] : n);
    }, g = (n) => e.itemData ? n[e.itemData] === e.modelValue : P(e.modelValue, n);
    return (n, f) => {
      const m = I("wt-icon");
      return i(), v("div", ie, [
        t.mode === N(S).Tree ? (i(), v("div", de, [
          (i(!0), v(x, null, A(t.data, (s, k) => (i(), w(ne, {
            key: k,
            "model-value": t.modelValue,
            "item-label": t.itemLabel,
            "item-data": t.itemData,
            data: s,
            "children-prop": t.childrenProp,
            multiple: t.multiple,
            "allow-parent": t.allowParent,
            "onUpdate:modelValue": f[0] || (f[0] = (V) => u("update:modelValue", V))
          }, U({ _: 2 }, [
            n.$slots["item-prefix"] ? {
              name: "item-prefix",
              fn: W((V) => [
                T(n.$slots, "item-prefix", q(F(V)), void 0, !0)
              ]),
              key: "0"
            } : void 0
          ]), 1032, ["model-value", "item-label", "item-data", "data", "children-prop", "multiple", "allow-parent"]))), 128))
        ])) : b("", !0),
        t.mode === N(S).List ? (i(), v("div", oe, [
          (i(!0), v(x, null, A($.value, (s, k) => (i(), v("span", {
            key: k,
            class: M(["wt-tree__label-wrapper", { active: g(s) }])
          }, [
            D("p", {
              class: "wt-tree__label",
              onClick: (V) => B(s)
            }, O(t.itemLabel ? s[t.itemLabel] : s), 9, re),
            g(s) ? (i(), w(m, {
              key: 0,
              icon: "chat-message-status-sent",
              class: "wt-tree__label-icon"
            })) : b("", !0)
          ], 2))), 128))
        ])) : b("", !0)
      ]);
    };
  }
}), fe = /* @__PURE__ */ R(se, [["__scopeId", "data-v-f86ae40a"]]);
export {
  fe as default
};
