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_types = require("../../types.js");
const require_index = require("../CupInfoCard/index.js");
const require_index$1 = require("../CustomerCard/index.js");
const require_hooks = require("./hooks.js");
const require_index$2 = require("../SearchBar/index.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/ecocup/components/CupStatusModal/index.tsx
const SCAN_LISTENER_KEY = "eco-cup-status-modal-scan-listener";
const CupStatusModal = (props) => {
	var _cupStatistics$unique, _cupList$, _cupList$2;
	const { visible, onCancel, onOk, data } = props;
	const { scanStatus, cupData } = data || {};
	const cupAction = require_hooks.useCupAction();
	const [cupList, setCupList] = (0, react.useState)([]);
	const batchCupAction = require_hooks.useBatchCupAction();
	(0, react.useEffect)(() => {
		if (!cupData) {
			setCupList([]);
			return;
		}
		setCupList(Array.isArray(cupData) ? cupData : [cupData]);
	}, [cupData]);
	/**
	* @description: OTHER_RENT 状态
	* @param {*} useMemo
	* @return {*}
	*/
	const otherRentStatus = (0, react.useMemo)(() => {
		return scanStatus === require_types.ECupScanStatus.OTHER_RENT;
	}, [scanStatus]);
	const isRed = !otherRentStatus;
	const cupStatistics = require_hooks.useCupListStatistics(cupList);
	const lineStyle = (0, react.useMemo)(() => {
		if (isRed) return {
			backgroundColor: "#FEE4E2",
			color: "#F04438"
		};
		return {
			backgroundColor: "#F6F2FF",
			color: "#7F56DA"
		};
	}, [isRed]);
	const cupInfoCardType = (0, react.useMemo)(() => {
		if (scanStatus === require_types.ECupScanStatus.OTHER_IN_STOCK) return "otherBelongInfo";
		if (scanStatus === require_types.ECupScanStatus.IN_STOCK) return "belongInfo";
		if (otherRentStatus || scanStatus === require_types.ECupScanStatus.RENT) return "rentInfo";
		if (scanStatus === require_types.ECupScanStatus.MISSING || scanStatus === require_types.ECupScanStatus.BROKEN) return "markInfo";
		return "";
	}, [scanStatus]);
	const _onCancel = () => {
		if (cupAction === null || cupAction === void 0 ? void 0 : cupAction.loading) return;
		onCancel === null || onCancel === void 0 || onCancel();
	};
	const _onOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (cupAction === null || cupAction === void 0 ? void 0 : cupAction.loading) return;
			const type = otherRentStatus ? require_types.ECupActionType.RETURN : "";
			if (!type) {
				onOk === null || onOk === void 0 || onOk();
				return;
			}
			let result;
			if (otherRentStatus) {
				const ids = cupList.map((cup) => (cup === null || cup === void 0 ? void 0 : cup.eco_cup_id) || (cup === null || cup === void 0 ? void 0 : cup.id));
				result = yield batchCupAction.run(ids);
			} else result = yield cupAction.run((cupData === null || cupData === void 0 ? void 0 : cupData.eco_cup_id) || (cupData === null || cupData === void 0 ? void 0 : cupData.id), type);
			if (result) {
				(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText(`pisell2.text.ecocup.toast.success-${type}`) });
				onOk === null || onOk === void 0 || onOk();
			}
		});
		return function _onOk() {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @description: 搜索结果回调，将新杯子添加到列表
	*/
	const onSearchResult = (result) => {
		var _result$data2;
		if (cupList.find((c) => {
			var _result$data;
			return (c === null || c === void 0 ? void 0 : c.id) === (result === null || result === void 0 || (_result$data = result.data) === null || _result$data === void 0 ? void 0 : _result$data.id);
		})) return;
		if ((result === null || result === void 0 || (_result$data2 = result.data) === null || _result$data2 === void 0 ? void 0 : _result$data2.status) !== require_types.ECupStatus.RENT) return;
		setCupList([...cupList, result === null || result === void 0 ? void 0 : result.data]);
	};
	/**
	* @description: 从列表中移除杯子
	*/
	const removeCupFromList = (cupId) => {
		setCupList(cupList.filter((cup) => (cup === null || cup === void 0 ? void 0 : cup.id) !== cupId));
	};
	const renderSearchBar = () => {
		return /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar" }, /* @__PURE__ */ react.default.createElement(require_index$2.default, {
			scanListenerKey: SCAN_LISTENER_KEY,
			searchType: "cup",
			placeholder: _pisell_utils.locales.getText("pisell2.text.ecocup.customer-first-modal.search-bar.placeholder"),
			onSearchResult,
			onCupStatusValidate: onSearchResult
		}));
	};
	const showRemove = (0, react.useMemo)(() => {
		return (cupList === null || cupList === void 0 ? void 0 : cupList.length) > 1 && otherRentStatus;
	}, [cupList.length, otherRentStatus]);
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		headerDivider: false,
		open: visible,
		onCancel: _onCancel,
		onOk: _onOk,
		title: otherRentStatus ? _pisell_utils.locales.getText(`pisell2.text.ecocup.cup-status-modal.title-${scanStatus}`)(cupList.length) : _pisell_utils.locales.getText(`pisell2.text.ecocup.cup-status-modal.title-${scanStatus}`),
		cancelText: otherRentStatus ? _pisell_utils.locales.getText("pisell2.text.ecocup.button.cancel") : _pisell_utils.locales.getText("pisell2.text.ecocup.button.dismiss"),
		okText: isRed ? _pisell_utils.locales.getText("pisell2.text.ecocup.button.confirm") : _pisell_utils.locales.getText("pisell2.text.ecocup.button.return"),
		okButtonProps: { loading: cupAction === null || cupAction === void 0 ? void 0 : cupAction.loading },
		cancelVisible: !isRed,
		width: 580
	}, /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell2-ecocup-cup-status-modal-content",
		style: { gap: otherRentStatus ? "20px" : "32px" }
	}, otherRentStatus ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-search" }, /* @__PURE__ */ react.default.createElement("span", { className: "tip-text" }, _pisell_utils.locales.getText("pisell2.text.ecocup.cup-status-modal.search-bar.title")), renderSearchBar()) : null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-cup-lists" }, cupList.map((cup, index) => /* @__PURE__ */ react.default.createElement("div", {
		key: (cup === null || cup === void 0 ? void 0 : cup.id) || index,
		className: "pisell2-ecocup-cup-status-modal-content-cup",
		style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, lineStyle), {}, { justifyContent: showRemove ? "space-between" : "center" })
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-cup-info" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
		type: "RestOutlined",
		size: 24
	}), /* @__PURE__ */ react.default.createElement("span", { style: { color: !isRed ? "#667085" : "#F04438" } }, _pisell_utils.locales.getText("pisell2.text.ecocup.cup-id")), /* @__PURE__ */ react.default.createElement("span", null, (cup === null || cup === void 0 ? void 0 : cup.eco_cup_code) || (cup === null || cup === void 0 ? void 0 : cup.code) || "")), showRemove && /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
			type: "pisell2-trash-01",
			size: 24,
			color: "#7F56DA"
		}),
		onClick: () => removeCupFromList(cup === null || cup === void 0 ? void 0 : cup.id),
		className: "info-remove"
	}, _pisell_utils.locales.getText("pisell2.text.ecocup.button.remove"))))), /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-info" }, /* @__PURE__ */ react.default.createElement("div", { style: { flex: 1 } }, ((cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.uniqueShop) || (cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.hasMultipleShops)) && cupInfoCardType ? /* @__PURE__ */ react.default.createElement(require_index.default, {
		type: cupInfoCardType,
		data: {
			shop: cupStatistics === null || cupStatistics === void 0 || (_cupStatistics$unique = cupStatistics.uniqueShop) === null || _cupStatistics$unique === void 0 ? void 0 : _cupStatistics$unique.name,
			time: cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.uniqueTime,
			isMultipleTime: cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.hasMultipleTimes,
			isMultipleShop: cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.hasMultipleShops
		}
	}) : null), /* @__PURE__ */ react.default.createElement("div", { style: { flex: 1 } }, (cupList === null || cupList === void 0 || (_cupList$ = cupList[0]) === null || _cupList$ === void 0 ? void 0 : _cupList$.status) === require_types.ECupStatus.RENT ? (cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.uniqueCustomer) || (cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.hasMultipleCustomers) ? /* @__PURE__ */ react.default.createElement(require_index$1.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.uniqueCustomer) || {}), {}, { isMultiple: cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.hasMultipleCustomers })) : null : /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-info-tip" }, (cupList === null || cupList === void 0 || (_cupList$2 = cupList[0]) === null || _cupList$2 === void 0 ? void 0 : _cupList$2.status) === require_types.ECupStatus.IN_STOCK ? _pisell_utils.locales.getText("pisell2.text.ecocup.cup-status-modal.tip-in-stock") : _pisell_utils.locales.getText("pisell2.text.ecocup.cup-status-modal.tip"))))));
};
//#endregion
exports.default = CupStatusModal;
