const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index = require("../../../../pro/pisellNumberSelector/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/plus/saleDetail/components/CartItems/index.tsx
const { LineItem } = _pisell_materials.ProductCard;
const STATUS_OPTIONS = [
	{
		key: "new",
		labelKey: "pisell2.sale-detail.status.new",
		color: "#91caff"
	},
	{
		key: "confirmed",
		labelKey: "pisell2.sale-detail.status.confirmed",
		color: "#b7eb8f"
	},
	{
		key: "arrived",
		labelKey: "pisell2.sale-detail.status.arrived",
		color: "#d3adf7"
	},
	{
		key: "started",
		labelKey: "pisell2.sale-detail.status.started",
		color: "#fffb8f"
	},
	{
		key: "cancelled",
		labelKey: "pisell2.sale-detail.status.cancelled",
		color: "#bfbfbf",
		danger: true
	},
	{
		key: "no_show",
		labelKey: "pisell2.sale-detail.status.no_show",
		color: "#ffa39e",
		danger: true
	},
	{
		key: "completed",
		labelKey: "pisell2.sale-detail.status.completed",
		color: "#ffd591"
	}
];
const getBookingQuantity = (item) => {
	var _ref, _ref2, _item$_extend$product, _item$_extend;
	return (_ref = (_ref2 = (_item$_extend$product = item === null || item === void 0 || (_item$_extend = item._extend) === null || _item$_extend === void 0 || (_item$_extend = _item$_extend.product) === null || _item$_extend === void 0 ? void 0 : _item$_extend.num) !== null && _item$_extend$product !== void 0 ? _item$_extend$product : item === null || item === void 0 ? void 0 : item.num) !== null && _ref2 !== void 0 ? _ref2 : item === null || item === void 0 ? void 0 : item.number) !== null && _ref !== void 0 ? _ref : 1;
};
const getCartLineKey = (item) => {
	var _item$_extend2, _item$_extend3, _item$_identity, _item$metadata;
	return String((item === null || item === void 0 || (_item$_extend2 = item._extend) === null || _item$_extend2 === void 0 || (_item$_extend2 = _item$_extend2.product) === null || _item$_extend2 === void 0 || (_item$_extend2 = _item$_extend2._identity) === null || _item$_extend2 === void 0 ? void 0 : _item$_extend2.unique_identification_number) || (item === null || item === void 0 || (_item$_extend3 = item._extend) === null || _item$_extend3 === void 0 || (_item$_extend3 = _item$_extend3.product) === null || _item$_extend3 === void 0 || (_item$_extend3 = _item$_extend3.metadata) === null || _item$_extend3 === void 0 ? void 0 : _item$_extend3.unique_identification_number) || (item === null || item === void 0 || (_item$_identity = item._identity) === null || _item$_identity === void 0 ? void 0 : _item$_identity.unique_identification_number) || (item === null || item === void 0 || (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.unique_identification_number) || (item === null || item === void 0 ? void 0 : item.product_uid) || (item === null || item === void 0 ? void 0 : item.booking_id) || (item === null || item === void 0 ? void 0 : item.order_detail_id) || (item === null || item === void 0 ? void 0 : item.product_id) || "");
};
function CartItems({ dataSource, sales, className, productsPerRow, showEmpty, emptyText }) {
	const [statusLoading, setStatusLoading] = (0, react.useState)(false);
	const [quantityLoadingKeys, setQuantityLoadingKeys] = (0, react.useState)({});
	const editingRef = (0, react.useRef)(false);
	const cart = Array.isArray(dataSource) ? {
		bookings: [],
		items: dataSource
	} : dataSource || {
		bookings: [],
		items: []
	};
	const bookings = cart.bookings || [];
	const items = cart.items || [];
	const displayBookings = (0, react.useMemo)(() => [...bookings].reverse(), [bookings]);
	const displayItems = (0, react.useMemo)(() => [...items].reverse(), [items]);
	const safeCols = Math.max(1, Math.floor(productsPerRow || 1));
	const isMultiCol = safeCols > 1;
	const isEmpty = !bookings.length && !items.length;
	const statusOptions = (0, react.useMemo)(() => STATUS_OPTIONS.map((option) => ({
		key: option.key,
		label: _pisell_utils.locales.getText(option.labelKey),
		color: option.color,
		danger: option.danger
	})), []);
	(0, react.useCallback)(function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (status) {
			const next = status.trim();
			if (!next) {
				antd.message.error(_pisell_utils.locales.getText("pisell2.sale-detail.status-empty-error"));
				return;
			}
			if (!(sales === null || sales === void 0 ? void 0 : sales.setBookingStatus)) return;
			setStatusLoading(true);
			try {
				yield sales.setBookingStatus(next);
			} catch (e) {
				var _e$message;
				antd.message.error(String((_e$message = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message !== void 0 ? _e$message : e));
			} finally {
				setStatusLoading(false);
			}
		});
		return function(_x) {
			return _ref3.apply(this, arguments);
		};
	}(), [sales]);
	const handleRemoveItem = (0, react.useCallback)(function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (item) {
			var _item$_extend$product2, _item$_extend4;
			const productItem = (_item$_extend$product2 = item === null || item === void 0 || (_item$_extend4 = item._extend) === null || _item$_extend4 === void 0 ? void 0 : _item$_extend4.product) !== null && _item$_extend$product2 !== void 0 ? _item$_extend$product2 : item;
			if (!productItem || !cart.removeItem) return;
			try {
				yield cart.removeItem(productItem);
			} catch (e) {
				var _e$message2;
				antd.message.error(String((_e$message2 = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message2 !== void 0 ? _e$message2 : e));
			}
		});
		return function(_x2) {
			return _ref4.apply(this, arguments);
		};
	}(), [cart.removeItem]);
	const handleEditCartLine = (0, react.useCallback)(function() {
		var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (item) {
			if (!cart.editCartLine || editingRef.current) return;
			editingRef.current = true;
			try {
				yield cart.editCartLine(item);
			} catch (e) {
				var _e$message3;
				antd.message.error(String((_e$message3 = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message3 !== void 0 ? _e$message3 : e));
			} finally {
				editingRef.current = false;
			}
		});
		return function(_x3) {
			return _ref5.apply(this, arguments);
		};
	}(), [cart.editCartLine]);
	const handleQuantityChange = (0, react.useCallback)(function() {
		var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (item, value) {
			const quantity = Math.floor(Number(value));
			if (!Number.isFinite(quantity) || quantity < 1) return;
			if (quantity === getBookingQuantity(item)) return;
			if (!cart.updateItemQuantity) {
				antd.message.error("[SalesSdk] Cart: updateItemQuantity 未配置");
				return;
			}
			const key = getCartLineKey(item);
			if (!key || quantityLoadingKeys[key]) return;
			setQuantityLoadingKeys((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { [key]: true }));
			try {
				yield cart.updateItemQuantity(item, quantity);
			} catch (e) {
				var _e$message4;
				antd.message.error(String((_e$message4 = e === null || e === void 0 ? void 0 : e.message) !== null && _e$message4 !== void 0 ? _e$message4 : e));
			} finally {
				setQuantityLoadingKeys((prev) => {
					const next = require_objectSpread2._objectSpread2({}, prev);
					delete next[key];
					return next;
				});
			}
		});
		return function(_x4, _x5) {
			return _ref6.apply(this, arguments);
		};
	}(), [cart.updateItemQuantity, quantityLoadingKeys]);
	const renderQuantityAction = (item) => [{
		key: "update_number",
		text: /* @__PURE__ */ react.default.createElement(require_index.default, {
			enablePriceAdjust: true,
			min: 1,
			className: "pisell-sale-detail-cart-items__quantity-selector",
			value: getBookingQuantity(item),
			disabled: Boolean(quantityLoadingKeys[getCartLineKey(item)]),
			onChange: (value) => handleQuantityChange(item, value)
		}),
		color: "#667085"
	}];
	return /* @__PURE__ */ react.default.createElement("section", {
		className: [
			"pisell-sale-detail-cart-items",
			isMultiCol ? "pisell-sale-detail-cart-items--multi-col" : "",
			className || ""
		].filter(Boolean).join(" "),
		style: isMultiCol ? { ["--pisell-cart-cols"]: safeCols } : void 0
	}, isEmpty && showEmpty ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-sale-detail-cart-items__empty" }, emptyText || _pisell_utils.locales.getText("pisell2.sale-detail.no-selected-items")) : null, displayBookings.map((item) => {
		var _item$metadata2;
		return /* @__PURE__ */ react.default.createElement(LineItem, {
			key: ((_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.unique_identification_number) || item.booking_id,
			itemType: "booking",
			type: "a9",
			rightActions: renderQuantityAction(item),
			escapeDom: ".pisell-number-selector-popover",
			dataSource: item,
			statusOptions,
			statusLoading,
			onBookingStatusChange: void 0,
			onAction: () => {},
			onDelete: () => handleRemoveItem(item),
			onNote: () => {},
			onCard: () => handleEditCartLine(item),
			onPromotion: () => {},
			isShowNote: true,
			disabledEdit: true,
			isShowDelete: true,
			isShowAmountFooter: true,
			isShowImage: true,
			maxSpecsCount: 1,
			scenario: "booking2",
			onGift: () => {}
		});
	}), displayItems.map((item) => {
		var _item$metadata3;
		return /* @__PURE__ */ react.default.createElement(LineItem, {
			key: ((_item$metadata3 = item.metadata) === null || _item$metadata3 === void 0 ? void 0 : _item$metadata3.unique_identification_number) || item.order_detail_id || item.product_id,
			itemType: "product",
			type: "a9",
			rightActions: renderQuantityAction(item),
			escapeDom: ".pisell-number-selector-popover",
			dataSource: item,
			onAction: () => {},
			onDelete: () => handleRemoveItem(item),
			onNote: () => {},
			onLike: () => {},
			onEditProduct: () => handleEditCartLine(item),
			onCard: () => handleEditCartLine(item),
			onChangeResource: () => handleEditCartLine(item),
			onChangeHolder: () => {},
			onAddHolder: () => {},
			isShowNote: true,
			isShowPackageNote: true,
			disabledEdit: true,
			isShowChangeResource: false,
			isShowChangeHolder: false,
			isShowDelete: true,
			isShowHolder: false,
			isShowAddHolderButton: false,
			isShowEditProduct: false,
			isShowAmountFooter: true,
			isShowImage: true,
			maxSpecsCount: 1,
			scenario: "booking2",
			onGift: () => {}
		});
	}));
}
//#endregion
exports.CartItems = CartItems;
