import { defineComponent as X, inject as se, resolveComponent as Q, openBlock as E, createElementBlock as w, Fragment as le, renderList as oe, normalizeClass as W, createElementVNode as R, withModifiers as $, normalizeStyle as ie, createCommentVNode as N, toDisplayString as re, createBlock as Y, withCtx as ce, withDirectives as de, createVNode as ae, vShow as ue, reactive as pe, computed as fe, onMounted as he, nextTick as j, watch as ge, provide as Ie, mergeProps as Se } from "vue";
import { C as n } from "../Shared/base-PQGakkrl.js";
const ke = ["onClick"], ye = ["onClick"], me = ["onClick", "onDblclick"], Ce = /* @__PURE__ */ X({
  name: "ClTreeUl",
  __name: "Ul",
  props: {
    list: {
      type: Array,
      default: () => []
    },
    keyIndex: String,
    multiple: {
      type: Boolean,
      default: !1
    },
    isSingleSelectParent: {
      type: Boolean,
      default: !1
    },
    isDisabled: {
      type: Boolean,
      default: !1
    },
    firstOpenIds: {
      type: Array,
      default: () => []
    },
    openIds: {
      type: Array,
      default: () => []
    },
    selectIds: {
      type: Array,
      default: () => []
    },
    selectUnIds: {
      type: Array,
      default: () => []
    },
    shrinkBackgroundColor: {
      type: String,
      default: "#FFFFFF"
    }
  },
  emits: ["onSelect", "onShrink"],
  setup(g, { emit: K }) {
    const C = g, h = K, O = se("$TreeTop"), o = {
      dbClickTimeOut: null
    }, f = (u) => {
      h("onSelect", u.toString());
    }, v = (u) => {
      h("onShrink", u.toString());
    }, M = (u) => {
      const I = {
        "tree-node": !0
      };
      if (I[`tree-level-${u.level}`] = !0, I["tree-node-end"] = u.isEnd, !u.isEnd) {
        const S = C.openIds.indexOf(u.id.toString()) > -1;
        I["tree-node-open"] = S, I["tree-node-closed"] = !S;
      }
      return I;
    }, q = (u, I) => {
      const S = {};
      return u.level === 1 && (I === 0 || I === C.list.length - 1) && (S.backgroundColor = C.shrinkBackgroundColor), S;
    }, G = (u) => {
      const I = u.id.toString(), S = {
        "tree-node-text": !0
      };
      return S["tree-node-select"] = C.selectIds.indexOf(I) > -1, S["tree-node-selectUn"] = C.selectUnIds.indexOf(I) > -1, S;
    }, x = (u) => {
      v(u.id);
    }, U = (u) => {
      f(u.id);
    }, V = (u) => {
      const I = u.id.toString();
      if (O.isCheckChildren(u.id)) {
        let S = !0;
        !C.multiple && !C.isSingleSelectParent && (S = !1), S && (window.clearTimeout(o.dbClickTimeOut), o.dbClickTimeOut = window.setTimeout(() => {
          f(u.id);
        }, 170));
      } else
        f(I);
    }, J = (u) => {
      O.isCheckChildren(u.id) && (o.dbClickTimeOut && window.clearTimeout(o.dbClickTimeOut), v(u.id));
    }, D = (u) => {
      f(u);
    }, F = (u) => {
      v(u);
    };
    return (u, I) => {
      const S = Q("cl-tree-ul"), L = Q("cl-transition-slideupdown");
      return E(), w("ul", null, [
        (E(!0), w(le, null, oe(g.list, (k, B) => (E(), w("li", {
          key: `${g.keyIndex}-${B}`,
          class: W(M(k))
        }, [
          R("i", {
            class: "tree-shrink",
            style: ie(q(k, B)),
            onClick: $((P) => x(k), ["stop"])
          }, null, 12, ke),
          R("span", {
            class: W(G(k))
          }, [
            g.multiple ? (E(), w("i", {
              key: 0,
              class: "tree-node-checkbox",
              onClick: $((P) => U(k), ["stop"])
            }, null, 8, ye)) : N("", !0),
            R("a", {
              href: "javascript:",
              tabindex: "-1",
              onClick: $((P) => V(k), ["stop"]),
              onDblclick: $((P) => J(k), ["stop"])
            }, [
              R("span", null, re(k.showTextLabel), 1)
            ], 40, me)
          ], 2),
          g.firstOpenIds.indexOf(k.id.toString()) > -1 ? (E(), Y(L, { key: 0 }, {
            default: ce(() => [
              de(ae(S, {
                list: k.list,
                keyIndex: g.keyIndex,
                multiple: g.multiple,
                isSingleSelectParent: g.isSingleSelectParent,
                isDisabled: g.isDisabled,
                firstOpenIds: g.firstOpenIds,
                openIds: g.openIds,
                selectIds: g.selectIds,
                selectUnIds: g.selectUnIds,
                shrinkBackgroundColor: g.shrinkBackgroundColor,
                onOnSelect: D,
                onOnShrink: F
              }, null, 8, ["list", "keyIndex", "multiple", "isSingleSelectParent", "isDisabled", "firstOpenIds", "openIds", "selectIds", "selectUnIds", "shrinkBackgroundColor"]), [
                [ue, g.openIds.indexOf(k.id.toString()) > -1]
              ])
            ]),
            _: 2
          }, 1024)) : N("", !0)
        ], 2))), 128))
      ]);
    };
  }
}), xe = { class: "parent-tree" }, Ve = /* @__PURE__ */ X({
  name: "ClTree",
  __name: "Index",
  props: {
    value: String,
    list: {
      type: Array,
      default: () => []
    },
    className: String,
    disabled: {
      type: Boolean,
      default: !1
    },
    multiple: {
      type: Boolean,
      default: !1
    },
    isSingleSelectParent: {
      type: Boolean,
      default: !1
    },
    textFormat: String,
    isOutMerge: {
      type: Boolean,
      default: !0
    },
    parentKey: {
      type: String,
      default: "parentId"
    },
    topParentValue: {
      type: String,
      default: "0"
    },
    shrinkColorBg: {
      type: String,
      default: "#FFFFFF"
    }
  },
  emits: ["update:value", "onChange", "onInit"],
  setup(g, { expose: K, emit: C }) {
    const h = g, O = C, o = pe({
      keyIndex: `tree-${n.getRandom(8)}`,
      firstOpenIds: [],
      openParentIds: [],
      selectedIds: [],
      selectedUnIds: [],
      list: [],
      isFirstDataFinish: !1
    }), f = {
      dataJson: {},
      dataGroup: {},
      childLastIdsJson: {}
    }, v = fe(() => ({
      keyIndex: o.keyIndex,
      multiple: h.multiple,
      isDisabled: h.disabled,
      shrinkBackgroundColor: h.shrinkColorBg,
      isSingleSelectParent: h.isSingleSelectParent
    })), M = (t) => {
      O("update:value", t);
    }, q = (t, e) => {
      M(t), O("onChange", t, e);
    }, G = () => {
      j(() => {
        O("onInit");
      });
    }, x = (t) => n.isExistJsonKey(f.dataGroup, t.toString()), U = (t) => {
      const e = t.toString();
      return x(e) ? f.dataGroup[e] : [];
    }, V = (t) => {
      const e = t.toString();
      return n.isExistJsonKey(f.dataJson, e) ? f.dataJson[e] : null;
    }, J = (t, e) => {
      const s = [], a = e + 1;
      return U(t).forEach((d) => {
        const l = n.getJsonClone(d), i = l.id.toString(), r = x(i);
        l.isEnd = !r, l.level = a, r && (l.list = J(i, a)), s.push(l);
      }), s;
    }, D = (t) => {
      const { parentKey: e, topParentValue: s } = h, a = s.toString(), d = {}, l = {};
      n.isEmpty(t) || t.forEach((i) => {
        const r = n.getJsonClone(i);
        r.showTextLabel = r.name;
        const p = r.id.toString(), c = r[e].toString();
        d[p] = r, n.isExistJsonKey(l, c) || (l[c] = []), l[c].push(r);
      }), f.dataJson = d, f.dataGroup = l, f.childLastIdsJson = {}, o.list = J(a, 0);
    }, F = (t) => {
      let e = [];
      const { parentKey: s, topParentValue: a } = h, d = a.toString(), l = n.isEmpty(t) ? "" : t.toString();
      if (n.isExistJsonKey(f.dataJson, l)) {
        e.push(l);
        const i = f.dataJson[l][s].toString();
        if (i !== d) {
          const r = F(i);
          e = e.concat(r);
        }
      }
      return e;
    }, u = (t) => {
      const e = F(t);
      return e.splice(0, 1), e.reverse(), e;
    }, I = (t) => {
      let e = [];
      const s = n.isEmpty(t) ? "" : t.toString();
      return e.push(s), x(s) && U(s).forEach((a) => {
        const d = a.id.toString(), l = I(d);
        e = e.concat(l);
      }), e;
    }, S = (t) => {
      const e = I(t);
      return e.splice(0, 1), e;
    }, L = (t, e, s) => {
      const a = { add: [], del: [] }, d = { add: [], del: [] };
      let l = n.getArrClone(n.isUndefined(e) ? o.selectedIds : e), i = n.getArrClone(
        n.isUndefined(s) ? o.selectedUnIds : s
      );
      const r = l.indexOf(t);
      r === -1 ? l.push(t) : l.splice(r, 1), i = n.getArrRemoveById(i, t);
      const p = u(t);
      return p.reverse(), p.length > 0 && p.forEach((c) => {
        let A = 0, b = 0;
        f.dataGroup[c].forEach((m) => {
          const T = m.id.toString();
          n.isExistValue(l, T) && (A += 1), n.isExistValue(i, T) && (b += 1);
        });
        const y = f.dataGroup[c].length;
        A === 0 && b === 0 ? (a.del.push(c), l = n.getArrRemoveById(l, c), d.del.push(c), i = n.getArrRemoveById(i, c)) : A === y ? (a.add.push(c), l.push(c), d.del.push(c), i = n.getArrRemoveById(i, c)) : (a.del.push(c), l = n.getArrRemoveById(l, c), d.add.push(c), i.push(c));
      }), { select: a, selectUn: d };
    }, k = (t) => {
      let e = [];
      return n.isExistJsonKey(f.childLastIdsJson, t) && f.childLastIdsJson[t].length > 0 ? e = f.childLastIdsJson[t] : (x(t) ? U(t).forEach((s) => {
        const a = k(s.id.toString());
        a.length > 0 && (e = e.concat(a));
      }) : e.push(t), f.childLastIdsJson[t] = e), e;
    }, B = (t) => {
      const { parentKey: e, topParentValue: s } = h, a = s.toString(), d = {}, l = [];
      let i = [];
      if (t.forEach((r) => {
        const p = V(r);
        if (!n.isUndefined(p)) {
          const c = p[e].toString();
          c === a ? i.push(r) : (n.isExistJsonKey(d, c) || (d[c] = []), d[c].push(r));
        }
      }), !n.isEmpty(d))
        for (const [r, p] of Object.entries(d))
          p.length === U(r).length ? l.push(r) : i = i.concat(p);
      if (l.length > 0) {
        i.push(...l);
        const r = n.getArrUnique(i);
        return B(r);
      } else
        return i;
    }, P = (t) => {
      const e = [], s = n.getArrConvertJson(t);
      return t.forEach((d) => {
        const l = U(d);
        if (n.isEmpty(l))
          e.push(d);
        else {
          const i = [];
          l.forEach((r) => {
            n.isExistJsonKey(s, r.id.toString()) && i.push(r.id);
          }), l.length !== i.length && e.push(d);
        }
      }), B(e);
    }, Z = (t) => {
      let e = [];
      return t.forEach((s) => {
        const a = k(s);
        e.push(...a);
      }), e = n.getArrUnique(e), e;
    }, _ = (t, e) => h.multiple ? (n.isBoolean(e) ? e : h.isOutMerge) ? P(t) : Z(t) : [t], z = (t) => {
      const e = [], s = [];
      return t.forEach((a) => {
        const d = V(a);
        e.push(d), s.push(d.name);
      }), { list: e, label: s };
    }, H = (t) => {
      let e = [], s = [];
      t.forEach((d) => {
        const l = V(d);
        if (!n.isUndefined(l)) {
          const i = L(d, e, s);
          e = n.getArrRemoveById(e, i.select.del), i.select.add.length > 0 && e.push(...i.select.add), s = n.getArrRemoveById(s, i.selectUn.del), i.selectUn.add.length > 0 && s.push(...i.selectUn.add);
          const r = S(d);
          e.push(...r), e.push(d), e = n.getArrUnique(e), s = n.getArrUnique(s);
        }
      }), o.selectedIds = e;
      const a = n.getArrRemoveById(s, e);
      o.selectedUnIds = n.getArrClone(a), o.firstOpenIds = n.getArrClone(a), o.openParentIds = n.getArrClone(a);
    }, ee = () => {
      if (D(h.list), !n.isEmpty(h.value) && h.value) {
        const t = h.value.split(",");
        H(t);
      }
      G();
    };
    he(() => {
      j(() => {
        ee();
      });
    }), ge(
      [() => h.value, () => h.list],
      ([t, e]) => {
        if (D(e), !n.isUndefined(t)) {
          const s = o.selectedIds, a = t && !n.isEmpty(t) ? t.split(",") : [], d = _(a), l = _(s), i = n.isEqualArr(d, l);
          o.selectedIds = [], H(i ? s : a);
        }
      }
    );
    const te = (t) => {
      const e = t.toString();
      n.isExistValue(o.firstOpenIds, e) || o.firstOpenIds.push(e), j(() => {
        const s = o.openParentIds.indexOf(e);
        s === -1 ? o.openParentIds.push(e) : o.openParentIds.splice(s, 1);
      });
    }, ne = (t) => {
      const e = t.toString();
      if (h.multiple) {
        const l = o.selectedIds.indexOf(e) === -1, i = S(e), r = L(e);
        let p = n.getArrClone(o.selectedIds), c = n.getArrClone(o.selectedUnIds);
        r.select.del.forEach((y) => {
          const m = p.indexOf(y);
          m > -1 && p.splice(m, 1);
        }), r.select.add.length > 0 && (p = p.concat(r.select.add), p = n.getArrUnique(p)), r.selectUn.del.forEach((y) => {
          const m = c.indexOf(y);
          m > -1 && c.splice(m, 1);
        }), r.selectUn.add.length > 0 && (c = c.concat(r.selectUn.add), c = n.getArrUnique(c)), i.length > 0 && i.forEach((y) => {
          const m = p.indexOf(y), T = c.indexOf(y);
          l ? n.isExistValue(p, y) || p.push(y) : m > -1 && p.splice(m, 1), T > -1 && c.splice(T, 1);
        });
        const A = p.indexOf(e);
        A > -1 ? p.splice(A, 1) : p.push(e);
        const b = c.indexOf(e);
        b > -1 && c.splice(b, 1), o.selectedIds = p, o.selectedUnIds = c;
      } else
        o.selectedIds = [e], o.selectedUnIds = [];
      const s = _(o.selectedIds), a = z(s);
      q(s.join(","), a.list);
    };
    return Ie("$TreeTop", {
      keyIndex: o.keyIndex,
      isCheckChildren: x
    }), K({
      getValue: () => _(o.selectedIds).join(","),
      getValueData: () => {
        const t = _(o.selectedIds);
        return z(t).list;
      },
      setValue: (t) => {
        o.selectedIds = n.isEmpty(t) ? [] : t.split(",");
      }
    }), (t, e) => (E(), w("div", xe, [
      o.list.length > 0 ? (E(), Y(Ce, Se({
        key: 0,
        list: o.list,
        firstOpenIds: o.firstOpenIds,
        openIds: o.openParentIds,
        selectIds: o.selectedIds,
        selectUnIds: o.selectedUnIds
      }, v.value, {
        onOnSelect: ne,
        onOnShrink: te
      }), null, 16, ["list", "firstOpenIds", "openIds", "selectIds", "selectUnIds"])) : N("", !0)
    ]));
  }
});
export {
  Ve as default
};
//# sourceMappingURL=ClTree.js.map
