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