import { useContext as useContext$1 } from "./useContext.js";
import React, { useCallback, useMemo, useState } from "react";
import { locales } from "@pisell/utils";
import { Iconfont, PisellModal } from "@pisell/materials";
import classNames from "classnames";
//#region src/components/shoppingCart/hooks/useClearCart.tsx
const useClearCart = (key) => {
	const [open, setOpen] = useState(false);
	const { state, onChange } = useContext$1();
	const { isProductList, disabledEdit, holders, isHideClearCart } = state;
	/**
	* @title: 清空购物车
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-11-28 22:47
	*/
	const handleClear = () => {
		let products = [...(state === null || state === void 0 ? void 0 : state.products) || []];
		if (typeof key === "undefined") products = [];
		else {
			onChange(products === null || products === void 0 ? void 0 : products.map((item) => item === null || item === void 0 ? void 0 : item._id), "delete_holders", key);
			setOpen(false);
			return;
		}
	};
	const handleOpenModal = useCallback(() => {
		setOpen(true);
	}, []);
	const handleCancel = useCallback(() => {
		setOpen(false);
	}, []);
	return useMemo(() => {
		const showClearBtn = !disabledEdit && (holders === null || holders === void 0 ? void 0 : holders.length) > 0 && !isHideClearCart;
		return /* @__PURE__ */ React.createElement(React.Fragment, null, showClearBtn ? /* @__PURE__ */ React.createElement("span", {
			className: classNames("pisell-lowcode__shopping-cart-clear-btn", { "pisell-lowcode__shopping-cart-clear-btn-empty": isProductList }),
			onClick: handleOpenModal
		}, /* @__PURE__ */ React.createElement(Iconfont, { type: "pisell2-trash-01" }), /* @__PURE__ */ React.createElement("span", null, locales.getText("pisell2.booking.info.text.clear-cart"))) : null, /* @__PURE__ */ React.createElement(PisellModal, {
			open,
			title: locales.getText("pisell2.booking.info.text.clear-cart-modal.title"),
			onOk: handleClear,
			onCancel: handleCancel,
			okText: locales.getText("pisell2.booking.info.text.clear-cart-modal.confirm"),
			cancelText: locales.getText("pisell2.booking.info.text.clear-cart-modal.cancel"),
			destroyOnClose: true
		}, /* @__PURE__ */ React.createElement("div", { style: { margin: "24px 0" } }, /* @__PURE__ */ React.createElement("span", null, locales.getText("pisell2.booking.info.text.clear-cart-modal.content")))));
	}, [
		open,
		disabledEdit,
		holders === null || holders === void 0 ? void 0 : holders.length,
		isHideClearCart,
		isProductList
	]);
};
//#endregion
export { useClearCart as default };
