const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_index = require("../../utils/index.js");
const require_locales = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/productStockCheckModal/index.tsx
/** 收起时列表最多展示的条目数 */
const MAX_COLLAPSED_ITEMS = 5;
const ProductStockCheckModal = (props, ref) => {
	var _context$engine, _context$appHelper, _context$appHelper2, _context$appHelper2$i;
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_locales.default, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || "en");
	require_index.request.setRequest(context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request);
	const isTerminal = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2$i = _context$appHelper2.isTerminal) === null || _context$appHelper2$i === void 0 ? void 0 : _context$appHelper2$i.call(_context$appHelper2);
	const [visible, setVisible] = (0, react.useState)(false);
	const modalParamsRef = (0, react.useRef)();
	const [items, setItems] = (0, react.useState)([]);
	const [isAllItemsSoldOut, setIsAllItemsSoldOut] = (0, react.useState)(false);
	const [listExpanded, setListExpanded] = (0, react.useState)(false);
	const onOpen = (0, react.useCallback)((params) => {
		var _params$unavailablePr, _params$isAllItemsSol, _props$onOpen;
		modalParamsRef.current = params;
		setItems((_params$unavailablePr = params === null || params === void 0 ? void 0 : params.unavailableProducts) !== null && _params$unavailablePr !== void 0 ? _params$unavailablePr : []);
		setIsAllItemsSoldOut((_params$isAllItemsSol = params === null || params === void 0 ? void 0 : params.isAllItemsSoldOut) !== null && _params$isAllItemsSol !== void 0 ? _params$isAllItemsSol : false);
		setListExpanded(false);
		setVisible(true);
		(_props$onOpen = props.onOpen) === null || _props$onOpen === void 0 || _props$onOpen.call(props);
	}, [props]);
	const onClose = (0, react.useCallback)(() => {
		var _props$onClose;
		modalParamsRef.current = void 0;
		setItems([]);
		setIsAllItemsSoldOut(false);
		setVisible(false);
		(_props$onClose = props.onClose) === null || _props$onClose === void 0 || _props$onClose.call(props);
	}, []);
	(0, react.useImperativeHandle)(ref, () => ({
		open: onOpen,
		close: onClose
	}));
	const handleReturn = (0, react.useCallback)(() => {
		var _props$onReturn;
		onClose();
		(_props$onReturn = props.onReturn) === null || _props$onReturn === void 0 || _props$onReturn.call(props, items);
	}, [
		props.onReturn,
		onClose,
		items
	]);
	const handleContinue = (0, react.useCallback)(() => {
		onClose();
		if (isAllItemsSoldOut) handleReturn();
		else {
			var _props$onContinue;
			(_props$onContinue = props.onContinue) === null || _props$onContinue === void 0 || _props$onContinue.call(props, items);
		}
	}, [
		props.onContinue,
		onClose,
		isAllItemsSoldOut,
		items
	]);
	const showListExpandToggle = (0, react.useMemo)(() => items.length > MAX_COLLAPSED_ITEMS, [items.length]);
	const displayItems = (0, react.useMemo)(() => {
		if (!showListExpandToggle || listExpanded) return items;
		return items.slice(0, MAX_COLLAPSED_ITEMS);
	}, [
		items,
		listExpanded,
		showListExpandToggle
	]);
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		platform: isTerminal ? "h5" : void 0,
		visible,
		header: false,
		footer: null,
		maskClosable: false,
		destroyOnClose: true,
		closable: false,
		onClose,
		onCancel: onClose,
		wrapClassName: "product-stock-check-modal",
		style: { zIndex: 1001 }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "product-stock-check-modal-inner" }, /* @__PURE__ */ react.default.createElement("div", {
		className: "product-stock-check-modal-icon",
		"aria-hidden": true
	}, /* @__PURE__ */ react.default.createElement("span", { className: "product-stock-check-modal-icon-mark" }, "!")), /* @__PURE__ */ react.default.createElement("h2", { className: "product-stock-check-modal-title" }, isAllItemsSoldOut ? _pisell_utils.locales.getText("pisell2.product.stock.check.modal.all_items_title") : _pisell_utils.locales.getText("pisell2.product.stock.check.modal.title")), /* @__PURE__ */ react.default.createElement("p", { className: "product-stock-check-modal-subtitle" }, _pisell_utils.locales.getText("pisell2.product.stock.check.modal.subtitle")), /* @__PURE__ */ react.default.createElement("div", { className: "product-stock-check-modal-list" }, /* @__PURE__ */ react.default.createElement("div", { className: "product-stock-check-modal-list-items" }, displayItems.map((item, index) => /* @__PURE__ */ react.default.createElement("div", {
		key: index,
		className: "product-stock-check-modal-item"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "product-stock-check-modal-item-text" }, /* @__PURE__ */ react.default.createElement("span", { className: "product-stock-check-modal-item-title" }, item.title), /* @__PURE__ */ react.default.createElement("span", { className: "product-stock-check-modal-item-qty" }, _pisell_utils.locales.getText("pisell2.product.stock.check.modal.item.quantity.prefix"), " ", "x", item.quantity)), /* @__PURE__ */ react.default.createElement("span", { className: "product-stock-check-modal-tag" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Tag, { color: "error" }, _pisell_utils.locales.getText("pisell2.product.stock.check.modal.status.sold_out")))))), showListExpandToggle && /* @__PURE__ */ react.default.createElement("div", {
		className: "product-stock-check-modal-list-toggle",
		onClick: () => setListExpanded((v) => !v)
	}, listExpanded ? _pisell_utils.locales.getText("pisell2.product.stock.check.modal.btn.collapse") : _pisell_utils.locales.getText("pisell2.product.stock.check.modal.btn.expand"), /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: listExpanded ? "pisell2-chevron-up-double" : "pisell2-chevron-down-double" }))), /* @__PURE__ */ react.default.createElement("div", { className: "product-stock-check-modal-buttons" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		onClick: handleContinue
	}, isAllItemsSoldOut ? _pisell_utils.locales.getText("pisell2.product.stock.check.modal.btn.remove_all_continue") : _pisell_utils.locales.getText("pisell2.product.stock.check.modal.btn.remove_continue")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "text",
		onClick: handleReturn
	}, _pisell_utils.locales.getText("pisell2.product.stock.check.modal.btn.go_back")))));
};
var productStockCheckModal_default = (0, react.forwardRef)(ProductStockCheckModal);
//#endregion
exports.default = productStockCheckModal_default;
