import { defineComponent as X, shallowRef as f, useSlots as Y, computed as s, watch as V, createVNode as o, mergeProps as k, createTextVNode as K, Fragment as A } from "vue";
import { useNamespace as Z, ElTable as p, ElTableColumn as w, ElCheckbox as F, ElRadio as ee, ElCheckboxGroup as ne } from "element-plus";
import { wetSchemaTableProps as te, wetBaseColumnProps as le } from "./types.mjs";
import { useElementBounding as ae } from "../../node_modules/.pnpm/@vueuse_core@10.9.0_vue@3.4.21/node_modules/@vueuse/core/index.mjs";
import ue from "./components/selection.vue.mjs";
import re from "./components/enum.vue.mjs";
import { useDebounceFn as oe } from "../../node_modules/.pnpm/@vueuse_shared@10.9.0_vue@3.4.21/node_modules/@vueuse/shared/index.mjs";
import i from "../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/get.mjs";
import ie from "../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/difference.mjs";
const O = (e) => Array.isArray(e) ? e.join("") : e, de = /* @__PURE__ */ X({
  name: "WetSchemaTable",
  props: te,
  emits: ["update:selections"],
  setup(e, {
    emit: g,
    expose: _
  }) {
    const E = f(), {
      height: j
    } = ae(E), b = Z("schema-table"), y = Y(), T = f(), h = f([]), z = s(() => {
      const n = {
        ...e
      };
      return delete n.columns, n;
    }), v = f(!0), N = oe(() => {
      v.value = !0;
    }, 200);
    V(() => e.size, () => {
      e.fill && (v.value = !1, N());
    });
    const B = s(() => Array.isArray(e.rowKey) ? e.rowKey.join(".") : e.rowKey), W = s(() => {
      if (e.rowKey)
        return e.selections.map((n) => i(n, e.rowKey, void 0));
    }), $ = (n, t) => {
      var a;
      let l = e.selections;
      !e.rowKey || !t || (n ? l.push(t) : l = l.filter((u) => {
        const r = i(t, e.rowKey, void 0), d = i(u, e.rowKey, void 0);
        return r !== d;
      }), (a = e.rowSelection) != null && a.onChange && e.rowSelection.onChange(l), g("update:selections", l));
    }, G = (n) => {
      let t = [...e.selections];
      const l = e.rowKey || "";
      if (n) {
        let a = t.map((u) => i(u, l, void 0));
        a = a.filter((u) => u !== void 0), e.data.forEach((u) => {
          const r = i(u, l, void 0);
          a.includes(r) || t.push(u);
        }), h.value = ["1"];
      } else {
        h.value = [];
        let a = e.data.map((u) => i(u, l, void 0));
        a = a.filter((u) => u !== void 0), t = t.filter((u) => {
          const r = i(u, l, void 0);
          return !a.includes(r);
        });
      }
      g("update:selections", t);
    }, S = s(() => {
      const n = e.rowKey || [], t = [...e.selections];
      let l = (e.data || []).map((r) => i(r, n, void 0));
      l = l.filter((r) => r !== void 0);
      let a = t.map((r) => i(r, n, void 0));
      return a = a.filter((r) => r !== void 0), ie(l, a).length === 0;
    }), H = s(() => S.value ? !S.value : e.selections.length !== 0);
    V(S, (n) => {
      n && e.selections.length !== 0 ? h.value = ["1"] : h.value = [];
    });
    const q = (n) => {
      var t, l;
      !e.rowKey || n === void 0 || (((t = e.rowSelection) == null ? void 0 : t.type) === "line" || ((l = e.rowSelection) == null ? void 0 : l.type) === "radio") && (e.rowSelection.onChange && e.rowSelection.onChange([n]), g("update:selections", [n]));
    }, J = s(() => {
      var n, t, l;
      return ((n = e.rowSelection) == null ? void 0 : n.type) === "line" || ((t = e.rowSelection) == null ? void 0 : t.type) === "radio" ? !0 : !!((l = e.columns) == null ? void 0 : l.find((u) => u.isScrollMark === !0)) || e.highlightCurrentRow;
    }), M = (n, t) => {
      const l = Array.isArray(t) ? t : [t];
      return i(n, l, e.emptyFormat || void 0);
    }, C = s(() => {
      var t;
      let n = !1;
      return (t = e.columns) == null || t.forEach((l) => {
        l.fixed && (n = !0);
      }), n;
    }), I = f({}), R = (n) => {
      const t = Array.isArray(n.dataIndex) ? n.dataIndex.join("") : n.dataIndex, l = {};
      return Object.keys(le).forEach((a) => {
        n[a] !== void 0 && (l[a] = n[a]);
      }), l.showOverflowTooltip = n.ellipsis ? e.ellipsisProps : !1, l.prop = t, n.columns && n.columns.length !== 0 ? o(w, k(l, {
        key: t
      }), {
        default() {
          return n.columns.map((a) => R(a));
        }
      }) : o(w, k(l, {
        label: l.label || n.title,
        key: t
      }), {
        default({
          row: a,
          $index: u
        }) {
          return o(A, null, [n.isScrollMark && o("span", {
            ref: (r) => I.value[u] = r
          }, null), n.isRender ? y[t] && y[t]({
            row: a,
            $index: u
          }) : n.render ? n.render({
            row: a,
            $index: u
          }) : n.valueEnum ? o(re, {
            valueEnum: n.valueEnum,
            value: M(a, n.dataIndex)
          }, null) : M(a, n.dataIndex)]);
        }
      });
    }, P = f({}), L = (n, t) => {
      const l = [...n, {}], a = [];
      let u = "", r = [], d = 0;
      return l.forEach((c, x) => {
        if (!u)
          u = c[t], r.push(c);
        else if (u === c[t])
          r.push(c);
        else {
          u = c[t];
          const m = d;
          d = x, a.push({
            start: m,
            lg: r.length
          }), r = [], r.push(c);
        }
      }), a;
    };
    e.mergeDataIndexs && V(() => e.data, (n) => {
      var t;
      n && ((t = e.mergeDataIndexs) == null || t.forEach((l) => {
        const a = O(l);
        P.value[a] = L(n, a);
      }));
    }, {
      immediate: !0,
      deep: !0
    });
    const Q = s(() => {
      if (e.mergeDataIndexs || e.spanMethod)
        return (n) => {
          const {
            column: t,
            rowIndex: l
          } = n;
          if (e.spanMethod)
            return e.spanMethod(n);
          if ((e.mergeDataIndexs || []).map((u) => O(u)).includes(t.property)) {
            const u = P.value[t.property].find((r) => r.start === l);
            return u ? [u.lg, 1] : [0, 0];
          } else
            return [1, 1];
        };
    });
    return _({
      scrollToIndex: (n) => {
        var l;
        I.value[n] && I.value[n].scrollIntoView({
          behavior: "smooth",
          block: "center"
        });
        const t = e.data[n];
        t && ((l = T.value) == null || l.setCurrentRow(t));
      }
    }), () => {
      var n;
      return o("div", {
        ref: E,
        class: [b.b(), b.is("fill", e.fill)]
      }, [o(ue, {
        type: (n = e.rowSelection) == null ? void 0 : n.type,
        modelValue: W.value
      }, {
        default: () => [o(p, k(z.value, {
          rowKey: B.value,
          highlightCurrentRow: J.value,
          height: e.height ? e.height : e.fill ? j.value : void 0,
          "onCurrent-change": q,
          ref: T,
          style: {
            display: v.value ? "block" : "none"
          },
          spanMethod: Q.value
        }), {
          default: () => [e.expandable && o(w, {
            fixed: C.value,
            type: "expand"
          }, {
            default({
              row: t,
              $index: l
            }) {
              var a, u, r, d, c, x;
              if (typeof e.expandable == "boolean")
                return y.expandable && y.expandable({
                  row: t,
                  $index: l
                });
              {
                let m;
                const D = (a = e.expandable) == null ? void 0 : a.dataIndex;
                return (u = e.expandable) != null && u.dataOutput ? m = e.expandable.dataOutput : m = (U) => D ? i(U, D, []) : [], o(de, {
                  size: ((r = e.expandable) == null ? void 0 : r.size) || "small",
                  class: [b.e("expandable")],
                  expandable: (d = e.expandable) == null ? void 0 : d.expandable,
                  showIndex: (c = e.expandable) == null ? void 0 : c.showIndex,
                  columns: (x = e.expandable) == null ? void 0 : x.columns,
                  ellipsisProps: e.ellipsisProps,
                  data: m(t, l)
                }, null);
              }
            }
          }), e.rowSelection && e.rowKey && o(w, {
            width: 50,
            fixed: e.rowSelection.fixed || C.value,
            align: "center"
          }, {
            default({
              row: t
            }) {
              var a, u;
              if (!e.rowKey)
                return;
              const l = i(t, e.rowKey, void 0);
              return ((a = e.rowSelection) == null ? void 0 : a.type) === "checkbox" ? o(F, {
                label: l,
                onChange: (r) => $(r, t)
              }, {
                default: () => [K(" ")]
              }) : ((u = e.rowSelection) == null ? void 0 : u.type) === "radio" ? o(ee, {
                label: l
              }, {
                default: () => [K(" ")]
              }) : void 0;
            },
            header() {
              var t;
              return o(A, null, [((t = e.rowSelection) == null ? void 0 : t.type) === "checkbox" && o(ne, {
                modelValue: h.value
              }, {
                default: () => [o(F, {
                  indeterminate: H.value,
                  label: "1",
                  onChange: G
                }, {
                  default: () => [K(" ")]
                })]
              })]);
            }
          }), e.showIndex && o(w, {
            fixed: C.value,
            label: "序号",
            showOverflowTooltip: e.ellipsisProps,
            width: 60
          }, {
            default({
              _: t,
              $index: l
            }) {
              return o(A, null, [l + 1]);
            }
          }), (e.columns || []).map((t) => R(t))]
        })]
      })]);
    };
  }
});
export {
  de as default
};
