import { j as t } from "../../../node_modules/react/jsx-runtime.mjs";
import { useState as v, useRef as W, useEffect as M } from "react";
import { BitzPickDrawer as V } from "../../BitzDrawer/index.mjs";
import k from "../../../node_modules/@better-scroll/core/dist/core.esm.mjs";
import R from "../../../node_modules/@better-scroll/wheel/dist/wheel.esm.mjs";
import s from "./index.module.less.mjs";
import { useImmer as S } from "../../../node_modules/use-immer/dist/use-immer.module.mjs";
import N from "../../../node_modules/antd/es/tag/index.mjs";
import C from "../../../node_modules/antd/es/input/index.mjs";
import F from "../../../node_modules/antd/es/space/index.mjs";
import I from "../../../node_modules/@ant-design/icons/es/icons/DownOutlined.mjs";
k.use(R);
const { Search: H } = C, K = ({ ...n }) => {
  const o = n.mode === "multiple" || n.mode === "tags", u = n.options || [], j = n.filterOption, [b, g] = v(() => {
    var l;
    const e = (l = n.options) == null ? void 0 : l.findIndex((r) => r.value === n.defaultValue);
    return e === -1 ? 0 : e;
  }), [c, x] = S(() => {
    var e, l;
    return o ? n.defaultValue : ((l = (e = n.options) == null ? void 0 : e.find((r) => r.value === n.defaultValue)) == null ? void 0 : l.value) || "";
  }), w = W(null), [p, m] = v(!1), [z, D] = v(""), [d, h] = S([]), O = () => {
    m(!1);
  }, T = () => {
    const e = n.onChange;
    if (o)
      x(d.map((l) => l.value)), e == null || e(d.map((l) => l.value));
    else {
      const l = u[b || 0];
      x(l.label), e == null || e(l.value, l);
    }
    m(!1);
  };
  M(() => {
    if (w.current)
      if (p) {
        let e = new k("." + s.wheel, {
          click: !0,
          probeType: 3,
          preventDefaultException: {
            className: /wheel-item/
          },
          wheel: {
            selectedIndex: b,
            wheelWrapperClass: s["wheel-scroll"],
            wheelItemClass: s["wheel-item"],
            wheelDisabledItemClass: s["wheel-disabled-item"]
          },
          useTransition: !1
        });
        if (e.on("scrollEnd", () => {
          g(e.getSelectedIndex());
        }), o) {
          const l = u.filter((r) => c == null ? void 0 : c.find((a) => a === r.value));
          h(l);
        }
      } else
        h((e) => {
          e.splice(0);
        });
  }, [p]);
  function $() {
    return o ? t.jsxs("div", { className: s.textarea, onClick: () => m(!0), children: [t.jsx("div", { children: c == null ? void 0 : c.map((e, l) => {
      var r;
      return t.jsx(N, { bordered: !1, closable: !0, onClose: (a) => {
        a.preventDefault();
        const i = c.findIndex((f) => f === e);
        x((f) => {
          f.splice(i, 1);
        });
      }, children: (r = u.find((a) => e === a.value)) == null ? void 0 : r.label }, l);
    }) }), t.jsx(I, { style: { color: "#d9d9d9" } })] }) : t.jsx(C, { placeholder: n.placeholder, onClick: () => m(!0), value: c, suffix: t.jsx(I, { style: { color: "#d9d9d9" } }) });
  }
  function y() {
    return n.showSearch ? t.jsx("div", { className: s["search-input"], children: t.jsx(H, { onKeyUp: (e) => {
      if (n.onSearch) {
        n.onSearch(e.currentTarget.value);
        return;
      }
      D(e.currentTarget.value);
    } }) }) : null;
  }
  function B() {
    return o ? t.jsxs(t.Fragment, { children: [t.jsx("div", { className: s["select-label"], children: "已选择:" }), t.jsx(F, { size: [0, "small"], wrap: !0, className: s["tag-list"], children: d.map((e, l) => t.jsx(N, { bordered: !1, closable: !0, onClose: (r) => {
      r.preventDefault();
      const a = d.findIndex((i) => e.value === i.value);
      h((i) => {
        i.splice(a, 1);
      });
    }, children: e.label }, l)) })] }) : null;
  }
  function E() {
    return (j ? u.filter((l) => j(z, l)) : u).map((l, r) => t.jsx("li", { className: s["wheel-item"], onClick: () => {
      o && h((a) => {
        if (d.find((i) => i.value === l.value)) {
          const i = d.findIndex((f) => l.value === f.value);
          a.splice(i, 1);
        } else
          a.push(l);
      });
    }, children: l.label }, r));
  }
  return t.jsxs("div", { className: `bitzSelectMobileBlock ${n.className}`, children: [$(), t.jsx(V, { open: p, handleCancel: O, handleOk: T, height: n.drawerHeight || 273, children: t.jsxs("div", { ref: w, className: s.bitzSelectWrapper, children: [y(), B(), t.jsxs("div", { className: s["picker-content"], children: [t.jsx("div", { className: `${s["mask-top"]} ${s["border-bottom-1px"]}` }), t.jsx("div", { className: `${s["mask-bottom"]} ${s["border-top-1px"]}` }), t.jsx("div", { className: s["wheel-wrapper"], children: t.jsx("div", { className: s.wheel, children: t.jsx("ul", { className: s["wheel-scroll"], children: E() }) }) })] })] }) })] });
}, _ = K;
export {
  _ as default
};
