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