import '@das-fed/ui/packages/components/tree-table/style.css';
import { defineComponent as he, mergeModels as ee, ref as U, useModel as ce, computed as I, watch as le, createElementBlock as V, openBlock as K, normalizeStyle as j, normalizeClass as ie, createElementVNode as $, renderSlot as L, createCommentVNode as w, toDisplayString as A, Fragment as G, renderList as J, createBlock as Q, unref as N, withCtx as F, createTextVNode as S, withDirectives as M, createVNode as W, vShow as H } from "vue";
import { withInstall as ve } from "@das-fed/utils/with-install/index";
import { setThemeRule as ke } from "@das-fed/web/packages/theme/index";
import { DasTree as be } from "@das-fed/ui/packages/components/tree/index";
import { DasVirtualTree as ye } from "@das-fed/ui/packages/components/virtual-tree/index";
import { DasCheckbox as X } from "@das-fed/ui/packages/components/checkbox/index";
import { vOverlay as ae } from "@das-fed/ui/packages/components/overlay/index";
import { DasEmpty as ge } from "@das-fed/ui/packages/components/empty/index";
import { DasSpin as me } from "@das-fed/ui/packages/components/spin/index";
import { treeFind as Ce, flatTree as te } from "@das-fed/utils/common-tools";
const we = (d, B, r, t, y, E, k) => ({ linkCheck: (T, v, g, b, p) => {
  let n = null;
  if (g ? n = (i) => {
    var u, c, h;
    i[(u = r.value) == null ? void 0 : u.children] && i[(c = r.value) == null ? void 0 : c.children].length && i[(h = r.value) == null ? void 0 : h.children].forEach((o) => {
      n && n(o), o[v + "-targetId"] == T && d.nodeKey && !t(i, p) && !y(i) && (b[i[d.nodeKey]].data[v] = !0, b[i[d.nodeKey]].data[v + "-targetId"] = T);
    });
  } : n = (i) => {
    var u, c, h;
    i[(u = r.value) == null ? void 0 : u.children] && i[(c = r.value) == null ? void 0 : c.children].length && i[(h = r.value) == null ? void 0 : h.children].forEach((o) => {
      i[v + "-targetId"] == o[E.value] && d.nodeKey && !t(o, p) && !y(o) && (b[o[d.nodeKey]].data[v] = !1, b[o[d.nodeKey]].data[v + "-targetId"] = o[d.nodeKey]), n && n(o);
    });
  }, k.value && k.value.length)
    for (let i = 0; i < k.value.length; i++)
      n && n(k.value[i]);
} }), Ke = (d, B, r, t, y, E, k) => ({ linkCheck: (T, v, g, b, p) => {
  let n = null;
  if (g ? n = (i) => {
    var u, c, h;
    i[(u = r.value) == null ? void 0 : u.children] && i[(c = r.value) == null ? void 0 : c.children].length && i[(h = r.value) == null ? void 0 : h.children].forEach((o) => {
      i[v + "-targetId"] == o[E.value] && d.nodeKey && (!t(o, p) && !y(o) && (b[o[d.nodeKey]].data[v] = !0), b[o[d.nodeKey]].data[v + "-targetId"] = o[d.nodeKey]), n && n(o);
    });
  } : n = (i) => {
    var u, c, h;
    i[(u = r.value) == null ? void 0 : u.children] && i[(c = r.value) == null ? void 0 : c.children].length && i[(h = r.value) == null ? void 0 : h.children].forEach((o) => {
      n && n(o), o[v + "-targetId"] == T && d.nodeKey && (!t(i, p) && !y(i) && (b[i[d.nodeKey]].data[v] = !1), b[i[d.nodeKey]].data[v + "-targetId"] = T);
    });
  }, k.value && k.value.length)
    for (let i = 0; i < k.value.length; i++)
      n && n(k.value[i]);
} }), Te = (d, B, r, t, y, E, k) => ({ linkCheck: (T, v, g, b, p) => {
  let n = null;
  if (g ? n = (i) => {
    var u, c, h;
    i[(u = r.value) == null ? void 0 : u.children] && i[(c = r.value) == null ? void 0 : c.children].length && i[(h = r.value) == null ? void 0 : h.children].forEach((o) => {
      i[v + "-targetId"] == o[E.value] && d.nodeKey && (!t(o, p) && !y(o) && (b[o[d.nodeKey]].data[v] = !0), b[o[d.nodeKey]].data[v + "-targetId"] = o[d.nodeKey]), n && n(o);
    });
  } : n = (i) => {
    var u, c, h;
    i[d.nodeKey] === T ? delete b[i[d.nodeKey]].data[v + "-targetId"] : i[(u = r.value) == null ? void 0 : u.children] && i[(c = r.value) == null ? void 0 : c.children].length && i[(h = r.value) == null ? void 0 : h.children].forEach((o) => {
      o[d.nodeKey] == T ? d.nodeKey && delete b[o[d.nodeKey]].data[v + "-targetId"] : n && n(o);
    });
  }, k.value && k.value.length) {
    const i = Ce(k.value, (u) => u[d.nodeKey] === T);
    n && n(i);
  }
} }), pe = { class: "table-header" }, Le = { class: "title table-label" }, Ie = {
  key: 0,
  class: "table-label"
}, Ve = {
  class: /* @__PURE__ */ ie(["table-body", "das-tree-box"])
}, $e = { class: "tree-node" }, Ne = { class: "tree-node" }, Ee = { class: "first-column" }, De = { class: "title" }, Ae = { class: "tree-node" }, Be = { class: "tree-node" }, Re = { class: "first-column" }, Fe = { class: "title" }, Me = {
  name: "DasTreeTable",
  inheritAttrs: !1
}, Oe = /* @__PURE__ */ he({
  ...Me,
  props: /* @__PURE__ */ ee({
    virtual: { type: Boolean, default: !0 },
    linkageType: { default: "first" },
    keyword: {},
    loading: { type: Boolean },
    nodeKey: { default: "id" },
    title: { default: "" },
    height: {},
    border: { type: Boolean },
    data: {},
    fistLabel: { default: "name" },
    checkboxLabels: { default: () => ["check1", "check2"] },
    allCheckboxNames: { default: () => ["全选一", "全选二"] },
    allCheckboxDisabled: { default: () => [!1, !1] },
    checkboxNames: { default: () => ["", ""] },
    checkboxWidth: { default: () => ["100px", "100px"] },
    defaultExpandAll: { type: Boolean, default: !0 },
    props: {},
    isView: { type: Boolean },
    checkNum: {},
    twoLinkFirstCheck: { type: Boolean },
    emptyText: { default: "暂无数据" }
  }, {
    keyword: { type: String, default: "" },
    keywordModifiers: {}
  }),
  emits: /* @__PURE__ */ ee(["update:data", "update:keyword", "all-check", "item-check"], ["update:keyword"]),
  setup(d, { expose: B, emit: r }) {
    const t = d, y = r, E = U(), k = ce(d, "keyword"), D = I(() => {
      var e;
      return t.checkNum ?? ((e = t.checkboxLabels) == null ? void 0 : e.length) ?? 0;
    }), T = I(() => t != null && t.height ? t.height + "px" : "100%"), v = I(() => {
      if (t != null && t.height) {
        if (typeof t.height == "string") {
          if (t.height.includes("%")) return;
          if (t.height.includes("px")) return Number(t.height.replace("px", ""));
        }
        return t.height;
      }
    }), g = I(() => {
      const e = { ...t.props };
      return e != null && e.disabled && delete e.disabled, e != null && e.hidden && delete e.hidden, e != null && e.parentId && delete e.parentId, { children: "children", disabled: "disabled", isLeaf: "leaf", ...e, label: t.fistLabel };
    }), b = I(() => {
      var e;
      return ((e = t.props) == null ? void 0 : e.parentId) ?? "parentId";
    }), p = I(() => {
      var a, l, s, f, C, R;
      let e = [];
      return (a = t.props) != null && a.disabled && ((l = t.props) != null && l.disabled.length) ? ((s = t.props) != null && s.disabled[0] ? e[0] = (f = t.props) == null ? void 0 : f.disabled[0] : e[0] = "disabled1", (C = t.props) != null && C.disabled[1] ? e[1] = (R = t.props) == null ? void 0 : R.disabled[1] : e[1] = "disabled2") : e = ["disabled1", "disabled2"], e;
    }), n = (e, a) => typeof p.value[a] == "function" ? p.value[a](e, a) : e[p.value[a]], i = I(() => {
      var e;
      return ((e = t.props) == null ? void 0 : e.hidden) ?? "hidden";
    }), u = (e) => typeof i.value == "function" ? i.value(e) : e[i.value], c = U(), h = U([!1, !1]), o = U([!1, !1]), m = I({
      get: () => t.data,
      set: (e) => y("update:data", e)
    }), Y = (e) => {
      const a = {};
      return Array.isArray(e) && e.forEach((l) => {
        const s = { data: l };
        a[l[t.nodeKey]] = s, Array.isArray(l[g.value.children]) && Object.assign(a, Y(l[g.value.children]));
      }), a;
    }, Z = () => Y(m.value), _ = (e, a, l) => {
      t.allCheckboxDisabled[l] || (e > 0 && a > 0 ? (h.value[l] = !1, o.value[l] = !0) : e == 0 && a >= 0 ? (h.value[l] = !1, o.value[l] = !1) : (h.value[l] = !0, o.value[l] = !1));
    }, oe = (e) => {
      const a = g.value.children;
      let l = [];
      l = te(m.value, a);
      for (let s = 0; s < D.value; s++) {
        let f = 0, C = 0;
        l.forEach((R) => {
          n(R, s) || u(R) || (R[t.checkboxLabels[s]] ? f++ : C++);
        }), _(f, C, s);
      }
    }, { linkCheck: se } = we(
      t,
      y,
      g,
      n,
      u,
      b,
      m
    ), { linkCheck: ne } = Ke(t, y, g, n, u, b, m), { linkCheck: de } = Te(
      t,
      y,
      g,
      n,
      u,
      b,
      m
    ), ue = (e, a, l, s, f) => t.linkageType === "custom" ? void 0 : {
      first: se,
      two: ne,
      three: de
    }[t.linkageType](e, a, l, s, f), z = (e, a) => {
      y("all-check", e, a), h.value[a] = e, o.value[a] = !1;
      const l = Z();
      if (l)
        for (let s in l)
          !n(l[s].data, a) && !u(l[s].data) && (l[s].data[t.checkboxLabels[a]] = e, delete l[s].data[t.checkboxLabels[a] + "-targetId"]);
      if (t.twoLinkFirstCheck) {
        if (e) {
          if (a === 1) {
            if (t.allCheckboxDisabled[a - 1]) return;
            z(e, a - 1);
          }
        } else if (a === 0) {
          if (t.allCheckboxDisabled[a + 1]) return;
          z(e, a + 1);
        }
      }
    }, re = I(() => {
      let e = [];
      if (!m.value || !m.value.length) return e;
      const a = g.value.children;
      return e = te(m.value, a).filter(
        (s) => s[g.value.label].toLowerCase().includes(k.value.toLowerCase())
      ), e;
    }), q = I(() => {
      var a;
      let e = !1;
      return (!m.value || !m.value.length) && (e = !0), k.value && !((a = re.value) != null && a.length) && (e = !0), e;
    }), fe = () => new Promise((e) => {
      const a = () => {
        var l;
        if ((l = c.value) != null && l.treeRef) return e(!0);
        setTimeout(() => a(), 50);
      };
      a();
    });
    let P = "";
    const x = async () => {
      var e, a;
      P != k.value && (await fe(), (a = (e = c.value) == null ? void 0 : e.treeRef) == null || a.filter(k.value), P = k.value);
    };
    le(
      () => m.value,
      () => {
        oe(), x();
      },
      { immediate: !0, deep: !0 }
    ), le(() => k.value, x);
    const O = (e, a, l) => {
      y("item-check", e, a, l);
      const s = Z(), f = t.checkboxLabels[l];
      s && (s[a[t.nodeKey]].data[f + "-targetId"] = a[t.nodeKey], s[a[t.nodeKey]].data[f] = e), ue(a[t.nodeKey], f, e, s, l), t.twoLinkFirstCheck && (e ? l === 1 && !n(a, l - 1) && !u(a) && O(e, a, l - 1) : l === 0 && !n(a, l + 1) && !u(a) && O(e, a, l + 1));
    };
    return B({
      $treeTableBody: c,
      /** 获取树的节点数据 */
      getTreeNodesMap: Z,
      /** 手动全选/取消全选；参数val,index */
      setAllCheck: z,
      /** 手动选中/取消选中；参数val,data,index */
      setCheck: O,
      /** 手动联动全选;参数trueNum，falseNum，index */
      linkAllCheck: _
    }), (e, a) => (K(), V(
      "div",
      {
        ref_key: "dasTreeTableRef",
        ref: E,
        class: ie(["das-ui-tree-table", e.border ? "is-border" : ""]),
        style: j({ height: T.value })
      },
      [
        $("div", pe, [
          $(
            "div",
            Le,
            A(e.title),
            1
            /* TEXT */
          ),
          e.$slots.headerLeft ? L(e.$slots, "headerLeft", { key: 0 }, void 0, !0) : w("v-if", !0),
          (K(!0), V(
            G,
            null,
            J(D.value, (l) => (K(), V(
              "div",
              {
                class: "check-item",
                style: j({ width: e.checkboxWidth[0] })
              },
              [
                L(e.$slots, e.checkboxLabels[l - 1] + "Header", {}, () => [
                  e.isView ? (K(), V(
                    "span",
                    Ie,
                    A(e.allCheckboxNames[l - 1] ?? ""),
                    1
                    /* TEXT */
                  )) : (K(), Q(N(X), {
                    key: 1,
                    modelValue: h.value[l - 1],
                    "onUpdate:modelValue": (s) => h.value[l - 1] = s,
                    checkboxType: "default",
                    indeterminate: o.value[l - 1],
                    disabled: e.allCheckboxDisabled[l - 1] || !m.value || !m.value.length,
                    isView: e.isView,
                    onChange: (s) => {
                      z(s, l - 1);
                    }
                  }, {
                    default: F(() => [
                      S(
                        A(e.allCheckboxNames[l - 1] ?? ""),
                        1
                        /* TEXT */
                      )
                    ]),
                    _: 2
                    /* DYNAMIC */
                  }, 1032, ["modelValue", "onUpdate:modelValue", "indeterminate", "disabled", "isView", "onChange"]))
                ], !0)
              ],
              4
              /* STYLE */
            ))),
            256
            /* UNKEYED_FRAGMENT */
          )),
          e.$slots.headerRight ? L(e.$slots, "headerRight", { key: 1 }, void 0, !0) : w("v-if", !0)
        ]),
        $("div", Ve, [
          e.virtual ? M((K(), Q(N(ye), {
            key: 0,
            ref_key: "$treeTableBody",
            ref: c,
            nodeKey: e.nodeKey,
            data: m.value,
            props: g.value,
            "expand-on-click-node": !1,
            "default-expand-all": e.defaultExpandAll,
            overlayZIndex: "auto",
            height: v.value,
            style: { width: "100%" }
          }, {
            default: F(({ data: l, node: s }) => [
              M((K(), V("div", $e, [
                $("div", Ne, [
                  $("div", Ee, [
                    e.$slots.icon ? L(e.$slots, "icon", {
                      key: 0,
                      node: s,
                      data: l
                    }, () => [
                      a[0] || (a[0] = S("icon"))
                    ], !0) : w("v-if", !0),
                    L(e.$slots, "title", {
                      node: s,
                      data: l
                    }, () => [
                      $(
                        "span",
                        De,
                        A(l[e.fistLabel]),
                        1
                        /* TEXT */
                      )
                    ], !0)
                  ]),
                  e.$slots.bodyLeft ? L(e.$slots, "bodyLeft", {
                    key: 0,
                    node: s,
                    data: l
                  }, void 0, !0) : w("v-if", !0),
                  u(l) ? w("v-if", !0) : (K(!0), V(
                    G,
                    { key: 1 },
                    J(D.value, (f) => (K(), V(
                      "div",
                      {
                        class: "check-item",
                        style: j({ width: e.checkboxWidth[f - 1] })
                      },
                      [
                        W(N(X), {
                          modelValue: l[e.checkboxLabels[f - 1]],
                          "onUpdate:modelValue": (C) => l[e.checkboxLabels[f - 1]] = C,
                          checkboxType: "default",
                          isView: e.isView,
                          disabled: n(l, f - 1),
                          onChange: (C) => {
                            O(C, l, f - 1);
                          }
                        }, {
                          default: F(() => [
                            S(
                              A(e.checkboxNames[f - 1] ?? ""),
                              1
                              /* TEXT */
                            )
                          ]),
                          _: 2
                          /* DYNAMIC */
                        }, 1032, ["modelValue", "onUpdate:modelValue", "isView", "disabled", "onChange"])
                      ],
                      4
                      /* STYLE */
                    ))),
                    256
                    /* UNKEYED_FRAGMENT */
                  )),
                  e.$slots.bodyRight ? L(e.$slots, "bodyRight", {
                    key: 2,
                    node: s,
                    data: l
                  }, void 0, !0) : w("v-if", !0)
                ])
              ])), [
                [N(ae), { x: !1, "z-index": "auto" }]
              ])
            ]),
            _: 3
            /* FORWARDED */
          }, 8, ["nodeKey", "data", "props", "default-expand-all", "height"])), [
            [H, !q.value && !e.loading]
          ]) : w("v-if", !0),
          e.virtual ? w("v-if", !0) : M((K(), Q(N(be), {
            key: 1,
            ref_key: "$treeTableBody",
            ref: c,
            nodeKey: e.nodeKey,
            data: m.value,
            props: g.value,
            "expand-on-click-node": !1,
            "default-expand-all": e.defaultExpandAll,
            overlayZIndex: "auto",
            height: v.value,
            style: { width: "100%" }
          }, {
            default: F(({ data: l, node: s }) => [
              M((K(), V("div", Ae, [
                $("div", Be, [
                  $("div", Re, [
                    e.$slots.icon ? L(e.$slots, "icon", {
                      key: 0,
                      node: s,
                      data: l
                    }, () => [
                      a[1] || (a[1] = S("icon"))
                    ], !0) : w("v-if", !0),
                    L(e.$slots, "title", {
                      node: s,
                      data: l
                    }, () => [
                      $(
                        "span",
                        Fe,
                        A(l[e.fistLabel]),
                        1
                        /* TEXT */
                      )
                    ], !0)
                  ]),
                  e.$slots.bodyLeft ? L(e.$slots, "bodyLeft", {
                    key: 0,
                    node: s,
                    data: l
                  }, void 0, !0) : w("v-if", !0),
                  u(l) ? w("v-if", !0) : (K(!0), V(
                    G,
                    { key: 1 },
                    J(D.value, (f) => (K(), V(
                      "div",
                      {
                        class: "check-item",
                        style: j({ width: e.checkboxWidth[f - 1] })
                      },
                      [
                        W(N(X), {
                          modelValue: l[e.checkboxLabels[f - 1]],
                          "onUpdate:modelValue": (C) => l[e.checkboxLabels[f - 1]] = C,
                          checkboxType: "default",
                          isView: e.isView,
                          disabled: n(l, f - 1),
                          onChange: (C) => {
                            O(C, l, f - 1);
                          }
                        }, {
                          default: F(() => [
                            S(
                              A(e.checkboxNames[f - 1] ?? ""),
                              1
                              /* TEXT */
                            )
                          ]),
                          _: 2
                          /* DYNAMIC */
                        }, 1032, ["modelValue", "onUpdate:modelValue", "isView", "disabled", "onChange"])
                      ],
                      4
                      /* STYLE */
                    ))),
                    256
                    /* UNKEYED_FRAGMENT */
                  )),
                  e.$slots.bodyRight ? L(e.$slots, "bodyRight", {
                    key: 2,
                    node: s,
                    data: l
                  }, void 0, !0) : w("v-if", !0)
                ])
              ])), [
                [N(ae), { x: !1, "z-index": "auto" }]
              ])
            ]),
            _: 3
            /* FORWARDED */
          }, 8, ["nodeKey", "data", "props", "default-expand-all", "height"])), [
            [H, !q.value && !e.loading]
          ]),
          w(" 空状态 "),
          M(W(
            N(ge),
            null,
            null,
            512
            /* NEED_PATCH */
          ), [
            [H, q.value && !e.loading]
          ]),
          w(" 加载状态 "),
          M(W(N(me), {
            size: "small",
            spinning: e.loading
          }, {
            default: F(() => a[2] || (a[2] = [
              $(
                "div",
                { style: { width: "100%", height: "100%" } },
                null,
                -1
                /* HOISTED */
              )
            ])),
            _: 1
            /* STABLE */
          }, 8, ["spinning"]), [
            [H, e.loading]
          ])
        ])
      ],
      6
      /* CLASS, STYLE */
    ));
  }
}), Se = (d, B) => {
  const r = d.__vccOpts || d;
  for (const [t, y] of B)
    r[t] = y;
  return r;
}, Ue = /* @__PURE__ */ Se(Oe, [["__scopeId", "data-v-5e8ac73d"]]), ze = {
  "--das-ui-tree-table-highlight-color": "#5582f3",
  "--das-ui-tree-table-header-color": "#212121",
  "--das-ui-tree-table-header-bg-color": "#F2F2F2",
  "--das-ui-tree-table-border-color": "#D9D9D9"
};
ke(ze);
const _e = ve(Ue), Pe = () => U(null), xe = { name: "tree-table 表格树" };
export {
  _e as DasTreeTable,
  Pe as DasTreeTableRef,
  xe as default
};
