import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { ECupActionType, ECupScanStatus, ECupStatus } from "../../types.js";
import CupInfoCard from "../CupInfoCard/index.js";
import CustomerCard from "../CustomerCard/index.js";
import { useBatchCupAction, useCupAction, useCupListStatistics } from "./hooks.js";
import SearchBar from "../SearchBar/index.js";
import React, { useEffect, useMemo, useState } from "react";
import { locales } from "@pisell/utils";
import { Button, Icon, PisellModal, PisellToast } from "@pisell/materials";
import "./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 = useCupAction();
	const [cupList, setCupList] = useState([]);
	const batchCupAction = useBatchCupAction();
	useEffect(() => {
		if (!cupData) {
			setCupList([]);
			return;
		}
		setCupList(Array.isArray(cupData) ? cupData : [cupData]);
	}, [cupData]);
	/**
	* @description: OTHER_RENT 状态
	* @param {*} useMemo
	* @return {*}
	*/
	const otherRentStatus = useMemo(() => {
		return scanStatus === ECupScanStatus.OTHER_RENT;
	}, [scanStatus]);
	const isRed = !otherRentStatus;
	const cupStatistics = useCupListStatistics(cupList);
	const lineStyle = useMemo(() => {
		if (isRed) return {
			backgroundColor: "#FEE4E2",
			color: "#F04438"
		};
		return {
			backgroundColor: "#F6F2FF",
			color: "#7F56DA"
		};
	}, [isRed]);
	const cupInfoCardType = useMemo(() => {
		if (scanStatus === ECupScanStatus.OTHER_IN_STOCK) return "otherBelongInfo";
		if (scanStatus === ECupScanStatus.IN_STOCK) return "belongInfo";
		if (otherRentStatus || scanStatus === ECupScanStatus.RENT) return "rentInfo";
		if (scanStatus === ECupScanStatus.MISSING || scanStatus === 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 = _asyncToGenerator(function* () {
			if (cupAction === null || cupAction === void 0 ? void 0 : cupAction.loading) return;
			const type = otherRentStatus ? 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) {
				PisellToast({ content: 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) !== 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.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar" }, /* @__PURE__ */ React.createElement(SearchBar, {
			scanListenerKey: SCAN_LISTENER_KEY,
			searchType: "cup",
			placeholder: locales.getText("pisell2.text.ecocup.customer-first-modal.search-bar.placeholder"),
			onSearchResult,
			onCupStatusValidate: onSearchResult
		}));
	};
	const showRemove = useMemo(() => {
		return (cupList === null || cupList === void 0 ? void 0 : cupList.length) > 1 && otherRentStatus;
	}, [cupList.length, otherRentStatus]);
	return /* @__PURE__ */ React.createElement(PisellModal, {
		headerDivider: false,
		open: visible,
		onCancel: _onCancel,
		onOk: _onOk,
		title: otherRentStatus ? locales.getText(`pisell2.text.ecocup.cup-status-modal.title-${scanStatus}`)(cupList.length) : locales.getText(`pisell2.text.ecocup.cup-status-modal.title-${scanStatus}`),
		cancelText: otherRentStatus ? locales.getText("pisell2.text.ecocup.button.cancel") : locales.getText("pisell2.text.ecocup.button.dismiss"),
		okText: isRed ? locales.getText("pisell2.text.ecocup.button.confirm") : locales.getText("pisell2.text.ecocup.button.return"),
		okButtonProps: { loading: cupAction === null || cupAction === void 0 ? void 0 : cupAction.loading },
		cancelVisible: !isRed,
		width: 580
	}, /* @__PURE__ */ React.createElement("div", {
		className: "pisell2-ecocup-cup-status-modal-content",
		style: { gap: otherRentStatus ? "20px" : "32px" }
	}, otherRentStatus ? /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-search" }, /* @__PURE__ */ React.createElement("span", { className: "tip-text" }, locales.getText("pisell2.text.ecocup.cup-status-modal.search-bar.title")), renderSearchBar()) : null, /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-cup-lists" }, cupList.map((cup, index) => /* @__PURE__ */ React.createElement("div", {
		key: (cup === null || cup === void 0 ? void 0 : cup.id) || index,
		className: "pisell2-ecocup-cup-status-modal-content-cup",
		style: _objectSpread2(_objectSpread2({}, lineStyle), {}, { justifyContent: showRemove ? "space-between" : "center" })
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-cup-info" }, /* @__PURE__ */ React.createElement(Icon, {
		type: "RestOutlined",
		size: 24
	}), /* @__PURE__ */ React.createElement("span", { style: { color: !isRed ? "#667085" : "#F04438" } }, locales.getText("pisell2.text.ecocup.cup-id")), /* @__PURE__ */ React.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.createElement(Button, {
		type: "primary",
		icon: /* @__PURE__ */ React.createElement(Icon, {
			type: "pisell2-trash-01",
			size: 24,
			color: "#7F56DA"
		}),
		onClick: () => removeCupFromList(cup === null || cup === void 0 ? void 0 : cup.id),
		className: "info-remove"
	}, locales.getText("pisell2.text.ecocup.button.remove"))))), /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-cup-status-modal-content-info" }, /* @__PURE__ */ React.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.createElement(CupInfoCard, {
		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.createElement("div", { style: { flex: 1 } }, (cupList === null || cupList === void 0 || (_cupList$ = cupList[0]) === null || _cupList$ === void 0 ? void 0 : _cupList$.status) === ECupStatus.RENT ? (cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.uniqueCustomer) || (cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.hasMultipleCustomers) ? /* @__PURE__ */ React.createElement(CustomerCard, _objectSpread2(_objectSpread2({}, (cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.uniqueCustomer) || {}), {}, { isMultiple: cupStatistics === null || cupStatistics === void 0 ? void 0 : cupStatistics.hasMultipleCustomers })) : null : /* @__PURE__ */ React.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) === ECupStatus.IN_STOCK ? locales.getText("pisell2.text.ecocup.cup-status-modal.tip-in-stock") : locales.getText("pisell2.text.ecocup.cup-status-modal.tip"))))));
};
//#endregion
export { CupStatusModal as default };
