import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { ECupStatus } from "../../types.js";
import CustomerCard from "../CustomerCard/index.js";
import SearchBar from "../SearchBar/index.js";
import CupActionList from "../CupActionList/index.js";
import { useBatchRentCups, useCustomerDetail } from "./hooks.js";
import React, { useEffect, useMemo, useState } from "react";
import { locales } from "@pisell/utils";
import { PisellModal } from "@pisell/materials";
import { useMemoizedFn } from "ahooks";
import "./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] = useState();
	const [rentedCups, setRentedCups] = useState([]);
	const [newCups, setNewCups] = useState([]);
	const [maxCupsNum, setMaxCupsNum] = useState(0);
	const customerDetail = useCustomerDetail();
	const batchRentCups = useBatchRentCups();
	const currentCupsNum = 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 = useMemo(() => {
		return currentCupsNum > maxCupsNum;
	}, [currentCupsNum, maxCupsNum]);
	const isLoading = 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]);
	useEffect(() => {
		setCustomerData((data === null || data === void 0 ? void 0 : data.customerData) || void 0);
	}, [data === null || data === void 0 ? void 0 : data.customerData]);
	const _getCustomerDetail = 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([]);
			}
		});
	});
	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 = _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) !== 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.createElement("div", null, /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-customer-first-modal-title" }, "Current Customer"), (customerData === null || customerData === void 0 ? void 0 : customerData.id) ? /* @__PURE__ */ React.createElement(CustomerCard, customerData || {}) : /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-customer-first-modal-no-customer" }, /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-customer-first-modal-no-customer-icon" }), /* @__PURE__ */ React.createElement("span", { className: "pisell2-ecocup-customer-first-modal-no-customer-text" }, locales.getText("pisell2.text.ecocup.customer-first-modal.no-customer"))));
	};
	const renderSearchBar = () => {
		return /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar" }, /* @__PURE__ */ React.createElement(SearchBar, {
			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) ? locales.getText("pisell2.text.ecocup.customer-first-modal.search-bar.placeholder-cup") : locales.getText("pisell2.text.ecocup.customer-first-modal.search-bar.placeholder"),
			onSearchResult,
			customerId: customerData === null || customerData === void 0 ? void 0 : customerData.id
		}), /* @__PURE__ */ React.createElement("div", { className: "pisell2-ecocup-customer-first-modal-search-bar-limit" }, /* @__PURE__ */ React.createElement("span", null, "Max"), /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement("span", { style: { color: isOverMax ? "#CF1322" : "#000000" } }, currentCupsNum), "/", maxCupsNum)));
	};
	return /* @__PURE__ */ React.createElement(PisellModal, {
		open: visible,
		title: title || locales.getText("pisell2.text.ecocup.customer-first-modal.title"),
		headerDivider: false,
		onCancel: _onCancel,
		onOk: _onOk,
		okText: locales.getText("pisell2.text.ecocup.button.confirm"),
		cancelText: 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.createElement("div", { className: "pisell2-ecocup-customer-first-modal" }, (data === null || data === void 0 ? void 0 : data.firstType) === "customer" ? /* @__PURE__ */ React.createElement(React.Fragment, null, renderCustomerCard(), renderSearchBar()) : /* @__PURE__ */ React.createElement(React.Fragment, null, renderSearchBar(), renderCustomerCard()), /* @__PURE__ */ React.createElement(CupActionList, {
		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
export { CustomerAndCupModal as default };
