import { j as t } from "../../../node_modules/react/jsx-runtime.mjs";
import { useState as v, useRef as E, 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 g 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 } = g, K = ({ ...r }) => {
  const o = r.mode === "multiple" || r.mode === "tags", u = r.options || [], j = r.filterOption, [b, C] = v(() => {
    var l;
    const e = (l = r.options) == null ? void 0 : l.findIndex((n) => n.value === r.defaultValue);
    return e === -1 ? 0 : e;
  }), [c, x] = S(() => {
    var e, l;
    return o ? r.defaultValue : ((l = (e = r.options) == null ? void 0 : e.find((n) => n.value === r.defaultValue)) == null ? void 0 : l.value) || "";
  }), w = E(null), [p, m] = v(!1), [z, D] = v(""), [d, h] = S([]), T = () => {
    m(!1);
  }, $ = () => {
    const e = r.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", () => {
          C(e.getSelectedIndex());
        }), o) {
          const l = u.filter((n) => c == null ? void 0 : c.find((i) => i === n.value));
          h(l);
        }
      } else
        h((e) => {
          e.splice(0);
        });
  }, [p]);
  function y() {
    return o ? /* @__PURE__ */ t.jsxs("div", { className: s.textarea, onClick: () => m(!0), children: [
      /* @__PURE__ */ t.jsx("div", { children: c == null ? void 0 : c.map((e, l) => {
        var n;
        return /* @__PURE__ */ t.jsx(
          N,
          {
            bordered: !1,
            closable: !0,
            onClose: (i) => {
              i.preventDefault();
              const a = c.findIndex((f) => f === e);
              x((f) => {
                f.splice(a, 1);
              });
            },
            children: (n = u.find((i) => e === i.value)) == null ? void 0 : n.label
          },
          l
        );
      }) }),
      /* @__PURE__ */ t.jsx(I, { style: { color: "#d9d9d9" } })
    ] }) : /* @__PURE__ */ t.jsx(g, { placeholder: r.placeholder, onClick: () => m(!0), value: c, suffix: /* @__PURE__ */ t.jsx(I, { style: { color: "#d9d9d9" } }) });
  }
  function B() {
    return r.showSearch ? /* @__PURE__ */ t.jsx("div", { className: s["search-input"], children: /* @__PURE__ */ t.jsx(
      H,
      {
        onKeyUp: (e) => {
          if (r.onSearch) {
            r.onSearch(e.currentTarget.value);
            return;
          }
          D(e.currentTarget.value);
        }
      }
    ) }) : null;
  }
  function O() {
    return o ? /* @__PURE__ */ t.jsxs(t.Fragment, { children: [
      /* @__PURE__ */ t.jsx("div", { className: s["select-label"], children: "已选择:" }),
      /* @__PURE__ */ t.jsx(F, { size: [0, "small"], wrap: !0, className: s["tag-list"], children: d.map((e, l) => /* @__PURE__ */ t.jsx(
        N,
        {
          bordered: !1,
          closable: !0,
          onClose: (n) => {
            n.preventDefault();
            const i = d.findIndex((a) => e.value === a.value);
            h((a) => {
              a.splice(i, 1);
            });
          },
          children: e.label
        },
        l
      )) })
    ] }) : null;
  }
  function W() {
    return (j ? u.filter((l) => j(z, l)) : u).map((l, n) => /* @__PURE__ */ t.jsx(
      "li",
      {
        className: s["wheel-item"],
        onClick: () => {
          o && h((i) => {
            if (d.find((a) => a.value === l.value)) {
              const a = d.findIndex((f) => l.value === f.value);
              i.splice(a, 1);
            } else
              i.push(l);
          });
        },
        children: l.label
      },
      n
    ));
  }
  return /* @__PURE__ */ t.jsxs("div", { className: `bitzSelectMobileBlock ${r.className}`, children: [
    y(),
    /* @__PURE__ */ t.jsx(V, { open: p, handleCancel: T, handleOk: $, height: r.drawerHeight || 273, children: /* @__PURE__ */ t.jsxs("div", { ref: w, className: s.bitzSelectWrapper, children: [
      B(),
      O(),
      /* @__PURE__ */ t.jsxs("div", { className: s["picker-content"], children: [
        /* @__PURE__ */ t.jsx("div", { className: `${s["mask-top"]} ${s["border-bottom-1px"]}` }),
        /* @__PURE__ */ t.jsx("div", { className: `${s["mask-bottom"]} ${s["border-top-1px"]}` }),
        /* @__PURE__ */ t.jsx("div", { className: s["wheel-wrapper"], children: /* @__PURE__ */ t.jsx("div", { className: s.wheel, children: /* @__PURE__ */ t.jsx("ul", { className: s["wheel-scroll"], children: W() }) }) })
      ] })
    ] }) })
  ] });
}, _ = K;
export {
  _ as default
};
