import { defineComponent as xe, useSlots as ke, ref as N, shallowRef as Ve, computed as s, onMounted as Be, watch as Ne, unref as l, createVNode as v, openBlock as y, createElementBlock as z, toDisplayString as I, createBlock as F, mergeProps as Ie, h as Ae, createSlots as _e, withCtx as g, createElementVNode as A, normalizeClass as C, createCommentVNode as E, resolveDynamicComponent as U, withModifiers as De, renderList as Pe, renderSlot as Me, normalizeProps as Le, guardReactiveProps as qe } from "vue";
import { Form as Te, Select as Re, Flex as j, Checkbox as K, Spin as ze } from "@aplus-frontend/antdv";
import { debounce as Ee, isFunction as $, isNil as H, isArray as W, omit as G, isNumber as Ue } from "lodash-unified";
import "../../config-provider/index.mjs";
import "../../utils/index.mjs";
import { useDevWarning as je } from "../../utils/warning.mjs";
import "../../hooks/index.mjs";
import { useDefaultPlaceholder as Ke } from "../hooks/use-default-placeholder.mjs";
import $e from "../style/index.mjs";
import He from "./use-select-options.mjs";
import { isDef as _ } from "../../utils/is.mjs";
import { useNamespace as J } from "../../config-provider/hooks/use-namespace.mjs";
import { useLocale as We } from "../../config-provider/hooks/use-locale.mjs";
import { useControllableValue as Ge } from "../../hooks/useControllableValue.mjs";
const Je = {
  key: 0
}, Qe = ["onClick"], dt = /* @__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(D, {
    expose: Q,
    emit: X
  }) {
    let i = 0;
    const O = ke(), t = D, {
      deprecated: Y
    } = je("ApFieldSelect");
    Y(!_(t.refetchOnFocus), "refetchOnFocus", "fetchOnFocus");
    const {
      b: S
    } = J("field-select"), {
      b: f
    } = J("field-select-popup-option"), Z = $e("field"), ee = Te.useInjectFormItemContext(), P = N(!1), M = N(!1), {
      t: L
    } = We(), te = Ee((e) => {
      if (!$(t.request))
        return;
      i += 1;
      const n = i;
      w([]), r.value = !0, t.request(e).then((o) => {
        i === n && (w(o), r.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: w
    } = He(t), x = Ve(), r = N(t?.loading || !1), oe = X, le = s(() => t.fetchOnFocus ?? t.refetchOnFocus), ae = s(() => {
      if (t.multiple)
        return t.tokenSeparators ? "tags" : "multiple";
    });
    async function d(e = !0) {
      if (!$(t.request))
        return;
      r.value = !0, i += 1;
      const n = i;
      try {
        e && w([]);
        const o = await t.request(void 0, t.params);
        if (n !== i)
          return;
        w(o);
      } finally {
        r.value = !1;
      }
    }
    function ue(e) {
      !P.value && i === 0 && t.lazy === "first-focus" ? d() : le.value && !r.value && d(!0), t.onFocus?.(e), P.value = !0;
    }
    function se(e) {
      if (!e) {
        t.onDropdownVisibleChange?.(e);
        return;
      }
      !M.value && i === 0 && t.lazy === "first-open" ? d() : t.fetchOnOpen && !r.value && d(!0), t.onDropdownVisibleChange?.(e), M.value = !0;
    }
    function k(e) {
      re(e), ee?.onFieldChange();
    }
    Be(() => {
      if (!t.lazy) {
        d();
        return;
      }
    }), Ne(() => t.params, () => {
      d();
    }, {
      deep: !0
    });
    const {
      value: p,
      updateValue: re
    } = Ge(t, oe), ie = Ke("Select", t), ce = s(() => {
      const e = l(p);
      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((h) => h[o] === a)?.[n]).filter(Boolean).join("、") || t.emptyText : l(c).find((a) => a[o] === e)?.[n] || t.emptyText;
    }), de = s(() => {
      const e = !!t?.showSearch, n = t.searchMode;
      let o = {};
      return e && (o = n === "request" ? {
        defaultActiveFirstOption: !1,
        onSearch: te,
        notFoundContent: r.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 = s(() => c.value.map((e) => e[t.fieldNames?.value || "value"])), V = s(() => c.value.filter((e) => !e.disabled).map((e) => e[t.fieldNames?.value || "value"])), q = s(() => !t.multiple || _(t.maxCount) ? !1 : Ue(t.addAll) ? fe.value.length >= t.addAll : !!t.addAll), pe = s(() => t.multiple && t.allowOnlySelect && (!_(t.maxCount) || t.maxCount >= 1));
    function me() {
      x.value?.focus();
    }
    function he() {
      x.value?.blur();
    }
    function m(e, n) {
      const o = t.fieldNames?.[n] ?? n;
      return o === "groupLabel" ? e[o] ?? e.label : e[o];
    }
    function ve(e) {
      const n = T(e);
      k(t.multiple ? [n] : n), t.onChange?.([n], [e]);
    }
    function ye(e) {
      return O.option ? v("div", {
        class: f("custom")
      }, [O.option(e)]) : v("span", {
        class: f("label")
      }, [m(e, e.options?.length ? "groupLabel" : "label")]);
    }
    const ge = s(() => {
      const e = l(p) || [], n = V.value;
      return n.length > 0 && n.every((o) => b(e, o));
    }), Ce = s(() => {
      const e = l(p) || [], n = V.value, o = n.filter((u) => b(e, u)).length;
      return o > 0 && o < n.length;
    });
    function B(e, n) {
      return (e.value ?? e) === n;
    }
    const be = (e) => {
      const n = l(p), o = m(e, "value");
      return W(n) ? !!n.find((u) => B(u, o)) : !1;
    };
    function b(e, n) {
      return e.find((o) => B(o, n)) != null;
    }
    function we(e, n) {
      return e.findIndex((o) => B(o, n));
    }
    function T(e, n) {
      const o = m(e, "label"), u = n ?? m(e, "value");
      return t.labelInValue ? {
        label: typeof o == "function" ? o() : o,
        originLabel: o,
        value: u,
        key: e?.key ?? u
      } : u;
    }
    function Fe() {
      const e = [...l(p) || []], n = V.value;
      n.length > 0 && n.every((a) => b(e, a)) ? n.forEach((a) => {
        const h = we(e, a);
        ~h && e.splice(h, 1);
      }) : n.forEach((a) => {
        if (b(e, a))
          return;
        const h = c.value.find((Se) => m(Se, "value") === a), Oe = T(h, a);
        e.push(Oe);
      });
      const u = c.value.filter((a) => b(e, m(a, "value")));
      k([...e]), t.onChange?.([...e], u);
    }
    function R(e) {
      e.pointerId === -1 && e.stopPropagation();
    }
    return Q({
      focus: me,
      blur: he,
      request: d
    }), (e, n) => D.mode === "read" ? (y(), z("span", Je, I(ce.value), 1)) : (y(), F(l(Re), Ie({
      key: 1,
      ref_key: "selectRef",
      ref: x,
      "allow-clear": ""
    }, de.value, {
      mode: ae.value,
      value: l(p),
      options: l(c),
      loading: r.value,
      "menu-item-selected-icon": Ae("span"),
      "popup-class-name": `${l(S)("popup")} ${l(Z)}`,
      "onUpdate:value": k,
      onFocus: ue,
      onDropdownVisibleChange: se
    }), _e({
      option: g((o) => [A("div", {
        class: C(l(f)("outter"))
      }, [v(l(j), {
        justify: "space-between",
        align: "center",
        class: C(l(f)("wrapper")),
        gap: 8
      }, {
        default: g(() => [v(l(j), {
          gap: 8,
          align: "center",
          class: C(l(f)("content-wrapper"))
        }, {
          default: g(() => [q.value ? (y(), F(l(K), {
            key: 0,
            checked: be(o),
            onClick: R
          }, null, 8, ["checked"])) : E("", !0), (y(), F(U(ye(o))))]),
          _: 2
        }, 1032, ["class"]), pe.value && !o?.disabled ? (y(), z("span", {
          key: 0,
          class: C(l(f)("action")),
          onClick: De(() => ve(o), ["stop"])
        }, I(l(L)("ap.field.chooseOnly")), 11, Qe)) : E("", !0)]),
        _: 2
      }, 1032, ["class"])], 2)]),
      _: 2
    }, [Pe(l(G)(O, ["option"]), (o, u) => ({
      name: u,
      fn: g((a) => [Me(e.$slots, u, Le(qe(a || {})))])
    })), r.value ? {
      name: "notFoundContent",
      fn: g(() => [v(l(ze), {
        size: "small"
      })]),
      key: "0"
    } : void 0, q.value ? {
      name: "dropdownRender",
      fn: g(({
        menuNode: o
      }) => [A("div", {
        tabindex: -1,
        class: C(l(S)("popup-all-option")),
        onClick: Fe
      }, [v(l(K), {
        checked: ge.value,
        indeterminate: Ce.value,
        onClick: R
      }, null, 8, ["checked", "indeterminate"]), A("span", {
        class: C(l(S)("popup-all-option-label"))
      }, I(l(L)("ap.field.selectAll")), 3)], 2), (y(), F(U(o)))]),
      key: "1"
    } : void 0]), 1040, ["mode", "value", "options", "loading", "menu-item-selected-icon", "popup-class-name"]));
  }
});
export {
  dt as default
};
