function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { Button, Empty, Tag, Input } from 'antd';
import { InfoCircleOutlined, PlusOutlined, CheckCircleFilled, SearchOutlined } from '@ant-design/icons';
import { PisellModal, PisellCard, PisellAvatar } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { settingsContext } from "../../index";
import "./index.less";
var AntTag = Tag;
var MemberSelector = function MemberSelector(_ref) {
  var dataSource = _ref.dataSource,
    value = _ref.value,
    onConfirm = _ref.onConfirm,
    open = _ref.open,
    onClose = _ref.onClose,
    _ref$showSearch = _ref.showSearch,
    showSearch = _ref$showSearch === void 0 ? false : _ref$showSearch,
    onSearch = _ref.onSearch,
    _ref$searchText = _ref.searchText,
    searchText = _ref$searchText === void 0 ? '' : _ref$searchText;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    internalSelected = _useState2[0],
    setInternalSelected = _useState2[1];
  useEffect(function () {
    if (open) {
      setInternalSelected(value || []);
    }
  }, [open, value]);
  var handleToggle = function handleToggle(val) {
    setInternalSelected(function (prev) {
      if (prev.includes(val)) {
        return prev.filter(function (v) {
          return v !== val;
        });
      }
      return [].concat(_toConsumableArray(prev), [val]);
    });
  };
  var handleConfirm = function handleConfirm() {
    onConfirm(internalSelected);
    onClose();
  };
  return /*#__PURE__*/React.createElement(PisellModal, {
    open: open,
    onCancel: onClose,
    title: locales.getText('pisell2.attendance.group-settings.basic-info.members-drawer.title'),
    width: 640,
    footer: /*#__PURE__*/React.createElement("div", {
      className: "pisell-member-selector-footer"
    }, /*#__PURE__*/React.createElement(Button, {
      type: "primary",
      onClick: handleConfirm,
      block: true
    }, locales.getText('pisell2.attendance.group-settings.actions.confirm'))),
    bodyStyle: {
      padding: 20
    }
  }, showSearch && /*#__PURE__*/React.createElement("div", {
    style: {
      marginBottom: 16
    }
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: locales.getText('pisell2.attendance.group-settings.actions.search'),
    prefix: /*#__PURE__*/React.createElement(SearchOutlined, null),
    value: searchText,
    onChange: function onChange(e) {
      onSearch === null || onSearch === void 0 || onSearch(e.target.value);
    },
    allowClear: true
  })), /*#__PURE__*/React.createElement("div", {
    className: "pisell-member-selector-list"
  }, dataSource.length === 0 ? /*#__PURE__*/React.createElement("div", {
    style: {
      gridColumn: '1/-1'
    }
  }, /*#__PURE__*/React.createElement(Empty, null)) : dataSource.map(function (item) {
    var _item$label;
    var isSelected = internalSelected.includes(item.value);
    return /*#__PURE__*/React.createElement("div", {
      key: item.value,
      className: "pisell-member-selector-item ".concat(isSelected ? 'pisell-member-selector-item-selected' : ''),
      onClick: function onClick() {
        return handleToggle(item.value);
      }
    }, /*#__PURE__*/React.createElement(PisellCard, {
      padding: 0
    }, /*#__PURE__*/React.createElement("div", {
      className: "pisell-member-selector-item-content"
    }, /*#__PURE__*/React.createElement(PisellAvatar, {
      src: item.avatar,
      size: "xxl",
      className: "pisell-member-selector-item-avatar"
    }, (_item$label = item.label) === null || _item$label === void 0 || (_item$label = _item$label[0]) === null || _item$label === void 0 ? void 0 : _item$label.toUpperCase()), /*#__PURE__*/React.createElement("div", {
      className: "pisell-member-selector-item-name",
      title: item.label
    }, item.label), /*#__PURE__*/React.createElement("div", {
      className: "pisell-member-selector-item-check"
    }, /*#__PURE__*/React.createElement(CheckCircleFilled, null)))));
  })));
};
var MembersField = function MembersField(props) {
  var _props$value;
  var _useContext = useContext(settingsContext),
    members = _useContext.members;
  var value = (_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : [];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isShowSelector = _useState4[0],
    setIsShowSelector = _useState4[1];
  var _useState5 = useState(''),
    _useState6 = _slicedToArray(_useState5, 2),
    searchText = _useState6[0],
    setSearchText = _useState6[1];
  var memberById = useMemo(function () {
    if (!Array.isArray(members)) {
      return {};
    }
    return members.reduce(function (prev, cur) {
      prev[cur.id] = cur;
      return prev;
    }, {});
  }, [members]);
  var memberOpts = useMemo(function () {
    var list = [];
    if (Array.isArray(members)) {
      list = members;
    }
    if (searchText) {
      list = list.filter(function (item) {
        var _item$nickname;
        return (_item$nickname = item.nickname) === null || _item$nickname === void 0 ? void 0 : _item$nickname.toLowerCase().includes(searchText.toLowerCase());
      });
    }
    return list.map(function (item) {
      var _item$account;
      return {
        label: item.nickname,
        value: item.id,
        avatar: (_item$account = item.account) === null || _item$account === void 0 ? void 0 : _item$account.avatar
      };
    });
  }, [members, searchText]);
  var onAddMember = function onAddMember() {
    setIsShowSelector(true);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    id: props.id,
    style: {
      background: '#f9fafb',
      border: '1px solid #d1d5db',
      borderRadius: 8,
      padding: 12
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__members-box"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__members-count"
  }, locales.getText('pisell2.attendance.group-settings.basic-info.members-count')(value.length)), /*#__PURE__*/React.createElement(Button, {
    type: "link",
    className: "saas-attendance-group-settings__link",
    onClick: onAddMember
  }, /*#__PURE__*/React.createElement(PlusOutlined, null), locales.getText('pisell2.attendance.group-settings.basic-info.add-members'))), /*#__PURE__*/React.createElement("div", {
    style: {
      marginTop: 8
    }
  }, value.map(function (id) {
    var _memberById$id;
    return /*#__PURE__*/React.createElement(AntTag, {
      key: id,
      closable: true,
      onClose: function onClose(e) {
        var _props$onChange;
        e.preventDefault();
        (_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, value.filter(function (v) {
          return v !== id;
        }));
      }
    }, ((_memberById$id = memberById[id]) === null || _memberById$id === void 0 ? void 0 : _memberById$id.nickname) || id);
  })), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__tip",
    style: {
      marginTop: 8
    }
  }, /*#__PURE__*/React.createElement(InfoCircleOutlined, null), /*#__PURE__*/React.createElement("span", null, locales.getText('pisell2.attendance.group-settings.basic-info.members-tip')))), /*#__PURE__*/React.createElement(MemberSelector, {
    value: value,
    dataSource: memberOpts,
    open: isShowSelector,
    showSearch: true,
    searchText: searchText,
    onSearch: setSearchText,
    onConfirm: function onConfirm(values) {
      if (values) {
        var _props$onChange2;
        (_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 || _props$onChange2.call(props, values);
      } else {
        var _props$onChange3;
        (_props$onChange3 = props.onChange) === null || _props$onChange3 === void 0 || _props$onChange3.call(props, []);
      }
    },
    onClose: function onClose() {
      setIsShowSelector(false);
      setSearchText('');
    }
  }));
};
export default MembersField;