import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import PisellNumberSelector from "../../../../pro/pisellNumberSelector/index.js";
import React, { useCallback, useMemo, useState } from "react";
import { message } from "antd";
import { locales } from "@pisell/utils";
import { ProductCard } from "@pisell/materials";
import "./index.less";
//#region src/plus/saleDetail/components/CartItems/index.tsx
const { LineItem } = 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;
};
function CartItems({ dataSource, sales, className }) {
	const [statusLoading, setStatusLoading] = useState(false);
	const cart = Array.isArray(dataSource) ? {
		bookings: [],
		items: dataSource
	} : dataSource || {
		bookings: [],
		items: []
	};
	const bookings = cart.bookings || [];
	const items = cart.items || [];
	const statusOptions = useMemo(() => STATUS_OPTIONS.map((option) => ({
		key: option.key,
		label: locales.getText(option.labelKey),
		color: option.color,
		danger: option.danger
	})), []);
	useCallback(function() {
		var _ref3 = _asyncToGenerator(function* (status) {
			const next = status.trim();
			if (!next) {
				message.error(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);
				message.success(locales.getText("pisell1.text.edit-status-success"));
			} catch (e) {
				var _e$message;
				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 renderQuantityAction = (item) => [{
		key: "update_number",
		text: /* @__PURE__ */ React.createElement(PisellNumberSelector, {
			enablePriceAdjust: true,
			min: 1,
			className: "pisell-sale-detail-cart-items__quantity-selector",
			value: getBookingQuantity(item),
			onChange: () => {}
		}),
		color: "#667085"
	}];
	return /* @__PURE__ */ React.createElement("section", { className: className ? `pisell-sale-detail-cart-items ${className}` : "pisell-sale-detail-cart-items" }, bookings.map((item) => {
		var _item$metadata;
		return /* @__PURE__ */ React.createElement(LineItem, {
			key: ((_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.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: () => {},
			onNote: () => {},
			onCard: () => {},
			onPromotion: () => {},
			isShowNote: true,
			disabledEdit: true,
			isShowDelete: false,
			isShowAmountFooter: true,
			isShowImage: true,
			maxSpecsCount: 1,
			scenario: "booking2",
			onGift: () => {}
		});
	}), items.map((item) => {
		var _item$metadata2;
		return /* @__PURE__ */ React.createElement(LineItem, {
			key: ((_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.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: () => {},
			onNote: () => {},
			onLike: () => {},
			onEditProduct: () => {},
			onCard: () => {},
			onChangeResource: () => {},
			onChangeHolder: () => {},
			onAddHolder: () => {},
			isShowNote: true,
			isShowPackageNote: true,
			disabledEdit: true,
			isShowChangeResource: false,
			isShowChangeHolder: false,
			isShowDelete: false,
			isShowHolder: false,
			isShowAddHolderButton: false,
			isShowEditProduct: false,
			isShowAmountFooter: true,
			isShowImage: true,
			maxSpecsCount: 1,
			scenario: "booking2",
			onGift: () => {}
		});
	}));
}
//#endregion
export { CartItems };
