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