var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saasAttendance/settings/components/membersField/index.tsx
var membersField_exports = {};
__export(membersField_exports, {
  default: () => membersField_default
});
module.exports = __toCommonJS(membersField_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import__ = require("../../index");
var import_index = require("./index.less");
var AntTag = import_antd.Tag;
var MemberSelector = ({
  dataSource,
  value,
  onConfirm,
  open,
  onClose,
  showSearch = false,
  onSearch,
  searchText = ""
}) => {
  const [internalSelected, setInternalSelected] = (0, import_react.useState)([]);
  (0, import_react.useEffect)(() => {
    if (open) {
      setInternalSelected(value || []);
    }
  }, [open, value]);
  const handleToggle = (val) => {
    setInternalSelected((prev) => {
      if (prev.includes(val)) {
        return prev.filter((v) => v !== val);
      }
      return [...prev, val];
    });
  };
  const handleConfirm = () => {
    onConfirm(internalSelected);
    onClose();
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open,
      onCancel: onClose,
      title: import_utils.locales.getText(
        "pisell2.attendance.group-settings.basic-info.members-drawer.title"
      ),
      width: 640,
      footer: /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-member-selector-footer" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", onClick: handleConfirm, block: true }, import_utils.locales.getText(
        "pisell2.attendance.group-settings.actions.confirm"
      ))),
      bodyStyle: { padding: 20 }
    },
    showSearch && /* @__PURE__ */ import_react.default.createElement("div", { style: { marginBottom: 16 } }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Input,
      {
        placeholder: import_utils.locales.getText(
          "pisell2.attendance.group-settings.actions.search"
        ),
        prefix: /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, null),
        value: searchText,
        onChange: (e) => {
          onSearch == null ? void 0 : onSearch(e.target.value);
        },
        allowClear: true
      }
    )),
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-member-selector-list" }, dataSource.length === 0 ? /* @__PURE__ */ import_react.default.createElement("div", { style: { gridColumn: "1/-1" } }, /* @__PURE__ */ import_react.default.createElement(import_antd.Empty, null)) : dataSource.map((item) => {
      var _a, _b;
      const isSelected = internalSelected.includes(item.value);
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          key: item.value,
          className: `pisell-member-selector-item ${isSelected ? "pisell-member-selector-item-selected" : ""}`,
          onClick: () => handleToggle(item.value)
        },
        /* @__PURE__ */ import_react.default.createElement(import_materials.PisellCard, { padding: 0 }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-member-selector-item-content" }, /* @__PURE__ */ import_react.default.createElement(
          import_materials.PisellAvatar,
          {
            src: item.avatar,
            size: "xxl",
            className: "pisell-member-selector-item-avatar"
          },
          (_b = (_a = item.label) == null ? void 0 : _a[0]) == null ? void 0 : _b.toUpperCase()
        ), /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: "pisell-member-selector-item-name",
            title: item.label
          },
          item.label
        ), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-member-selector-item-check" }, /* @__PURE__ */ import_react.default.createElement(import_icons.CheckCircleFilled, null))))
      );
    }))
  );
};
var MembersField = (props) => {
  const { members } = (0, import_react.useContext)(import__.settingsContext);
  const value = props.value ?? [];
  const [isShowSelector, setIsShowSelector] = (0, import_react.useState)(false);
  const [searchText, setSearchText] = (0, import_react.useState)("");
  const memberById = (0, import_react.useMemo)(() => {
    if (!Array.isArray(members)) {
      return {};
    }
    return members.reduce(
      (prev, cur) => {
        prev[cur.id] = cur;
        return prev;
      },
      {}
    );
  }, [members]);
  const memberOpts = (0, import_react.useMemo)(() => {
    let list = [];
    if (Array.isArray(members)) {
      list = members;
    }
    if (searchText) {
      list = list.filter(
        (item) => {
          var _a;
          return (_a = item.nickname) == null ? void 0 : _a.toLowerCase().includes(searchText.toLowerCase());
        }
      );
    }
    return list.map((item) => {
      var _a;
      return {
        label: item.nickname,
        value: item.id,
        avatar: (_a = item.account) == null ? void 0 : _a.avatar
      };
    });
  }, [members, searchText]);
  const onAddMember = () => {
    setIsShowSelector(true);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      id: props.id,
      style: {
        background: "#f9fafb",
        border: "1px solid #d1d5db",
        borderRadius: 8,
        padding: 12
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__members-box" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-attendance-group-settings__members-count" }, import_utils.locales.getText(
      "pisell2.attendance.group-settings.basic-info.members-count"
    )(value.length)), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        type: "link",
        className: "saas-attendance-group-settings__link",
        onClick: onAddMember
      },
      /* @__PURE__ */ import_react.default.createElement(import_icons.PlusOutlined, null),
      import_utils.locales.getText(
        "pisell2.attendance.group-settings.basic-info.add-members"
      )
    )),
    /* @__PURE__ */ import_react.default.createElement("div", { style: { marginTop: 8 } }, value.map((id) => {
      var _a;
      return /* @__PURE__ */ import_react.default.createElement(
        AntTag,
        {
          key: id,
          closable: true,
          onClose: (e) => {
            var _a2;
            e.preventDefault();
            (_a2 = props.onChange) == null ? void 0 : _a2.call(props, value.filter((v) => v !== id));
          }
        },
        ((_a = memberById[id]) == null ? void 0 : _a.nickname) || id
      );
    })),
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "saas-attendance-group-settings__tip",
        style: { marginTop: 8 }
      },
      /* @__PURE__ */ import_react.default.createElement(import_icons.InfoCircleOutlined, null),
      /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText(
        "pisell2.attendance.group-settings.basic-info.members-tip"
      ))
    )
  ), /* @__PURE__ */ import_react.default.createElement(
    MemberSelector,
    {
      value,
      dataSource: memberOpts,
      open: isShowSelector,
      showSearch: true,
      searchText,
      onSearch: setSearchText,
      onConfirm: (values) => {
        var _a, _b;
        if (values) {
          (_a = props.onChange) == null ? void 0 : _a.call(props, values);
        } else {
          (_b = props.onChange) == null ? void 0 : _b.call(props, []);
        }
      },
      onClose: () => {
        setIsShowSelector(false);
        setSearchText("");
      }
    }
  ));
};
var membersField_default = MembersField;
