const require_runtime = require("../../../../_virtual/_rolldown/runtime.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("../CustomerCard/index.js");
const require_index$1 = require("../SearchBar/index.js");
const require_index$2 = require("../CupActionList/index.js");
const require_hooks = require("./hooks.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/ecocup/components/CustomerAndCupModal/index.tsx
/**
* 先选择的客户，然后选择杯子
*/
const SCAN_LISTENER_KEY = "eco-cup-customer-first-modal-scan-listener";
const CustomerAndCupModal = (props) => {
	const { key, title, visible, onCancel, onOk, data } = props;
	const [customerData, setCustomerData] = (0, react.useState)();
	const [rentedCups, setRentedCups] = (0, react.useState)([]);
	const [newCups, setNewCups] = (0, react.useState)([]);
	const [maxCupsNum, setMaxCupsNum] = (0, react.useState)(0);
	const customerDetail = require_hooks.useCustomerDetail();
	const batchRentCups = require_hooks.useBatchRentCups();
	const currentCupsNum = (0, react.useMemo)(() => {
		return ((rentedCups === null || rentedCups === void 0 ? void 0 : rentedCups.length) || 0) + ((newCups === null || newCups === void 0 ? void 0 : newCups.length) || 0);
	}, [rentedCups, newCups]);
	const isOverMax = (0, react.useMemo)(() => {
		return currentCupsNum > maxCupsNum;
	}, [currentCupsNum, maxCupsNum]);
	const isLoading = (0, react.useMemo)(() => {
		return (customerDetail === null || customerDetail === void 0 ? void 0 : customerDetail.loading) || (batchRentCups === null || batchRentCups === void 0 ? void 0 : batchRentCups.loading);
	}, [customerDetail === null || customerDetail === void 0 ? void 0 : customerDetail.loading, batchRentCups === null || batchRentCups === void 0 ? void 0 : batchRentCups.loading]);
	(0, react.useEffect)(() => {
		setCustomerData((data === null || data === void 0 ? void 0 : data.customerData) || void 0);
	}, [data === null || data === void 0 ? void 0 : data.customerData]);
	const _getCustomerDetail = (0, ahooks.useMemoizedFn)((id) => {
		if (!id) return;
		customerDetail.run(id).then((res) => {
			console.log("res", res);
			if (res) {
				setMaxCupsNum((res === null || res === void 0 ? void 0 : res.max_rent_count) || 0);
				setRentedCups((res === null || res === void 0 ? void 0 : res.rent_cups) || []);
			} else {
				setMaxCupsNum(0);
				setRentedCups([]);
			}
		});
	});
	(0, react.useEffect)(() => {
		if ((data === null || data === void 0 ? void 0 : data.firstType) === "cup") !(data === null || data === void 0) && data.cupData && setNewCups([data === null || data === void 0 ? void 0 : data.cupData]);
		else {
			var _data$customerData;
			_getCustomerDetail(data === null || data === void 0 || (_data$customerData = data.customerData) === null || _data$customerData === void 0 ? void 0 : _data$customerData.id);
		}
	}, [
		data === null || data === void 0 ? void 0 : data.firstType,
		data === null || data === void 0 ? void 0 : data.customerData,
		data === null || data === void 0 ? void 0 : data.cupData,
		_getCustomerDetail
	]);
	const _onCancel = () => {
		if (isLoading) return;
		onCancel === null || onCancel === void 0 || onCancel();
	};
	const _onOk = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (isLoading || !(customerData === null || customerData === void 0 ? void 0 : customerData.id)) return;
			const cupIds = newCups.map((c) => c === null || c === void 0 ? void 0 : c.id);
			if (!(yield batchRentCups.run(customerData === null || customerData === void 0 ? void 0 : customerData.id, cupIds))) return;
			setNewCups([]);
			_getCustomerDetail(customerData === null || customerData === void 0 ? void 0 : customerData.id);
		});
		return function _onOk() {
			return _ref.apply(this, arguments);
		};
	}();
	const onSearchResult = (result) => {
		console.log("onSearchResult", result);
		if ((result === null || result === void 0 ? void 0 : result.type) === "cup") {
			var _result$data2;
			if (newCups.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.IN_STOCK) return;
			setNewCups([...newCups, result === null || result === void 0 ? void 0 : result.data]);
		} else {
			var _result$data3;
			setCustomerData(result === null || result === void 0 ? void 0 : result.data);
			_getCustomerDetail(result === null || result === void 0 || (_result$data3 = result.data) === null || _result$data3 === void 0 ? void 0 : _result$data3.id);
		}
	};
	const renderCustomerCard = () => {
		return /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-title" }, "Current Customer"), (customerData === null || customerData === void 0 ? void 0 : customerData.id) ? /* @__PURE__ */ react.default.createElement(require_index.default, customerData || {}) : /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-no-customer" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-no-customer-icon" }), /* @__PURE__ */ react.default.createElement("span", { className: "pisell2-ecocup-customer-first-modal-no-customer-text" }, _pisell_utils.locales.getText("pisell2.text.ecocup.customer-first-modal.no-customer"))));
	};
	const renderSearchBar = () => {
		return /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar" }, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
			scanListenerKey: `${key}-${SCAN_LISTENER_KEY}`,
			searchType: (customerData === null || customerData === void 0 ? void 0 : customerData.id) ? "cup" : void 0,
			placeholder: (customerData === null || customerData === void 0 ? void 0 : customerData.id) ? _pisell_utils.locales.getText("pisell2.text.ecocup.customer-first-modal.search-bar.placeholder-cup") : _pisell_utils.locales.getText("pisell2.text.ecocup.customer-first-modal.search-bar.placeholder"),
			onSearchResult,
			customerId: customerData === null || customerData === void 0 ? void 0 : customerData.id
		}), /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar-limit" }, /* @__PURE__ */ react.default.createElement("span", null, "Max"), /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("span", { style: { color: isOverMax ? "#CF1322" : "#000000" } }, currentCupsNum), "/", maxCupsNum)));
	};
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: visible,
		title: title || _pisell_utils.locales.getText("pisell2.text.ecocup.customer-first-modal.title"),
		headerDivider: false,
		onCancel: _onCancel,
		onOk: _onOk,
		okText: _pisell_utils.locales.getText("pisell2.text.ecocup.button.confirm"),
		cancelText: _pisell_utils.locales.getText("pisell2.text.ecocup.button.dismiss"),
		okButtonProps: {
			disabled: newCups.length === 0 || isOverMax || isLoading || !(customerData === null || customerData === void 0 ? void 0 : customerData.id),
			loading: isLoading
		},
		width: 580
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell2-ecocup-customer-first-modal" }, (data === null || data === void 0 ? void 0 : data.firstType) === "customer" ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, renderCustomerCard(), renderSearchBar()) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, renderSearchBar(), renderCustomerCard()), /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		maxNum: maxCupsNum,
		currentNum: currentCupsNum,
		cups: [...rentedCups, ...newCups],
		customerData,
		onClearAll: () => setNewCups([]),
		onRemove: (cup) => {
			setNewCups(newCups.filter((c) => (c === null || c === void 0 ? void 0 : c.id) !== (cup === null || cup === void 0 ? void 0 : cup.id)));
		},
		onAction: (type, cup) => {
			const key = (cup === null || cup === void 0 ? void 0 : cup.eco_cup_id) ? "eco_cup_id" : "id";
			setRentedCups(rentedCups.filter((c) => (c === null || c === void 0 ? void 0 : c[key]) !== (cup === null || cup === void 0 ? void 0 : cup[key])));
		}
	})));
};
//#endregion
exports.default = CustomerAndCupModal;
