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_utils = require("../../../utils.js");
const require_ConnectExistingDeviceModal = require("./ConnectExistingDeviceModal.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
let _ant_design_icons = require("@ant-design/icons");
require("./DeviceConnectionsPanel.less");
//#region src/plus/saasDevice/devicePlanning/components/DeviceDetailDrawer/components/DeviceConnectionsPanel.tsx
const DeviceConnectionsPanel = ({ activeKind, currentDevice, connections, loading, submitting, deleting, getText, translation, onCreateConnection, onUpdateConnection, onDeleteConnection }) => {
	const [removingConnection, setRemovingConnection] = (0, react.useState)();
	const [connectMode, setConnectMode] = (0, react.useState)();
	const [editingConnection, setEditingConnection] = (0, react.useState)();
	if (activeKind === "integrated") return null;
	const isSmart = activeKind === "smart";
	const getConnectedDevice = (connection) => (isSmart ? connection.target_device : connection.source_device) || null;
	const getConnectedDeviceName = (connection) => {
		if (!connection) return "";
		const device = getConnectedDevice(connection);
		return device ? translation(device.name) || String(device.id) : "-";
	};
	const title = getText(isSmart ? "pisell2.saas.device-planning.drawer.connections.peripherals" : "pisell2.saas.device-planning.drawer.connections.connected-to");
	const emptyText = getText(isSmart ? "pisell2.saas.device-planning.drawer.connections.no-peripherals" : "pisell2.saas.device-planning.drawer.connections.not-connected-smart");
	const menuItems = [{
		key: "connect-existing",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.LinkOutlined, null),
		label: getText("pisell2.saas.device-planning.drawer.connections.connect-existing"),
		onClick: () => setConnectMode("existing")
	}, {
		key: "add-new",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.PlusOutlined, null),
		label: getText("pisell2.saas.device-planning.drawer.connections.add-new"),
		onClick: () => setConnectMode("new")
	}];
	const actionItems = (connection) => [{
		key: "edit",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.EditOutlined, null),
		label: getText("pisell2.saas.device-planning.drawer.connections.edit-configuration"),
		onClick: () => setEditingConnection(connection)
	}, {
		key: "remove",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.DeleteOutlined, null),
		danger: true,
		label: getText("pisell2.saas.device-planning.drawer.connections.remove"),
		onClick: () => setRemovingConnection(connection)
	}];
	const connectButton = isSmart ? /* @__PURE__ */ react.default.createElement(antd.Dropdown, {
		menu: { items: menuItems },
		trigger: ["click"],
		placement: "bottomRight"
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, { icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.PlusOutlined, null) }, getText("pisell2.saas.device-planning.drawer.connections.connect"))) : /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.PlusOutlined, null),
		onClick: () => setConnectMode("existing")
	}, getText("pisell2.saas.device-planning.drawer.connections.connect"));
	const renderProfileTags = (connection) => (connection.profiles || []).map((profile) => /* @__PURE__ */ react.default.createElement("span", {
		key: profile.id,
		className: "device-detail-drawer__connection-tag"
	}, translation(profile.name) || profile.id));
	const renderConnectionItem = (connection) => {
		var _connection$config, _device$type;
		const device = getConnectedDevice(connection);
		const connectionType = require_utils.getConnectionTypeLabel((_connection$config = connection.config) === null || _connection$config === void 0 ? void 0 : _connection$config.connection_type);
		return /* @__PURE__ */ react.default.createElement("div", {
			key: connection.id,
			className: "device-detail-drawer__connection-item"
		}, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connection-icon" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: (device === null || device === void 0 || (_device$type = device.type) === null || _device$type === void 0 ? void 0 : _device$type.icon) || (isSmart ? "PrinterOutlined" : "DesktopOutlined") })), /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connection-main" }, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connection-name" }, device ? translation(device.name) || device.id : "-"), /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connection-meta" }, /* @__PURE__ */ react.default.createElement("span", null, connectionType), renderProfileTags(connection))), /* @__PURE__ */ react.default.createElement(antd.Dropdown, {
			menu: { items: actionItems(connection) },
			trigger: ["click"],
			placement: "bottomRight"
		}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "text",
			icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.MoreOutlined, null),
			className: "device-detail-drawer__connection-more"
		})));
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connections" }, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connections-header" }, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connections-title" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: isSmart ? "ApiOutlined" : "LinkOutlined" }), /* @__PURE__ */ react.default.createElement("span", null, title)), connectButton), /* @__PURE__ */ react.default.createElement(antd.Spin, { spinning: loading }, connections.length ? /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connection-list" }, connections.map(renderConnectionItem)) : /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__connections-empty" }, /* @__PURE__ */ react.default.createElement(antd.Empty, {
		image: antd.Empty.PRESENTED_IMAGE_SIMPLE,
		description: emptyText
	}))), removingConnection ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: true,
		title: getText("pisell2.saas.device-planning.drawer.connections.remove-title"),
		onCancel: () => setRemovingConnection(void 0),
		okText: getText("pisell2.saas.device-planning.drawer.connections.remove"),
		cancelText: getText("pisell2.saas.device-planning.drawer.connections.cancel"),
		okButtonProps: {
			block: true,
			size: "large",
			danger: true,
			loading: deleting,
			onClick: function() {
				var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
					yield onDeleteConnection(removingConnection.id);
					setRemovingConnection(void 0);
				});
				return function onClick() {
					return _ref.apply(this, arguments);
				};
			}()
		},
		cancelButtonProps: {
			block: true,
			size: "large",
			type: "default",
			disabled: deleting,
			onClick: () => setRemovingConnection(void 0)
		},
		width: 460,
		headerDivider: false
	}, require_constants.formatToken(getText("pisell2.saas.device-planning.drawer.connections.remove-confirm"), { DeviceName: getConnectedDeviceName(removingConnection) })) : null, connectMode ? /* @__PURE__ */ react.default.createElement(require_ConnectExistingDeviceModal.default, {
		open: true,
		mode: connectMode,
		currentDevice,
		connectedDeviceIds: connections.map((connection) => activeKind === "smart" ? connection.target_device_id : connection.source_device_id),
		currentWorkAreaId: currentDevice.workarea_id,
		getText,
		translation,
		submitting,
		onClose: () => setConnectMode(void 0),
		onSubmit: function() {
			var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (payload) {
				yield onCreateConnection(payload);
				setConnectMode(void 0);
			});
			return function(_x) {
				return _ref2.apply(this, arguments);
			};
		}()
	}) : null, editingConnection ? /* @__PURE__ */ react.default.createElement(require_ConnectExistingDeviceModal.default, {
		open: true,
		mode: "edit",
		currentDevice,
		editConnection: editingConnection,
		connectedDeviceIds: [],
		currentWorkAreaId: currentDevice.workarea_id,
		getText,
		translation,
		submitting,
		onClose: () => setEditingConnection(void 0),
		onUpdate: function() {
			var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (id, payload) {
				yield onUpdateConnection(id, payload);
				setEditingConnection(void 0);
			});
			return function(_x2, _x3) {
				return _ref3.apply(this, arguments);
			};
		}()
	}) : null);
};
//#endregion
exports.default = DeviceConnectionsPanel;
