import { useScanOrderContext } from "../../context.js";
import React, { useEffect, useMemo, useState } from "react";
import { Radio } from "antd";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/scanOrder/components/FulfillmentMode/index.tsx
/**
* FulfillmentMode
*
* 通过 scanOrder module 读取 getFulfillmentModes() 返回的 { enableTableService, enablePickup }：
* - 两个都开启：渲染可切换的 Radio.Group，默认 Table service，挂载即同步一次 setPickupReferenceMode。
* - 只开启一个：仅渲染单行文案提示当前取餐方式，并同步一次 setPickupReferenceMode。
* - 都未开启：不渲染。
*/
function FulfillmentMode() {
	const { scanOrderModule } = useScanOrderContext();
	const [modes, setModes] = useState({});
	const [mode, setMode] = useState("table_service");
	useEffect(() => {
		if (!scanOrderModule) return;
		const next = (typeof scanOrderModule.getFulfillmentModes === "function" ? scanOrderModule.getFulfillmentModes() : void 0) || {};
		setModes(next);
		const onlyTable = !!next.enableTableService && !next.enablePickup;
		const initial = !next.enableTableService && !!next.enablePickup ? "counter_pickup" : onlyTable ? "table_service" : "table_service";
		setMode(initial);
		if ((next.enableTableService || next.enablePickup) && typeof scanOrderModule.setPickupReferenceMode === "function") scanOrderModule.setPickupReferenceMode(initial);
	}, [scanOrderModule]);
	const { enableTableService, enablePickup } = modes;
	const labelTitle = useMemo(() => String(locales.getText("pisell2.scan.order.fulfillment.title")), []);
	const tableServiceText = useMemo(() => String(locales.getText("pisell2.scan.order.fulfillment.table_service")), []);
	const pickupText = useMemo(() => String(locales.getText("pisell2.scan.order.fulfillment.pickup")), []);
	if (!enableTableService && !enablePickup) return null;
	if (!enableTableService || !enablePickup) {
		const singleLabel = enablePickup ? pickupText : tableServiceText;
		return /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-fulfillment pisell-scan-order-fulfillment-single" }, /* @__PURE__ */ React.createElement("span", { className: "pisell-scan-order-fulfillment-title" }, labelTitle), /* @__PURE__ */ React.createElement("span", { className: "pisell-scan-order-fulfillment-value" }, singleLabel));
	}
	const handleChange = (next) => {
		if (next === mode) return;
		setMode(next);
		if (typeof (scanOrderModule === null || scanOrderModule === void 0 ? void 0 : scanOrderModule.setPickupReferenceMode) === "function") scanOrderModule.setPickupReferenceMode(next);
	};
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-fulfillment" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-scan-order-fulfillment-title" }, labelTitle), /* @__PURE__ */ React.createElement(Radio.Group, {
		className: "pisell-scan-order-fulfillment-group",
		optionType: "button",
		buttonStyle: "solid",
		value: mode,
		onChange: (event) => {
			var _event$target;
			return handleChange(event === null || event === void 0 || (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.value);
		}
	}, /* @__PURE__ */ React.createElement(Radio.Button, { value: "table_service" }, tableServiceText), /* @__PURE__ */ React.createElement(Radio.Button, { value: "counter_pickup" }, pickupText)));
}
//#endregion
export { FulfillmentMode as default };
