const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_constants = require("../constants.js");
const require_usePeripheralRuntime = require("../hooks/usePeripheralRuntime.js");
const require_peripheralCardModel = require("../utils/peripheralCardModel.js");
const require_PeripheralConnectionCard = require("./PeripheralConnectionCard.js");
const require_PhysicalDeviceSelectModal = require("./PhysicalDeviceSelectModal.js");
const require_PrinterUsageModal = require("./PrinterUsageModal.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
//#region src/plus/saasDevice/appPeripheral/components/PeripheralConnectionCardContainer.tsx
const getPeripheralName = (connection, translation) => {
	const peripheral = connection === null || connection === void 0 ? void 0 : connection.target_device;
	return peripheral ? translation(peripheral.name) || String(peripheral.id) : "-";
};
const PeripheralConnectionCardContainer = ({ row, variant, getText, translation, getAppInstance, onPhysicalChange, onUsageChange, onTest, onEdit, onRemove }) => {
	const [disconnecting, setDisconnecting] = (0, react.useState)(false);
	const { runtimeState, physicalSelect, printUsageSelect, isRuntimeSubmitting, setPhysicalSelect, setPrintUsageSelect, handleConnectPeripheral, handleDisconnectPeripheral, handleOpenUsageSelect, handleSelectPeripheralUsage, connectSelectedPhysicalDevice } = require_usePeripheralRuntime.usePeripheralRuntime({
		row,
		getAppInstance,
		getText,
		onPhysicalChange,
		onUsageChange
	});
	/** 视图模型只负责展示派生，稳定数据仍以 row 为准。 */
	const model = require_peripheralCardModel.createPeripheralConnectionCardModel({
		row,
		runtimeOverlay: runtimeState,
		getText,
		translation
	});
	const isPhone = variant === "phone";
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_PeripheralConnectionCard.default, {
		model,
		getText,
		onEdit,
		onRemove,
		onRequestDisconnect: () => setDisconnecting(true),
		onOpenUsageSelect: () => handleOpenUsageSelect(row),
		onTest: () => {
			onTest === null || onTest === void 0 || onTest({
				connection: row.connection,
				targetDevice: row.peripheral,
				physicalDevice: model.physicalDevice || row.physical,
				peripheralUsage: row.usage
			});
		},
		onOpenPhysicalSelect: () => {
			const candidates = runtimeState === null || runtimeState === void 0 ? void 0 : runtimeState.candidates;
			if (!(candidates === null || candidates === void 0 ? void 0 : candidates.length)) return;
			setPhysicalSelect({
				row,
				devices: candidates
			});
		},
		onConnect: () => handleConnectPeripheral(row)
	}), disconnecting ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: true,
		title: getText("pisell2.saas.app-peripheral.disconnect.title"),
		headerDivider: false,
		okText: getText("pisell2.saas.app-peripheral.confirm"),
		cancelText: getText("pisell2.saas.app-peripheral.cancel"),
		okButtonProps: {
			block: true,
			size: "large",
			danger: true,
			loading: isRuntimeSubmitting,
			onClick: function() {
				var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
					yield handleDisconnectPeripheral(row);
					setDisconnecting(false);
				});
				return function onClick() {
					return _ref.apply(this, arguments);
				};
			}()
		},
		cancelButtonProps: {
			block: true,
			size: "large",
			type: "default",
			disabled: isRuntimeSubmitting,
			onClick: () => setDisconnecting(false)
		},
		width: 460,
		platform: isPhone ? "h5" : void 0,
		mobileModalHeight: isPhone ? "auto" : void 0,
		className: (0, classnames.default)("saas-app-peripheral-confirm-modal", { "saas-app-peripheral-confirm-modal--phone": isPhone }),
		destroyOnClose: true,
		maskClosable: !isRuntimeSubmitting,
		onCancel: () => {
			if (isRuntimeSubmitting) return;
			setDisconnecting(false);
		}
	}, require_constants.formatToken(getText("pisell2.saas.app-peripheral.disconnect.confirm"), { name: getPeripheralName(row.connection, translation) })) : null, /* @__PURE__ */ react.default.createElement(require_PhysicalDeviceSelectModal.default, {
		state: physicalSelect,
		variant,
		submitting: isRuntimeSubmitting,
		getText,
		onClose: () => setPhysicalSelect(void 0),
		onSelect: (device) => {
			if (!physicalSelect) return;
			setPhysicalSelect(void 0);
			connectSelectedPhysicalDevice(physicalSelect.row, device);
		}
	}), /* @__PURE__ */ react.default.createElement(require_PrinterUsageModal.default, {
		state: printUsageSelect,
		variant,
		submitting: isRuntimeSubmitting,
		getText,
		onClose: () => setPrintUsageSelect(void 0),
		onSelect: (usage) => {
			handleSelectPeripheralUsage(usage);
		}
	}));
};
//#endregion
exports.default = PeripheralConnectionCardContainer;
