import { formatGuest } from "../../utils.js";
import { useAppointmentBookingContext } from "../../context.js";
import { useAppointmentBookingAccount, useAppointmentBookingStep, useConfig } from "../../hooks.js";
import GuestOptionsModal from "./components/InformationOptionsModal/index.js";
import React, { forwardRef, useImperativeHandle, useMemo, useState } from "react";
import { Dropdown } from "antd";
import { getUniqueId, locales } from "@pisell/utils";
import { Button as Button$1, Iconfont, PisellAvatar, PisellCard, PisellToast } from "@pisell/materials";
import "./index.less";
//#region src/components/appointmentBooking/components/Guest/index.tsx
const Guest = forwardRef((props, ref) => {
	const [openOption, setOpenOption] = useState(false);
	const [holderItem, setHolderItem] = useState(null);
	const { carts, appointmentBooking } = useAppointmentBookingContext();
	const { accountLists } = useAppointmentBookingAccount();
	const { nextStep } = useAppointmentBookingStep();
	const { isMobile } = useConfig();
	useImperativeHandle(ref, () => ({ next: () => {
		nextStep();
	} }));
	const lists = useMemo(() => {
		return formatGuest(accountLists, carts);
	}, [accountLists, carts]);
	/**
	* @title: 选项
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-19 10:44
	*/
	const 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
	*/
	const handleClick = ({ key }, item) => {
		console.log(key, item, "wh978967");
		if (key === "edit") {
			appointmentBooking.setActiveAccount(item.id);
			appointmentBooking.back();
			return;
		}
		appointmentBooking.removeAccount(item.id);
	};
	/**
	* @title: 渲染options
	* @description:
	* @param {any} item
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-19 11:44
	*/
	const renderOptionButton = (item) => {
		console.log(item, options, "wh978776");
		let _options = [...options];
		if (!item.isGuest) _options = _options.filter((option) => option.key !== "remove");
		if (isMobile) return /* @__PURE__ */ React.createElement(Button$1, {
			size: "large",
			onClick: () => {
				setHolderItem(item);
				setOpenOption(true);
			}
		}, locales.getText("pisell2.event.option"));
		return /* @__PURE__ */ React.createElement(Dropdown, {
			menu: {
				items: _options,
				onClick: (e) => {
					setHolderItem(item);
					handleClick(e, item);
				}
			},
			placement: "bottomRight",
			overlayClassName: "appointment-guest-custom-dropdown-menu"
		}, /* @__PURE__ */ React.createElement(Button$1, null, locales.getText("pisell2.event.option")));
	};
	const handleAddGuest = () => {
		if (accountLists.length >= 6) return PisellToast({ content: locales.getText("pisell2.appointment.max.guest") });
		const guest = {
			id: getUniqueId(),
			username: `Guest${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((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" }, `${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$1, {
		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(GuestOptionsModal, {
		options,
		open: openOption,
		holderItem,
		onCancel: () => setOpenOption(false),
		onClick: (e) => {
			handleClick(e, holderItem);
			setOpenOption(false);
		}
	}) : null);
});
//#endregion
export { Guest as default };
