import { defineComponent as xe, useSlots as ke, ref as I, shallowRef as Ve, computed as u, onMounted as Be, watch as Ne, unref as l, createVNode as y, openBlock as g, createBlock as w, withCtx as f, createElementVNode as O, toDisplayString as A, mergeProps as Ie, h as Ae, createSlots as De, normalizeClass as C, createCommentVNode as E, resolveDynamicComponent as U, createElementBlock as _e, withModifiers as Me, renderList as Pe, renderSlot as Re, normalizeProps as Le, guardReactiveProps as qe } from "vue";
import { Form as Te, Select as ze, Flex as j, Checkbox as K, Spin as Ee } from "@aplus-frontend/antdv";
import { debounce as Ue, isFunction as $, isNil as H, isArray as W, omit as G, isNumber as je } from "lodash-unified";
import "../../config-provider/index.mjs";
import "../../render/index.mjs";
import "../../utils/index.mjs";
import { useDevWarning as Ke } from "../../utils/warning.mjs";
import "../../hooks/index.mjs";
import { useDefaultPlaceholder as $e } from "../hooks/use-default-placeholder.mjs";
import { useReadModeRender as He } from "../hooks/use-read-mode-render.mjs";
import We from "../style/index.mjs";
import Ge from "./use-select-options.mjs";
import { isDef as D } from "../../utils/is.mjs";
import { useNamespace as J } from "../../config-provider/hooks/use-namespace.mjs";
import { useLocale as Je } from "../../config-provider/hooks/use-locale.mjs";
import { useControllableValue as Qe } from "../../hooks/useControllableValue.mjs";
import Xe from "../../render/render.mjs";
const Ye = ["onClick"], vt = /* @__PURE__ */ xe({
  name: "ApFieldSelect",
  __name: "index",
  props: {
    mode: {
      default: "edit"
    },
    class: {},
    style: {},
    emptyText: {
      default: "--"
    },
    customReadMode: {},
    defaultValue: {},
    notFoundContent: {},
    suffixIcon: {},
    itemIcon: {},
    size: {},
    bordered: {
      type: Boolean,
      default: void 0
    },
    showArrow: {
      type: Boolean,
      default: void 0
    },
    variant: {},
    maxCount: {},
    transitionName: {},
    choiceTransitionName: {},
    popupClassName: {},
    dropdownClassName: {},
    placement: {},
    status: {},
    children: {},
    listHeight: {},
    onMouseenter: {},
    onMouseleave: {},
    tabindex: {},
    onClick: {},
    onFocus: {},
    onBlur: {},
    onChange: {},
    onKeydown: {},
    onKeyup: {},
    onMousedown: {},
    onSelect: {},
    open: {
      type: Boolean,
      default: void 0
    },
    animation: {},
    direction: {},
    disabled: {
      type: Boolean,
      default: void 0
    },
    prefixCls: {},
    id: {},
    autofocus: {
      type: Boolean,
      default: void 0
    },
    getPopupContainer: {},
    virtual: {
      type: Boolean,
      default: void 0
    },
    dropdownMatchSelectWidth: {},
    placeholder: {},
    loading: {
      type: Boolean,
      default: void 0
    },
    prefix: {},
    clearIcon: {},
    allowClear: {
      type: Boolean,
      default: !0
    },
    fieldNames: {},
    inputValue: {},
    searchValue: {},
    autoClearSearchValue: {
      type: Boolean,
      default: void 0
    },
    onDeselect: {},
    onActive: {},
    filterOption: {},
    filterSort: {},
    optionFilterProp: {},
    optionLabelProp: {},
    options: {},
    defaultActiveFirstOption: {
      type: Boolean,
      default: void 0
    },
    listItemHeight: {},
    onClickOption: {},
    menuItemSelectedIcon: {},
    labelInValue: {
      type: Boolean,
      default: void 0
    },
    showSearch: {
      type: Boolean,
      default: void 0
    },
    tagRender: {},
    optionLabelRender: {},
    onClear: {},
    defaultOpen: {
      type: Boolean,
      default: void 0
    },
    onDropdownVisibleChange: {},
    maxTagTextLength: {},
    maxTagCount: {},
    maxTagPlaceholder: {},
    tokenSeparators: {},
    removeIcon: {},
    dropdownStyle: {},
    dropdownRender: {},
    dropdownAlign: {},
    showAction: {},
    onPopupScroll: {},
    onInputKeyDown: {},
    request: {},
    multiple: {
      type: Boolean,
      default: void 0
    },
    value: {},
    "onUpdate:value": {},
    searchMode: {
      default: "filter"
    },
    refetchOnFocus: {
      type: Boolean,
      default: !1
    },
    fetchOnFocus: {
      type: Boolean,
      default: void 0
    },
    fetchOnOpen: {
      type: Boolean
    },
    lazy: {
      type: [Boolean, String],
      default: !1
    },
    addAll: {
      type: [Boolean, Number],
      default: 5
    },
    params: {},
    searchDelay: {
      default: 300
    },
    allowOnlySelect: {
      type: Boolean,
      default: !0
    }
  },
  emits: ["update:value"],
  setup(_, {
    expose: Q,
    emit: X
  }) {
    let i = 0;
    const S = ke(), t = _, {
      deprecated: Y
    } = Ke("ApFieldSelect");
    Y(!D(t.refetchOnFocus), "refetchOnFocus", "fetchOnFocus");
    const {
      b: x
    } = J("field-select"), {
      b: p
    } = J("field-select-popup-option"), Z = We("field"), ee = Te.useInjectFormItemContext(), M = I(!1), P = I(!1), {
      t: R
    } = Je(), te = Ue((e) => {
      if (!$(t.request))
        return;
      i += 1;
      const n = i;
      F([]), s.value = !0, t.request(e).then((o) => {
        i === n && (F(o), s.value = !1);
      });
    }, t.searchDelay), ne = (e, n) => {
      const o = t.fieldNames?.label || "label";
      return typeof n?.[o] == "string" && n[o].toLowerCase().indexOf(e.toLowerCase()) > -1;
    }, {
      options: c,
      updateOptions: F
    } = Ge(t), k = Ve(), s = I(t?.loading || !1), oe = X, le = u(() => t.fetchOnFocus ?? t.refetchOnFocus), ae = u(() => {
      if (t.multiple)
        return t.tokenSeparators ? "tags" : "multiple";
    });
    async function d(e = !0) {
      if (!$(t.request))
        return;
      s.value = !0, i += 1;
      const n = i;
      try {
        e && F([]);
        const o = await t.request(void 0, t.params);
        if (n !== i)
          return;
        F(o);
      } finally {
        s.value = !1;
      }
    }
    function re(e) {
      !M.value && i === 0 && t.lazy === "first-focus" ? d() : le.value && !s.value && d(!0), t.onFocus?.(e), M.value = !0;
    }
    function ue(e) {
      if (!e) {
        t.onDropdownVisibleChange?.(e);
        return;
      }
      !P.value && i === 0 && t.lazy === "first-open" ? d() : t.fetchOnOpen && !s.value && d(!0), t.onDropdownVisibleChange?.(e), P.value = !0;
    }
    function V(e) {
      se(e), ee?.onFieldChange();
    }
    Be(() => {
      if (!t.lazy) {
        d();
        return;
      }
    }), Ne(() => t.params, () => {
      d();
    }, {
      deep: !0
    });
    const {
      value: m,
      updateValue: se
    } = Qe(t, oe), ie = $e("Select", t), L = u(() => {
      const e = l(m);
      if (H(e))
        return t.emptyText;
      const n = t.fieldNames?.label || "label", o = t.fieldNames?.value || "value";
      return W(e) ? e.map((a) => l(c).find((v) => v[o] === a)?.[n]).filter(Boolean).join("、") || t.emptyText : l(c).find((a) => a[o] === e)?.[n] || t.emptyText;
    }), {
      renderer: ce
    } = He(t, L), de = u(() => {
      const e = !!t?.showSearch, n = t.searchMode;
      let o = {};
      return e && (o = n === "request" ? {
        defaultActiveFirstOption: !1,
        onSearch: te,
        notFoundContent: s.value ? void 0 : null,
        filterOption: !1,
        suffixIcon: null
      } : {
        filterOption: H(t?.filterOption) ? ne : t?.filterOption
      }), {
        ...G(t, ["mode", "value", "onUpdate:value", "onSearch", "request", "searchMode", "onFocus", "onBeforeSelect", "onDropdownVisibleChange"]),
        placeholder: l(ie),
        ...o
      };
    }), fe = u(() => c.value.map((e) => e[t.fieldNames?.value || "value"])), B = u(() => c.value.filter((e) => !e.disabled).map((e) => e[t.fieldNames?.value || "value"])), q = u(() => !t.multiple || D(t.maxCount) ? !1 : je(t.addAll) ? fe.value.length >= t.addAll : !!t.addAll), pe = u(() => t.multiple && t.allowOnlySelect && (!D(t.maxCount) || t.maxCount >= 1));
    function me() {
      k.value?.focus();
    }
    function he() {
      k.value?.blur();
    }
    function h(e, n) {
      const o = t.fieldNames?.[n] ?? n;
      return o === "groupLabel" ? e[o] ?? e.label : e[o];
    }
    function ve(e) {
      const n = T(e);
      V(t.multiple ? [n] : n), t.onChange?.([n], [e]);
    }
    function ye(e) {
      return S.option ? y("div", {
        class: p("custom")
      }, [S.option(e)]) : y("span", {
        class: p("label")
      }, [h(e, e.options?.length ? "groupLabel" : "label")]);
    }
    const ge = u(() => {
      const e = l(m) || [], n = B.value;
      return n.length > 0 && n.every((o) => b(e, o));
    }), Ce = u(() => {
      const e = l(m) || [], n = B.value, o = n.filter((r) => b(e, r)).length;
      return o > 0 && o < n.length;
    });
    function N(e, n) {
      return (e.value ?? e) === n;
    }
    const be = (e) => {
      const n = l(m), o = h(e, "value");
      return W(n) ? !!n.find((r) => N(r, o)) : !1;
    };
    function b(e, n) {
      return e.find((o) => N(o, n)) != null;
    }
    function we(e, n) {
      return e.findIndex((o) => N(o, n));
    }
    function T(e, n) {
      const o = h(e, "label"), r = n ?? h(e, "value");
      return t.labelInValue ? {
        label: typeof o == "function" ? o() : o,
        originLabel: o,
        value: r,
        key: e?.key ?? r
      } : r;
    }
    function Fe() {
      const e = [...l(m) || []], n = B.value;
      n.length > 0 && n.every((a) => b(e, a)) ? n.forEach((a) => {
        const v = we(e, a);
        ~v && e.splice(v, 1);
      }) : n.forEach((a) => {
        if (b(e, a))
          return;
        const v = c.value.find((Se) => h(Se, "value") === a), Oe = T(v, a);
        e.push(Oe);
      });
      const r = c.value.filter((a) => b(e, h(a, "value")));
      V([...e]), t.onChange?.([...e], r);
    }
    function z(e) {
      e.pointerId === -1 && e.stopPropagation();
    }
    return Q({
      focus: me,
      blur: he,
      request: d
    }), (e, n) => _.mode === "read" ? (g(), w(l(Xe), {
      key: 0,
      renderer: l(ce)
    }, {
      default: f(() => [O("span", null, A(L.value), 1)]),
      _: 1
    }, 8, ["renderer"])) : (g(), w(l(ze), Ie({
      key: 1,
      ref_key: "selectRef",
      ref: k,
      "allow-clear": ""
    }, de.value, {
      mode: ae.value,
      value: l(m),
      options: l(c),
      loading: s.value,
      "menu-item-selected-icon": Ae("span"),
      "popup-class-name": `${l(x)("popup")} ${l(Z)}`,
      "onUpdate:value": V,
      onFocus: re,
      onDropdownVisibleChange: ue
    }), De({
      option: f((o) => [O("div", {
        class: C(l(p)("outter"))
      }, [y(l(j), {
        justify: "space-between",
        align: "center",
        class: C(l(p)("wrapper")),
        gap: 8
      }, {
        default: f(() => [y(l(j), {
          gap: 8,
          align: "center",
          class: C(l(p)("content-wrapper"))
        }, {
          default: f(() => [q.value ? (g(), w(l(K), {
            key: 0,
            checked: be(o),
            onClick: z
          }, null, 8, ["checked"])) : E("", !0), (g(), w(U(ye(o))))]),
          _: 2
        }, 1032, ["class"]), pe.value && !o?.disabled ? (g(), _e("span", {
          key: 0,
          class: C(l(p)("action")),
          onClick: Me(() => ve(o), ["stop"])
        }, A(l(R)("ap.field.chooseOnly")), 11, Ye)) : E("", !0)]),
        _: 2
      }, 1032, ["class"])], 2)]),
      _: 2
    }, [Pe(l(G)(S, ["option"]), (o, r) => ({
      name: r,
      fn: f((a) => [Re(e.$slots, r, Le(qe(a || {})))])
    })), s.value ? {
      name: "notFoundContent",
      fn: f(() => [y(l(Ee), {
        size: "small"
      })]),
      key: "0"
    } : void 0, q.value ? {
      name: "dropdownRender",
      fn: f(({
        menuNode: o
      }) => [O("div", {
        tabindex: -1,
        class: C(l(x)("popup-all-option")),
        onClick: Fe
      }, [y(l(K), {
        checked: ge.value,
        indeterminate: Ce.value,
        onClick: z
      }, null, 8, ["checked", "indeterminate"]), O("span", {
        class: C(l(x)("popup-all-option-label"))
      }, A(l(R)("ap.field.selectAll")), 3)], 2), (g(), w(U(o)))]),
      key: "1"
    } : void 0]), 1040, ["mode", "value", "options", "loading", "menu-item-selected-icon", "popup-class-name"]));
  }
});
export {
  vt as default
};
