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, { useMemo, useState, forwardRef, useImperativeHandle } from 'react';
import { Dropdown } from 'antd';
import { Button, Iconfont, PisellCard, PisellAvatar, PisellToast } from '@pisell/materials';
import { locales, isMobile, getUniqueId } from '@pisell/utils';
import InformationOptionsModal from "./components/InformationOptionsModal";
import { useAppointmentBookingContext } from "../../context";
import { useAppointmentBookingAccount, useAppointmentBookingStep } from "../../hooks";
import { formatGuest } from "../../utils";
import "./index.less";
var Guest = /*#__PURE__*/forwardRef(function (props, ref) {
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    openOption = _useState2[0],
    setOpenOption = _useState2[1];
  // 编辑哪个holder
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    holderItem = _useState4[0],
    setHolderItem = _useState4[1];
  var _useAppointmentBookin = useAppointmentBookingContext(),
    carts = _useAppointmentBookin.carts,
    appointmentBooking = _useAppointmentBookin.appointmentBooking;
  var _useAppointmentBookin2 = useAppointmentBookingAccount(),
    accountLists = _useAppointmentBookin2.accountLists;
  var _useAppointmentBookin3 = useAppointmentBookingStep(),
    nextStep = _useAppointmentBookin3.nextStep;
  useImperativeHandle(ref, function () {
    return {
      next: function next() {
        nextStep();
      }
    };
  });
  var lists = useMemo(function () {
    var _lists = formatGuest(accountLists, carts);
    return _lists;
  }, [accountLists, carts]);

  /**
   * @title: 选项
   * @description:
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-19 10:44
   */
  var options = [{
    key: 'edit',
    label: /*#__PURE__*/React.createElement("span", {
      className: "dropdown-menu-item"
    }, locales.getText('pisell2.appointment.edit.service'))
  }, {
    key: 'remove',
    label: /*#__PURE__*/React.createElement("span", {
      className: 'dropdown-menu-item dropdown-menu-remove-item'
    }, locales.getText('pisell2.event.remove'))
  }];

  /**
   * @title: 点击item触发
   * @description:
   * @param {string} key
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-18 16:14
   */
  var handleClick = function handleClick(_ref, item) {
    var key = _ref.key;
    console.log(key, item, 'wh978967');
    // 编辑holder
    if (key === 'edit') {
      appointmentBooking.setActiveAccount(item.id);
      appointmentBooking.back();
      return;
    }

    // 移除holder
    appointmentBooking.removeAccount(item.id);
  };

  /**
   * @title: 渲染options
   * @description:
   * @param {any} item
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-19 11:44
   */
  var renderOptionButton = function renderOptionButton(item) {
    console.log(item, options, 'wh978776');
    var _options = [].concat(options);
    if (!item.isGuest) {
      _options = _options.filter(function (option) {
        return option.key !== 'remove';
      });
    }
    if (isMobile()) {
      return /*#__PURE__*/React.createElement(Button, {
        size: "large",
        onClick: function onClick() {
          setHolderItem(item);
          setOpenOption(true);
        }
      }, locales.getText('pisell2.event.option'));
    }
    return /*#__PURE__*/React.createElement(Dropdown, {
      menu: {
        items: _options,
        onClick: function onClick(e) {
          setHolderItem(item);
          handleClick(e, item);
        }
      },
      placement: "bottomRight",
      overlayClassName: "appointment-guest-custom-dropdown-menu"
    }, /*#__PURE__*/React.createElement(Button, null, locales.getText('pisell2.event.option')));
  };
  var handleAddGuest = function handleAddGuest() {
    // 最多添加6个人
    if (accountLists.length >= 6) {
      return PisellToast({
        content: locales.getText('pisell2.appointment.max.guest')
      });
    }
    var guest = {
      id: getUniqueId(),
      username: "Guest".concat(accountLists.length + 1),
      isGuest: true
    };
    appointmentBooking.addAccounts([guest]);
    appointmentBooking.setActiveAccount(guest.id);
    appointmentBooking.back();
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "appointment-guest-wrap"
  }, lists === null || lists === void 0 ? void 0 : lists.map(function (item) {
    var _item$children;
    return /*#__PURE__*/React.createElement(PisellCard, {
      key: item.id,
      padding: 20,
      className: "information-card",
      isClickable: false
    }, /*#__PURE__*/React.createElement("div", {
      className: "information-header"
    }, /*#__PURE__*/React.createElement("div", {
      className: "information-left"
    }, /*#__PURE__*/React.createElement(PisellAvatar, {
      src: item.avatar,
      size: "md"
    }), /*#__PURE__*/React.createElement("div", {
      className: "information-title"
    }, /*#__PURE__*/React.createElement("span", null, item.name), /*#__PURE__*/React.createElement("span", {
      className: "subtitle"
    }, "".concat(locales.getText('pisell2.appointment.service.title')((_item$children = item.children) === null || _item$children === void 0 ? void 0 : _item$children.length))))), renderOptionButton(item)));
  }), /*#__PURE__*/React.createElement(Button, {
    size: "large",
    style: {
      width: 'max-content'
    },
    onClick: handleAddGuest,
    className: "appointment-guest-add-button"
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-plus",
    style: {
      fontSize: '20px'
    }
  }), locales.getText('pisell2.appointment.add.guest'))), openOption ? /*#__PURE__*/React.createElement(InformationOptionsModal, {
    options: options,
    open: openOption,
    holderItem: holderItem,
    onCancel: function onCancel() {
      return setOpenOption(false);
    },
    onClick: function onClick(e) {
      handleClick(e, holderItem);
      setOpenOption(false);
    }
  }) : null);
});
export default Guest;