import '@das-fed/ui/packages/components/tree-select/style.css';
import { ref as L, computed as b, watch as ve, nextTick as q, unref as o, onMounted as De, onBeforeUnmount as xe, defineComponent as Le, useAttrs as Ne, withDirectives as pe, createElementBlock as se, openBlock as ue, normalizeStyle as he, normalizeClass as Pe, createCommentVNode as Oe, createVNode as X, withCtx as R, createElementVNode as Ve, withModifiers as de, mergeProps as me, isRef as ce, createSlots as Re, renderSlot as G, createTextVNode as Me, toDisplayString as Be } from "vue";
import { withInstall as Ee } from "@das-fed/utils/with-install/index";
import { ElPopover as We } from "element-plus";
import "element-plus/es/components/popover/style/css";
import { currentLang as Fe, t as Ie } from "@das-fed/web/packages/i18n/index";
import { DasSelect as Ae } from "@das-fed/ui/packages/components/select/index";
import { DasTreeRef as Ge, DasTree as He } from "@das-fed/ui/packages/components/tree/index";
import { DasIconXiala as ze } from "@das-fed/ui/packages/icons/xiala/index";
import { vDasTooltip as Ue } from "@das-fed/ui/packages/components/text-tooltip/index";
import { SizeWatcher as _e } from "@das-fed/utils/size-watcher";
import { flatTree as qe } from "@das-fed/utils/common-tools";
import { vClickOutside as Xe } from "@das-fed/utils/directive";
import { setThemeRule as Je } from "@das-fed/web/packages/theme/index";
var J = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {};
function Qe(e) {
  return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
}
function Ye(e) {
  var i = typeof e;
  return e != null && (i == "object" || i == "function");
}
var we = Ye, Ze = typeof J == "object" && J && J.Object === Object && J, Ke = Ze, et = Ke, tt = typeof self == "object" && self && self.Object === Object && self, rt = et || tt || Function("return this")(), Ce = rt, lt = Ce, at = function() {
  return lt.Date.now();
}, ot = at, nt = /\s/;
function it(e) {
  for (var i = e.length; i-- && nt.test(e.charAt(i)); )
    ;
  return i;
}
var st = it, ut = st, dt = /^\s+/;
function ct(e) {
  return e && e.slice(0, ut(e) + 1).replace(dt, "");
}
var ft = ct, vt = Ce, pt = vt.Symbol, $e = pt, be = $e, je = Object.prototype, ht = je.hasOwnProperty, mt = je.toString, H = be ? be.toStringTag : void 0;
function bt(e) {
  var i = ht.call(e, H), c = e[H];
  try {
    e[H] = void 0;
    var d = !0;
  } catch {
  }
  var g = mt.call(e);
  return d && (i ? e[H] = c : delete e[H]), g;
}
var gt = bt, yt = Object.prototype, kt = yt.toString;
function Tt(e) {
  return kt.call(e);
}
var St = Tt, ge = $e, wt = gt, Ct = St, $t = "[object Null]", jt = "[object Undefined]", ye = ge ? ge.toStringTag : void 0;
function Dt(e) {
  return e == null ? e === void 0 ? jt : $t : ye && ye in Object(e) ? wt(e) : Ct(e);
}
var xt = Dt;
function Lt(e) {
  return e != null && typeof e == "object";
}
var Nt = Lt, Pt = xt, Ot = Nt, Vt = "[object Symbol]";
function Rt(e) {
  return typeof e == "symbol" || Ot(e) && Pt(e) == Vt;
}
var Mt = Rt, Bt = ft, ke = we, Et = Mt, Te = NaN, Wt = /^[-+]0x[0-9a-f]+$/i, Ft = /^0b[01]+$/i, It = /^0o[0-7]+$/i, At = parseInt;
function Gt(e) {
  if (typeof e == "number")
    return e;
  if (Et(e))
    return Te;
  if (ke(e)) {
    var i = typeof e.valueOf == "function" ? e.valueOf() : e;
    e = ke(i) ? i + "" : i;
  }
  if (typeof e != "string")
    return e === 0 ? e : +e;
  e = Bt(e);
  var c = Ft.test(e);
  return c || It.test(e) ? At(e.slice(2), c ? 2 : 8) : Wt.test(e) ? Te : +e;
}
var Ht = Gt, zt = we, fe = ot, Se = Ht, Ut = "Expected a function", _t = Math.max, qt = Math.min;
function Xt(e, i, c) {
  var d, g, j, y, p, f, x = 0, C = !1, T = !1, V = !0;
  if (typeof e != "function")
    throw new TypeError(Ut);
  i = Se(i) || 0, zt(c) && (C = !!c.leading, T = "maxWait" in c, j = T ? _t(Se(c.maxWait) || 0, i) : j, V = "trailing" in c ? !!c.trailing : V);
  function h(s) {
    var $ = d, O = g;
    return d = g = void 0, x = s, y = e.apply(O, $), y;
  }
  function k(s) {
    return x = s, p = setTimeout(N, i), C ? h(s) : y;
  }
  function W(s) {
    var $ = s - f, O = s - x, I = i - $;
    return T ? qt(I, j - O) : I;
  }
  function u(s) {
    var $ = s - f, O = s - x;
    return f === void 0 || $ >= i || $ < 0 || T && O >= j;
  }
  function N() {
    var s = fe();
    if (u(s))
      return F(s);
    p = setTimeout(N, W(s));
  }
  function F(s) {
    return p = void 0, V && d ? h(s) : (d = g = void 0, y);
  }
  function M() {
    p !== void 0 && clearTimeout(p), x = 0, d = f = g = p = void 0;
  }
  function D() {
    return p === void 0 ? y : F(fe());
  }
  function m() {
    var s = fe(), $ = u(s);
    if (d = arguments, g = this, f = s, $) {
      if (p === void 0)
        return k(f);
      if (T)
        return clearTimeout(p), p = setTimeout(N, i), h(f);
    }
    return p === void 0 && (p = setTimeout(N, i)), y;
  }
  return m.cancel = M, m.flush = D, m;
}
var Jt = Xt;
const Qt = /* @__PURE__ */ Qe(Jt), Yt = (e) => /^[+-]?\d+(\.\d+)?(px|em|rem|vw|vh|vmin|vmax|cm|mm|in|pt|pc|%)$/.test(e), Q = (e) => typeof e == "string" && Yt(e) ? e : e + "px", Zt = (e, i, c, d, g, j) => {
  const y = L(null), p = L(null), f = Ge(), x = L(), C = L(!1), T = L(""), V = L(""), h = b(() => e.mode === "multiple" || e.mode === "tags"), k = b(() => ({ label: "name", children: "children", pathName: "pathName", disabled: "disabled", ...d.props ?? {} })), W = Fe.value === "zh-CN" ? "，" : ", ", u = b({
    get: () => e.modelValue ? e.modelValue : void 0,
    set: (t) => {
      i("update:modelValue", t);
    }
  }), N = b({
    get: () => e.keyword || "",
    set: (t) => {
      i("update:keyword", t);
    }
  }), F = b({
    get: () => e.path,
    set: (t) => {
      i("update:path", t);
    }
  }), M = (t, l, n = []) => {
    for (let r = 0; r < t.length; r++) {
      const S = t[r], P = S[k.value.label];
      if (n.push(P), P === l) return n.join("/");
      if (S.children && S.children.length > 0) {
        const a = M(S[k.value.children], l, n);
        if (a) return a;
      }
      n.pop();
    }
    return null;
  }, D = b(() => {
    var n;
    let t = [], l = [];
    return g.value ? l = ((n = f.value) == null ? void 0 : n.lazyData) || [] : e.data && (l = qe(e.data, k.value.children) || []), l.length && (t = l.map((r) => ({
      ...r,
      [k.value.pathName]: r[k.value.pathName] ? r[k.value.pathName] : g.value ? r[k.value.label] : M(e.data, r[k.value.label])
    }))), t;
  }), m = L(""), s = L(!0), $ = () => {
    var t, l;
    if (!e.showTextStrict || !g.value || e.parentLinkChildrenStrictly) return !0;
    if (D.value.length === 0) return !1;
    if (h.value) {
      if (!u.value || !((t = u.value) != null && t.length)) return !0;
      const n = D.value.map((r) => r[c.value]);
      return (l = u.value) == null ? void 0 : l.some((r) => n.includes(r));
    } else
      return u.value ? !!D.value.find((r) => r[c.value] === u.value) : !0;
  };
  ve(
    () => [D.value, u.value],
    ([t, l]) => {
      if (s.value = !$(), !s.value)
        if (h.value)
          if (!l || !(l != null && l.length))
            m.value = "";
          else {
            const n = t == null ? void 0 : t.filter((r) => l.includes(r[c.value]));
            n != null && n.length ? m.value = n.map((r) => e.showFullPath ? r[k.value.pathName] : r[k.value.label]).join(W) : m.value = "";
          }
        else if (!l)
          m.value = "";
        else {
          const n = t == null ? void 0 : t.find((r) => r[c.value] === l);
          n ? m.value = e.showFullPath ? n[k.value.pathName] : n[k.value.label] : m.value = "";
        }
    },
    { immediate: !0 }
  );
  const O = b(() => ({
    height: e.poppverHeight ? Q(e.poppverHeight) : void 0,
    maxHeight: e.poppverMaxHeight ? Q(e.poppverMaxHeight) : void 0
  })), I = () => {
    i("visible-change", !0);
  }, Y = () => {
    var t;
    N.value = "", (t = f.value) == null || t.searchClear(), i("visible-change", !1);
  }, Z = Qt(() => {
    e.disabled || e.isView || (C.value ? C.value = !1 : (document.body.click(), C.value = !0));
  }, 200), A = async () => {
    var l, n;
    await q();
    let t = [];
    h.value || (u.value || u.value === 0) && (g.value && await ((l = f.value) == null ? void 0 : l.lazyDataNextTick(u.value)), t = D.value.filter((r) => r[c.value] === u.value)), h.value && (u.value || u.value.length) && (g.value && await ((n = f.value) == null ? void 0 : n.lazyDataNextTick(u.value)), t = D.value.filter((r) => u.value.includes(r[c.value]))), i("change", u.value, t);
  }, B = L(!1), K = async () => {
    C.value = !1, u.value = h.value ? [] : "", A(), B.value = !0;
  }, ee = async (t) => {
    var l, n, r, S, P, a;
    if (!B.value && h.value) {
      if (!g.value && !j.value) {
        let v = (n = (l = f.value) == null ? void 0 : l.treeRef) == null ? void 0 : n.getNode(t);
        v.checked = !1, (r = f.value) == null || r.childLinkParent(v, v.checked), (S = f.value) == null || S.parentLinkChild(v, v.checked);
      }
      if (g.value && e.parentLinkChildrenStrictly) {
        let v = D.value.find((w) => w[c.value] === t);
        if (v && ((P = f.value) != null && P.getGrandChildrenFn)) {
          const w = await f.value.getGrandChildrenFn(v);
          if (w && w.length) {
            const ne = w.map((ie) => ie[c.value]);
            u.value && ((a = u.value) != null && a.length) && (u.value = u.value.filter((ie) => !ne.includes(ie)));
          }
        }
      }
      A();
    }
  }, te = (t) => {
    if (!e.filter) return;
    const l = t && (t == null ? void 0 : t.trim());
    N.value = l, C.value || (C.value = !0);
  }, re = () => {
    var t, l;
    h.value || (C.value = !1, e.filter && ((l = (t = p.value) == null ? void 0 : t.$select) == null || l.focus()));
  }, z = () => y != null && y.value && y.value.offsetWidth ? y.value.offsetWidth + "px" : "100%", U = () => {
    q(() => {
      T.value = e.width ? Q(e.width) : z(), V.value = e.poppverWidth ? Q(e.poppverWidth) : z();
    });
  }, le = (t) => {
    var l;
    B.value && (B.value = !1), (l = p.value) == null || l.toChangeMaxCount(!0), i("focus", t);
  }, _ = (t) => {
    i("blur", t);
  };
  ve(
    () => C.value,
    (t) => {
      var n, r;
      const l = (n = y.value) == null ? void 0 : n.querySelector(".ant-select-arrow>.das-icon");
      t ? l.style.transform = "rotate(180deg)" : l.style.transform = "", (r = p.value) == null || r.toChangeMaxCount(t), q(() => {
        var S, P, a;
        (a = (P = (S = o(x)) == null ? void 0 : S.popperRef) == null ? void 0 : P.popperInstanceRef) == null || a.update();
      });
    }
  );
  const E = L(null), ae = () => new Promise((t) => {
    const l = () => {
      var r;
      const n = (r = y.value) == null ? void 0 : r.querySelector(".ant-select-selector");
      if (n) return t(n);
      setTimeout(() => l(), 50);
    };
    l();
  }), oe = (t) => {
    u.value = t;
  };
  return De(async () => {
    U();
    const t = await ae();
    t && (E.value = _e((l) => {
      q(() => {
        var n, r, S;
        (S = (r = (n = o(x)) == null ? void 0 : n.popperRef) == null ? void 0 : r.popperInstanceRef) == null || S.update(), U();
      });
    }, t));
  }), xe(() => {
    var t;
    (t = E.value) == null || t.disconnect();
  }), {
    innerValue: u,
    popoverRef: x,
    dasTreeSelectRef: y,
    $tree: f,
    $select: p,
    innerKeyword: N,
    realWidth: T,
    realPoppverWidth: V,
    poppverContentStyle: O,
    treePath: F,
    treeProps: k,
    selectOptions: D,
    currentLabel: m,
    poppverShow: I,
    poppverHide: Y,
    treeChange: A,
    nodeChange: re,
    deselect: ee,
    selectSearch: te,
    selectClear: K,
    popoverVisible: C,
    selectMouseDown: Z,
    resetDefaultVal: oe,
    focus: le,
    blur: _,
    checkselectOptionsData: $,
    textHide: s,
    showCheckbox: h
  };
}, Kt = {
  key: 1,
  class: "das-tree-select__selct-text-content"
}, er = {
  name: "DasTreeSelect",
  inheritAttrs: !1
}, tr = /* @__PURE__ */ Le({
  ...er,
  props: {
    data: { default: () => [] },
    mode: { default: void 0 },
    modelValue: { default: void 0 },
    showFullPath: { type: Boolean },
    showSelectDom: { type: Boolean, default: !0 },
    size: { default: "middle" },
    borderType: { default: "underline" },
    placeholder: { default: "" },
    disabled: { type: Boolean, default: !1 },
    isView: { type: Boolean },
    errorMode: { default: "default" },
    error: { type: Boolean },
    errorLabel: {},
    selectAttrs: {},
    width: {},
    poppverWidth: {},
    poppverHeight: { default: 320 },
    poppverMaxHeight: {},
    filter: { type: Boolean, default: !0 },
    keyword: { default: "" },
    defaultExpandAll: { type: Boolean, default: !0 },
    checkStrictly: { type: Boolean, default: void 0 },
    path: {},
    load: {},
    search: {},
    parentLinkChildrenStrictly: { type: Boolean },
    getGrandChildren: {},
    childLinkParentStrictly: { type: Boolean },
    showTextStrict: { type: Boolean, default: !0 }
  },
  emits: ["update:modelValue", "update:path", "update:keyword", "change", "focus", "blur", "visible-change"],
  setup(e, { expose: i, emit: c }) {
    const d = e, g = c, j = Ne(), y = b(() => d.placeholder || Ie("请选择")), p = b(() => j.nodeKey ?? j["node-key"] ?? "id"), f = b(() => d.load ?? void 0), x = b(() => d.search ?? void 0), C = b(() => d.getGrandChildren ?? void 0), T = b(() => j.lazy ?? !!f.value), V = b(() => T.value ? !1 : d.defaultExpandAll), h = b(() => d.selectAttrs ? d.selectAttrs : {}), k = b(() => !!(d.disabled || d.isView)), W = b(() => d.parentLinkChildrenStrictly || d.childLinkParentStrictly ? !0 : !!(d.checkStrictly ?? T.value)), u = {
      fn: () => {
        console.log("click-out"), E.value = !1;
      }
    }, N = (a) => {
      a.key === "Enter" && a.stopPropagation();
    }, {
      dasTreeSelectRef: F,
      $tree: M,
      $select: D,
      innerValue: m,
      innerKeyword: s,
      realWidth: $,
      poppverContentStyle: O,
      realPoppverWidth: I,
      treeChange: Y,
      nodeChange: Z,
      treePath: A,
      treeProps: B,
      selectOptions: K,
      currentLabel: ee,
      popoverRef: te,
      deselect: re,
      poppverShow: z,
      poppverHide: U,
      selectSearch: le,
      selectMouseDown: _,
      popoverVisible: E,
      resetDefaultVal: ae,
      selectClear: oe,
      focus: t,
      blur: l,
      textHide: n,
      checkselectOptionsData: r,
      showCheckbox: S
    } = Zt(d, g, p, j, T, W), P = b(() => h.value.isTooltip ? h.value.isTooltip && r() : h.value["is-tooltip"] ? h.value["is-tooltip"] && r() : r());
    return i({
      /** das-select */
      $select: D,
      /** das-tree */
      $tree: M,
      /** 设置默认值，当默认值为动态设置时使用(兼容老版本) */
      resetDefaultVal: ae
    }), (a, v) => pe((ue(), se(
      "div",
      {
        ref_key: "dasTreeSelectRef",
        ref: F,
        class: Pe({ "das-ui-tree-select": !0, "is-lazy": T.value, "text-hide": o(n), "show-select": a.showSelectDom }),
        style: he({ width: o($) })
      },
      [
        Oe(` 在使用 2.x 版本的element-UI时，里面的Popover气泡卡片组件，有自带的自动适应位置的功能；在 element plus 中，直接使用，已经无法自适应气泡弹窗的位置了；需要自行配置:fallback-placements="['bottom', 'top', 'right', 'left']" `),
        X(o(We), {
          ref_key: "popoverRef",
          ref: te,
          visible: o(E),
          "popper-class": "das-tree-select-popper",
          placement: "bottom-start",
          "show-arrow": !1,
          width: o(I),
          disabled: k.value,
          "fallback-placements": ["bottom", "top", "right", "left"],
          offset: 3,
          onShow: o(z),
          onHide: o(U)
        }, {
          reference: R(() => [
            a.showSelectDom ? (ue(), se(
              "div",
              {
                key: 0,
                class: "das-tree-select__select-content",
                onMousedown: v[1] || (v[1] = de(
                  //@ts-ignore
                  (...w) => o(_) && o(_)(...w),
                  ["stop"]
                ))
              },
              [
                X(o(Ae), me({
                  ref_key: "$select",
                  ref: D,
                  class: { "is-active": o(E) },
                  dropdownClassName: "das-tree-select-dropdown",
                  modelValue: o(m),
                  "onUpdate:modelValue": v[0] || (v[0] = (w) => ce(m) ? m.value = w : null)
                }, h.value, {
                  searchValue: o(s),
                  options: o(K),
                  disabled: a.disabled,
                  isView: a.isView,
                  size: a.size,
                  mode: a.mode,
                  borderType: a.borderType,
                  placeholder: y.value,
                  "show-search": a.filter,
                  filterOption: a.filter,
                  width: o($),
                  fieldNames: {
                    value: p.value,
                    label: a.showFullPath ? o(B).pathName : o(B).label
                  },
                  isTooltip: P.value,
                  errorMode: a.errorMode,
                  error: a.error,
                  errorLabel: a.errorLabel,
                  autoClearSearchValue: !1,
                  isExterControlCount: "",
                  onDeselect: o(re),
                  onSearch: o(le),
                  onInputKeyDown: N,
                  onFocus: o(t),
                  onBlur: o(l),
                  onClear: o(oe)
                }), {
                  suffixIcon: R(() => [
                    X(o(ze), {
                      size: "medium",
                      style: { transition: "transform 0.5s" },
                      iconColor: o(E) ? "#5582F3" : "currentColor"
                    }, null, 8, ["iconColor"])
                  ]),
                  _: 1
                  /* STABLE */
                }, 16, ["class", "modelValue", "searchValue", "options", "disabled", "isView", "size", "mode", "borderType", "placeholder", "show-search", "filterOption", "width", "fieldNames", "isTooltip", "errorMode", "error", "errorLabel", "onDeselect", "onSearch", "onFocus", "onBlur", "onClear"])
              ],
              32
              /* NEED_HYDRATION */
            )) : pe((ue(), se("div", Kt, [
              Me(
                Be(o(ee)),
                1
                /* TEXT */
              )
            ])), [
              [o(Ue)]
            ])
          ]),
          default: R(() => [
            Ve(
              "div",
              {
                class: "das-tree-select__tree-content",
                style: he(o(O)),
                onClick: v[4] || (v[4] = de(() => {
                }, ["stop"])),
                onMousedown: v[5] || (v[5] = de(() => {
                }, ["stop"]))
              },
              [
                X(o(He), me({
                  ref_key: "$tree",
                  ref: M
                }, o(j), {
                  data: a.data,
                  modelValue: o(m),
                  "onUpdate:modelValue": v[2] || (v[2] = (w) => ce(m) ? m.value = w : null),
                  "filter-value": o(s),
                  "onUpdate:filterValue": v[3] || (v[3] = (w) => ce(s) ? s.value = w : null),
                  "show-checkbox": o(S),
                  "is-filter": a.filter,
                  "show-filter-input": !1,
                  "filter-mode": "input",
                  lazy: T.value,
                  load: f.value,
                  search: x.value,
                  path: o(A),
                  "default-expand-all": V.value,
                  "check-strictly": T.value ? !0 : W.value,
                  parentLinkChildrenStrictly: a.parentLinkChildrenStrictly,
                  getGrandChildren: C.value,
                  childLinkParentStrictly: a.childLinkParentStrictly,
                  onChange: o(Y),
                  onNodeChange: o(Z)
                }), Re({
                  default: R(({ node: w, data: ne }) => [
                    G(a.$slots, "default", {
                      node: w,
                      data: ne
                    })
                  ]),
                  _: 2
                  /* DYNAMIC */
                }, [
                  a.$slots.prefix ? {
                    name: "prefix",
                    fn: R(() => [
                      G(a.$slots, "prefix")
                    ]),
                    key: "0"
                  } : void 0,
                  a.$slots.suffix ? {
                    name: "suffix",
                    fn: R(() => [
                      G(a.$slots, "suffix")
                    ]),
                    key: "1"
                  } : void 0,
                  a.$slots.prepend ? {
                    name: "prepend",
                    fn: R(() => [
                      G(a.$slots, "prepend")
                    ]),
                    key: "2"
                  } : void 0,
                  a.$slots.append ? {
                    name: "append",
                    fn: R(() => [
                      G(a.$slots, "append")
                    ]),
                    key: "3"
                  } : void 0
                ]), 1040, ["data", "modelValue", "filter-value", "show-checkbox", "is-filter", "lazy", "load", "search", "path", "default-expand-all", "check-strictly", "parentLinkChildrenStrictly", "getGrandChildren", "childLinkParentStrictly", "onChange", "onNodeChange"])
              ],
              36
              /* STYLE, NEED_HYDRATION */
            )
          ]),
          _: 3
          /* FORWARDED */
        }, 8, ["visible", "width", "disabled", "onShow", "onHide"])
      ],
      6
      /* CLASS, STYLE */
    )), [
      [o(Xe), u]
    ]);
  }
}), rr = {
  "--das-ui-tree-select-keyword-input-active": "#5582F3",
  "--das-ui-tree-select-search-no-data": "#999999"
};
Je(rr);
const mr = Ee(tr), br = () => L(null), gr = { title: "tree-select 树选择" };
export {
  mr as DasTreeSelect,
  br as DasTreeSelectRef,
  gr as default
};
