const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../pro/compensatedPisellContainer/index.js");
const require_withResponsive = require("../../../responsive/withResponsive.js");
const require_index$1 = require("../../../utils/index.js");
const require_useLocale = require("../../../hooks/useLocale.js");
const require_serve = require("./serve.js");
const require_hooks = require("./hooks.js");
const require_locales = require("./locales.js");
const require_constants = require("./constants.js");
const require_index$2 = require("./components/WorkAreaList/index.js");
const require_index$3 = require("./components/DeviceTable/index.js");
const require_index$4 = require("./components/AddDeviceModal/index.js");
const require_index$5 = require("./components/EditDeviceModal/index.js");
const require_index$6 = require("./components/DeleteDeviceModal/index.js");
const require_index$7 = require("./components/WorkAreaModal/index.js");
const require_index$8 = require("./components/DeleteWorkAreaModal/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
let _ant_design_icons = require("@ant-design/icons");
require("./index.less");
//#region src/plus/saasDevice/appDevicePlanning/index.tsx
const EMPTY_DEVICES = [];
const TYPE_CODE_STORAGE_KEY = "pisell_iot_type_code";
const MODEL_CODE_STORAGE_KEY = "pisell_iot_model_code";
const APP_TYPE_STORAGE_KEY = "pisell_app_type";
const getStoredIotDevice = (app) => {
	try {
		var _app$storage, _app$storage2;
		const iotDevice = JSON.parse((app === null || app === void 0 || (_app$storage = app.storage) === null || _app$storage === void 0 ? void 0 : _app$storage.getStorage("iotDevice")) || "{}");
		const iotDeviceLastUsed = JSON.parse((app === null || app === void 0 || (_app$storage2 = app.storage) === null || _app$storage2 === void 0 ? void 0 : _app$storage2.getStorage("iotDeviceLastUsed")) || "{}");
		return {
			id: (iotDeviceLastUsed === null || iotDeviceLastUsed === void 0 ? void 0 : iotDeviceLastUsed.id) !== void 0 && (iotDeviceLastUsed === null || iotDeviceLastUsed === void 0 ? void 0 : iotDeviceLastUsed.id) !== null ? iotDeviceLastUsed.id : void 0,
			number: (iotDevice === null || iotDevice === void 0 ? void 0 : iotDevice.number) ? String(iotDevice.number) : ""
		};
	} catch (e) {
		return { number: "" };
	}
};
const SaasAppDevicePlanning = ({ className, style, kind = "smart", showBack = false, onLinked, onBack, variant }) => {
	var _context$appHelper, _context$appHelper2, _modelRequest$data, _modelDetail$type;
	const [typeCode, setTypeCode] = (0, react.useState)(() => localStorage.getItem(TYPE_CODE_STORAGE_KEY) || "");
	const [modelCode, setModelCode] = (0, react.useState)(() => localStorage.getItem(MODEL_CODE_STORAGE_KEY) || "");
	const context = require_useEngineContext.default();
	const locale = require_useLocale.default();
	const currentVariant = variant || "desktop";
	const isPhoneVariant = currentVariant === "phone";
	_pisell_utils.locales.init(require_locales.default, locale || "en");
	const getText = _pisell_utils.locales.getText;
	const translation = require_hooks.useTranslation();
	const utils = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const iotEndpoint = (context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.constants) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.iotEndpoint) || "";
	const isModelSelecting = !typeCode || !modelCode;
	const isUniversalApp = localStorage.getItem(APP_TYPE_STORAGE_KEY) === "universal";
	const [workAreas, setWorkAreas] = (0, react.useState)([]);
	const [selectedWorkAreaId, setSelectedWorkAreaId] = (0, react.useState)();
	const [search, setSearch] = (0, react.useState)("");
	const [page, setPage] = (0, react.useState)(1);
	const [pageSize, setPageSize] = (0, react.useState)(10);
	const [addDeviceOpen, setAddDeviceOpen] = (0, react.useState)(false);
	const [takeoverDevice, setTakeoverDevice] = (0, react.useState)();
	const [selectedDevice, setSelectedDevice] = (0, react.useState)();
	const [editDeviceOpen, setEditDeviceOpen] = (0, react.useState)(false);
	const [deleteDeviceOpen, setDeleteDeviceOpen] = (0, react.useState)(false);
	const [workAreaModalOpen, setWorkAreaModalOpen] = (0, react.useState)(false);
	const [workAreaDrawerOpen, setWorkAreaDrawerOpen] = (0, react.useState)(false);
	const [workAreaModalMode, setWorkAreaModalMode] = (0, react.useState)("create");
	const [editingWorkArea, setEditingWorkArea] = (0, react.useState)();
	const [deleteModalOpen, setDeleteModalOpen] = (0, react.useState)(false);
	const [deleteRecord, setDeleteRecord] = (0, react.useState)();
	const [deleteCounts, setDeleteCounts] = (0, react.useState)({
		smart: 0,
		dumb: 0,
		integrated: 0
	});
	const [linkingDeviceId, setLinkingDeviceId] = (0, react.useState)();
	const [storedIotDevice, setStoredIotDevice] = (0, react.useState)(() => {
		var _utils$getApp;
		return getStoredIotDevice(utils === null || utils === void 0 || (_utils$getApp = utils.getApp) === null || _utils$getApp === void 0 ? void 0 : _utils$getApp.call(utils));
	});
	(0, react.useEffect)(() => {
		if (utils === null || utils === void 0 ? void 0 : utils.request) require_index$1.request.setRequest(utils.request);
	}, [utils === null || utils === void 0 ? void 0 : utils.request]);
	const workAreaRequest = (0, ahooks.useRequest)(require_serve.getWorkAreas, { onSuccess: (list) => {
		setWorkAreas(require_constants.sortWorkAreaList(list));
	} });
	const deviceTypeRequest = (0, ahooks.useRequest)(() => require_serve.getDeviceTypes({
		kind,
		with: ["activeModels", "activeModels.activeProfiles"]
	}), { manual: true });
	const modelRequest = (0, ahooks.useRequest)(require_serve.getDeviceModel, { manual: true });
	const saveRequest = (0, ahooks.useRequest)((payload) => workAreaModalMode === "create" ? require_serve.createWorkArea(payload) : require_serve.updateWorkArea((editingWorkArea === null || editingWorkArea === void 0 ? void 0 : editingWorkArea.id) || "", payload), { manual: true });
	const deviceRequest = (0, ahooks.useRequest)(require_serve.getDevices, { manual: true });
	const createDeviceRequest = (0, ahooks.useRequest)(require_serve.createDevice, { manual: true });
	const updateDeviceRequest = (0, ahooks.useRequest)((params) => require_serve.updateDevice(params.id, params.payload), { manual: true });
	const removeDeviceRequest = (0, ahooks.useRequest)(require_serve.deleteDevice, { manual: true });
	const getWorkAreaDetailRequest = (0, ahooks.useRequest)(require_serve.getWorkAreaDetail, { manual: true });
	const deleteRequest = (0, ahooks.useRequest)(require_serve.deleteWorkArea, { manual: true });
	(0, react.useEffect)(() => {
		if (!isModelSelecting) return;
		deviceTypeRequest.run();
	}, [isModelSelecting, kind]);
	(0, react.useEffect)(() => {
		if (!modelCode) return;
		modelRequest.run(modelCode);
	}, [modelCode]);
	(0, react.useEffect)(() => {
		if (!selectedWorkAreaId) return;
		if (!workAreas.some((item) => item.id === selectedWorkAreaId)) setSelectedWorkAreaId(void 0);
	}, [selectedWorkAreaId, workAreas]);
	const selectedWorkArea = (0, react.useMemo)(() => workAreas.find((item) => item.id === selectedWorkAreaId), [selectedWorkAreaId, workAreas]);
	const selectedWorkAreaName = selectedWorkArea ? translation(selectedWorkArea.name) || getText("pisell2.saas.device-planning.all-work-areas") : getText("pisell2.saas.device-planning.all-work-areas");
	const devices = (deviceRequest.data || EMPTY_DEVICES).filter((device) => device.model_code === modelCode);
	const modelDetail = ((_modelRequest$data = modelRequest.data) === null || _modelRequest$data === void 0 ? void 0 : _modelRequest$data.code) === modelCode ? modelRequest.data : void 0;
	const modelName = translation(modelDetail === null || modelDetail === void 0 ? void 0 : modelDetail.name) || modelCode || "-";
	const modelDescription = translation(modelDetail === null || modelDetail === void 0 ? void 0 : modelDetail.description);
	const deviceTypeName = translation(modelDetail === null || modelDetail === void 0 || (_modelDetail$type = modelDetail.type) === null || _modelDetail$type === void 0 ? void 0 : _modelDetail$type.name) || (modelDetail === null || modelDetail === void 0 ? void 0 : modelDetail.type_code) || typeCode || "-";
	const profileNameMap = (0, react.useMemo)(() => {
		const map = /* @__PURE__ */ new Map();
		((modelDetail === null || modelDetail === void 0 ? void 0 : modelDetail.active_profiles) || []).forEach((profile) => {
			map.set(profile.id, translation(profile.name) || String(profile.id));
		});
		devices.forEach((device) => {
			var _device$profile;
			if ((_device$profile = device.profile) === null || _device$profile === void 0 ? void 0 : _device$profile.id) map.set(device.profile.id, translation(device.profile.name) || String(device.profile.id));
		});
		return map;
	}, [
		devices,
		modelDetail,
		translation
	]);
	const tableData = (0, react.useMemo)(() => {
		const keyword = require_constants.normalizeText(search);
		const hasCurrentDevice = devices.some((item) => storedIotDevice.number && item.number && storedIotDevice.number === String(item.number));
		return devices.map((item) => {
			var _item$type, _item$model, _item$profile;
			const name = translation(item.name);
			const typeName = translation((_item$type = item.type) === null || _item$type === void 0 ? void 0 : _item$type.name) || deviceTypeName;
			const modelDisplayName = translation((_item$model = item.model) === null || _item$model === void 0 ? void 0 : _item$model.name) || modelName;
			const linkStatus = item.link || "unlinked";
			const isCurrent = Boolean(storedIotDevice.number && item.number && storedIotDevice.number === String(item.number));
			const profileName = translation((_item$profile = item.profile) === null || _item$profile === void 0 ? void 0 : _item$profile.name) || (item.profile_id ? profileNameMap.get(item.profile_id) : "") || (item.profile_id ? String(item.profile_id) : "-");
			return {
				id: item.id,
				name,
				typeName,
				modelName: modelDisplayName,
				profileName,
				link: linkStatus,
				status: linkStatus === "linked" ? item.status || "offline" : "unknown",
				typeCode: item.type_code,
				modelCode: item.model_code,
				isCurrent,
				isLastUsed: Boolean(!hasCurrentDevice && storedIotDevice.id !== void 0 && String(storedIotDevice.id) === String(item.id)),
				_raw: item
			};
		}).filter((item) => keyword ? [
			item.name,
			item.typeName,
			item.modelName,
			item.profileName,
			item._raw.workarea ? translation(item._raw.workarea.name) : ""
		].some((value) => require_constants.normalizeText(value || "").includes(keyword)) : true);
	}, [
		deviceTypeName,
		devices,
		modelName,
		profileNameMap,
		search,
		storedIotDevice,
		translation
	]);
	const deviceTypeGroups = (0, react.useMemo)(() => (deviceTypeRequest.data || []).map((type) => ({
		type,
		models: [...type.active_models || []].filter((model) => {
			var _model$data;
			return iotEndpoint ? ((_model$data = model.data) === null || _model$data === void 0 ? void 0 : _model$data.endpoint) === iotEndpoint : true;
		}).sort((a, b) => (a.sort || 0) - (b.sort || 0))
	})).filter((group) => group.models.length > 0), [deviceTypeRequest.data, iotEndpoint]);
	const showInitialLoading = !isModelSelecting && (workAreaRequest.loading && workAreas.length === 0 || modelRequest.loading && !modelDetail);
	const handleBack = () => {
		var _context$appHelper3, _getApp, _history$goBack;
		if (onBack) {
			onBack();
			return;
		}
		const getApp = context === null || context === void 0 || (_context$appHelper3 = context.appHelper) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.utils) === null || _context$appHelper3 === void 0 ? void 0 : _context$appHelper3.getApp;
		const history = getApp === null || getApp === void 0 || (_getApp = getApp()) === null || _getApp === void 0 || (_getApp = _getApp.history) === null || _getApp === void 0 ? void 0 : _getApp.instance;
		history === null || history === void 0 || (_history$goBack = history.goBack) === null || _history$goBack === void 0 || _history$goBack.call(history);
	};
	const reloadWorkAreas = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (selectedId) {
			const sorted = require_constants.sortWorkAreaList(yield workAreaRequest.runAsync());
			setWorkAreas(sorted);
			if (selectedId) setSelectedWorkAreaId(selectedId);
			return sorted;
		});
		return function reloadWorkAreas(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const resetDeviceView = () => {
		setSearch("");
		setPage(1);
	};
	const handleSelectWorkArea = (id) => {
		setSelectedWorkAreaId(id);
		resetDeviceView();
		setWorkAreaDrawerOpen(false);
	};
	const persistDeviceModel = (nextTypeCode, nextModelCode) => {
		if (!nextTypeCode || !nextModelCode) return;
		localStorage.setItem(TYPE_CODE_STORAGE_KEY, nextTypeCode);
		localStorage.setItem(MODEL_CODE_STORAGE_KEY, nextModelCode);
	};
	const handleSelectDeviceModel = (type, model) => {
		const nextTypeCode = model.type_code || type.code;
		const nextModelCode = model.code;
		if (!nextTypeCode || !nextModelCode) return;
		if (!showBack) persistDeviceModel(nextTypeCode, nextModelCode);
		setTypeCode(nextTypeCode);
		setModelCode(nextModelCode);
		resetDeviceView();
	};
	const handleSwitchDeviceModel = () => {
		setTypeCode("");
		setModelCode("");
		setSearch("");
		setPage(1);
		setAddDeviceOpen(false);
		setEditDeviceOpen(false);
		setDeleteDeviceOpen(false);
		setSelectedDevice(void 0);
		setTakeoverDevice(void 0);
	};
	const getDeviceListParams = (0, react.useCallback)(() => ({
		workarea_id: selectedWorkAreaId,
		type_kind: kind,
		type_code: typeCode,
		model_code: modelCode,
		with: [
			"type",
			"model",
			"profile",
			"workarea"
		]
	}), [
		kind,
		modelCode,
		selectedWorkAreaId,
		typeCode
	]);
	const reloadDevices = (0, react.useCallback)(() => {
		if (!modelCode) return;
		deviceRequest.run(getDeviceListParams());
	}, [getDeviceListParams, modelCode]);
	(0, react.useEffect)(() => {
		reloadDevices();
	}, [reloadDevices]);
	(0, react.useEffect)(() => {
		if (!modelCode) return;
		const timer = window.setInterval(() => {
			reloadDevices();
		}, 60 * 1e3);
		return () => {
			window.clearInterval(timer);
		};
	}, [modelCode, reloadDevices]);
	const openCreateModal = () => {
		setWorkAreaModalMode("create");
		setEditingWorkArea(void 0);
		setWorkAreaModalOpen(true);
	};
	const openEditModal = (record) => {
		setWorkAreaModalMode("edit");
		setEditingWorkArea(record);
		setWorkAreaModalOpen(true);
	};
	const handleSaveWorkArea = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (payload) {
			try {
				const saved = yield saveRequest.runAsync(payload);
				(0, _pisell_materials.PisellToast)({
					content: getText(workAreaModalMode === "create" ? "pisell2.saas.device-planning.message.create-success" : "pisell2.saas.device-planning.message.update-success"),
					duration: 2
				});
				setWorkAreaModalOpen(false);
				setEditingWorkArea(void 0);
				yield reloadWorkAreas((saved === null || saved === void 0 ? void 0 : saved.id) || (editingWorkArea === null || editingWorkArea === void 0 ? void 0 : editingWorkArea.id));
			} catch (e) {}
		});
		return function handleSaveWorkArea(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const openDeleteModal = (record) => {
		setDeleteRecord(record);
		setDeleteCounts({
			smart: 0,
			dumb: 0,
			integrated: 0
		});
		setDeleteModalOpen(true);
		getWorkAreaDetailRequest.runAsync({
			id: record.id,
			relation_count: [
				"smartDevices",
				"dumbDevices",
				"integratedDevices"
			]
		}).then((res) => {
			var _res$data;
			const _workArea = res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data[0];
			setDeleteCounts({
				smart: (_workArea === null || _workArea === void 0 ? void 0 : _workArea.smart_devices_count) || 0,
				dumb: (_workArea === null || _workArea === void 0 ? void 0 : _workArea.dumb_devices_count) || 0,
				integrated: (_workArea === null || _workArea === void 0 ? void 0 : _workArea.integrated_devices_count) || 0
			});
		}).catch(() => setDeleteCounts({
			smart: 0,
			dumb: 0,
			integrated: 0
		}));
	};
	const handleConfirmDelete = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
			if (!deleteRecord) return;
			try {
				yield deleteRequest.runAsync(deleteRecord.id);
				(0, _pisell_materials.PisellToast)({
					content: getText("pisell2.saas.device-planning.message.delete-success"),
					duration: 2
				});
				setDeleteModalOpen(false);
				setDeleteRecord(void 0);
				yield reloadWorkAreas();
				reloadDevices();
			} catch (e) {}
		});
		return function handleConfirmDelete() {
			return _ref3.apply(this, arguments);
		};
	}();
	const handleCreateDevice = function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (payload) {
			try {
				yield createDeviceRequest.runAsync(payload);
				(0, _pisell_materials.PisellToast)({
					content: getText("pisell2.saas.device-planning.add.create-success"),
					duration: 2
				});
				setAddDeviceOpen(false);
				setPage(1);
				reloadDevices();
			} catch (e) {}
		});
		return function handleCreateDevice(_x3) {
			return _ref4.apply(this, arguments);
		};
	}();
	const handleUpdateDevice = function() {
		var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (id, payload) {
			try {
				yield updateDeviceRequest.runAsync({
					id,
					payload
				});
				(0, _pisell_materials.PisellToast)({
					content: getText("pisell2.saas.device-planning.edit.success"),
					duration: 2
				});
				setEditDeviceOpen(false);
				setSelectedDevice(void 0);
				reloadDevices();
			} catch (e) {}
		});
		return function handleUpdateDevice(_x4, _x5) {
			return _ref5.apply(this, arguments);
		};
	}();
	const handleDeleteDevice = function() {
		var _ref6 = require_asyncToGenerator._asyncToGenerator(function* () {
			if (!selectedDevice) return;
			try {
				yield removeDeviceRequest.runAsync(selectedDevice.id);
				(0, _pisell_materials.PisellToast)({
					content: getText("pisell2.saas.device-planning.delete-device.success"),
					duration: 2
				});
				setDeleteDeviceOpen(false);
				setSelectedDevice(void 0);
				reloadDevices();
			} catch (e) {}
		});
		return function handleDeleteDevice() {
			return _ref6.apply(this, arguments);
		};
	}();
	const handleLinkDevice = function() {
		var _ref7 = require_asyncToGenerator._asyncToGenerator(function* (record) {
			var _utils$getApp2;
			const app = utils === null || utils === void 0 || (_utils$getApp2 = utils.getApp) === null || _utils$getApp2 === void 0 ? void 0 : _utils$getApp2.call(utils);
			setLinkingDeviceId(record.id);
			try {
				var _app$locales, _app$plugins, _app$plugins$getDevic, _app$storage3, _app$storage4, _localStorage, _localStorage$setItem;
				const language = (app === null || app === void 0 || (_app$locales = app.locales) === null || _app$locales === void 0 ? void 0 : _app$locales.getLocale()) || "en";
				const deviceDetail = yield app === null || app === void 0 || (_app$plugins = app.plugins) === null || _app$plugins === void 0 || (_app$plugins = _app$plugins.get("device")) === null || _app$plugins === void 0 || (_app$plugins$getDevic = _app$plugins.getDeviceDetail) === null || _app$plugins$getDevic === void 0 ? void 0 : _app$plugins$getDevic.call(_app$plugins, { language });
				const linkedDevice = yield require_serve.linkDevice(record.id, (deviceDetail === null || deviceDetail === void 0 ? void 0 : deviceDetail.data) || {});
				app === null || app === void 0 || (_app$storage3 = app.storage) === null || _app$storage3 === void 0 || _app$storage3.setStorage("iotDevice", JSON.stringify(linkedDevice || {}));
				app === null || app === void 0 || (_app$storage4 = app.storage) === null || _app$storage4 === void 0 || _app$storage4.setStorage("iotDeviceLastUsed", JSON.stringify(linkedDevice || {}));
				setStoredIotDevice({
					id: typeof (linkedDevice === null || linkedDevice === void 0 ? void 0 : linkedDevice.id) === "number" ? linkedDevice.id : void 0,
					number: (linkedDevice === null || linkedDevice === void 0 ? void 0 : linkedDevice.number) ? String(linkedDevice.number) : ""
				});
				persistDeviceModel(record.typeCode, record.modelCode);
				const setting = yield require_serve.getDeviceSetting(record.id, ["general.channel"]);
				const channel = (setting === null || setting === void 0 ? void 0 : setting["general.channel"]) ? String(setting["general.channel"]) : "system";
				(_localStorage = localStorage) === null || _localStorage === void 0 || (_localStorage$setItem = _localStorage.setItem) === null || _localStorage$setItem === void 0 || _localStorage$setItem.call(_localStorage, "pisell_app_channel", channel);
				setTakeoverDevice(void 0);
				reloadDevices();
				yield onLinked === null || onLinked === void 0 ? void 0 : onLinked({
					device: linkedDevice,
					channel
				});
			} catch (e) {} finally {
				setLinkingDeviceId(void 0);
			}
		});
		return function handleLinkDevice(_x6) {
			return _ref7.apply(this, arguments);
		};
	}();
	const renderModelSelection = () => /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__model-step" }, deviceTypeRequest.loading && !deviceTypeGroups.length ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__empty-workarea" }, /* @__PURE__ */ react.default.createElement(antd.Spin, null)) : deviceTypeGroups.length ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__model-group-list" }, deviceTypeGroups.map(({ type, models }) => /* @__PURE__ */ react.default.createElement("section", {
		key: type.code,
		className: "saas-app-device-planning__model-group"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__model-group-title" }, type.icon ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: type.icon }) : null, /* @__PURE__ */ react.default.createElement("span", null, translation(type.name) || type.code)), /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__model-grid" }, models.map((model) => {
		const name = translation(model.name) || model.code;
		const description = translation(model.description);
		return /* @__PURE__ */ react.default.createElement("div", {
			key: `${type.code}-${model.code}`,
			className: "saas-app-device-planning__model-card",
			onClick: () => handleSelectDeviceModel(type, model),
			style: {
				cursor: "pointer",
				padding: "16px"
			}
		}, /* @__PURE__ */ react.default.createElement("span", { className: "saas-app-device-planning__model-card-title" }, name), description ? /* @__PURE__ */ react.default.createElement("span", { className: "saas-app-device-planning__model-card-desc" }, description) : null);
	}))))) : /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__empty-workarea" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Empty, {
		image: _pisell_materials.Empty.PRESENTED_IMAGE_SIMPLE,
		description: getText("pisell2.saas.device-planning.empty.models")
	})));
	const renderTakeoverModal = () => /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		title: getText("pisell2.saas.device-planning.takeover.title"),
		headerDivider: false,
		footerDivider: false,
		open: Boolean(takeoverDevice),
		footer: /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning-takeover-modal__footer" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "default",
			block: true,
			size: "large",
			disabled: Boolean(linkingDeviceId),
			onClick: () => setTakeoverDevice(void 0)
		}, getText("pisell2.saas.device-planning.takeover.cancel")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "primary",
			block: true,
			size: "large",
			icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: "ThunderboltOutlined" }),
			loading: linkingDeviceId === (takeoverDevice === null || takeoverDevice === void 0 ? void 0 : takeoverDevice.id),
			disabled: Boolean(linkingDeviceId),
			onClick: () => takeoverDevice && handleLinkDevice(takeoverDevice)
		}, getText("pisell2.saas.device-planning.takeover.confirm"))),
		width: 640,
		destroyOnClose: true,
		maskClosable: false,
		platform: isPhoneVariant ? "h5" : void 0,
		mobileModalHeight: isPhoneVariant ? "auto" : void 0,
		className: (0, classnames.default)("saas-app-device-planning-takeover-modal", `saas-app-device-planning-takeover-modal--${currentVariant}`),
		onCancel: () => setTakeoverDevice(void 0)
	}, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning-takeover-modal__warning" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: "ExclamationCircleOutlined" }), /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning-takeover-modal__message" }, getText("pisell2.saas.device-planning.takeover.message")), /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning-takeover-modal__detail" }, /* @__PURE__ */ react.default.createElement("strong", null, getText("pisell2.saas.device-planning.takeover.device")), (takeoverDevice === null || takeoverDevice === void 0 ? void 0 : takeoverDevice.name) || "-"), /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning-takeover-modal__detail" }, /* @__PURE__ */ react.default.createElement("strong", null, getText("pisell2.saas.device-planning.takeover.status")), getText(`pisell2.saas.device-planning.status.${takeoverDevice === null || takeoverDevice === void 0 ? void 0 : takeoverDevice.link}`) || (takeoverDevice === null || takeoverDevice === void 0 ? void 0 : takeoverDevice.link) || "-", " • ", getText(`pisell2.saas.device-planning.status.${takeoverDevice === null || takeoverDevice === void 0 ? void 0 : takeoverDevice.status}`) || (takeoverDevice === null || takeoverDevice === void 0 ? void 0 : takeoverDevice.status) || "-"), /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning-takeover-modal__detail" }, /* @__PURE__ */ react.default.createElement("strong", null, getText("pisell2.saas.device-planning.takeover.hardware")), (takeoverDevice === null || takeoverDevice === void 0 ? void 0 : takeoverDevice.modelName) || "-"), /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning-takeover-modal__desc" }, getText("pisell2.saas.device-planning.takeover.desc")))));
	const renderWorkAreaDrawer = () => isPhoneVariant ? /* @__PURE__ */ react.default.createElement(require_index.default, {
		renderType: "drawer",
		showHandlerBar: isPhoneVariant,
		props: {
			open: workAreaDrawerOpen,
			placement: "bottom",
			height: "80dvh",
			className: (0, classnames.default)("saas-app-device-planning-workarea-drawer", `saas-app-device-planning-workarea-drawer--${currentVariant}`),
			destroyOnClose: true,
			onClose: () => setWorkAreaDrawerOpen(false),
			closable: false,
			bodyStyle: { padding: 0 }
		}
	}, /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		className: "saas-app-device-planning-sider--drawer",
		loading: workAreaRequest.loading,
		workAreas,
		selectedWorkAreaId,
		getText,
		translation,
		onCreate: () => {
			setWorkAreaDrawerOpen(false);
			openCreateModal();
		},
		onSelect: handleSelectWorkArea,
		onEdit: (record) => {
			setWorkAreaDrawerOpen(false);
			openEditModal(record);
		},
		onDelete: (record) => {
			setWorkAreaDrawerOpen(false);
			openDeleteModal(record);
		}
	})) : null;
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("saas-app-device-planning", `saas-app-device-planning--${currentVariant}`, className),
		style
	}, /* @__PURE__ */ react.default.createElement("header", { className: (0, classnames.default)("saas-app-device-planning__global-header", { "saas-app-device-planning__global-header--with-back": showBack }) }, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__global-header-left" }, showBack ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "text",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.ArrowLeftOutlined, null),
		onClick: handleBack
	}) : null, /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__global-title" }, getText(isModelSelecting ? "pisell2.saas.device-planning.select-model-title" : "pisell2.saas.device-planning.page-title")), isModelSelecting ? null : /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__global-subtitle" }, getText("pisell2.saas.device-planning.page-subtitle")))), isModelSelecting ? null : /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__global-actions" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Tag, { color: "green" }, deviceTypeName), isUniversalApp ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "default",
		className: "saas-app-device-planning__switch-model-button",
		icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: "SwapOutlined" }),
		onClick: handleSwitchDeviceModel
	}, getText("pisell2.saas.device-planning.switch-model")) : null)), /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__body" }, isModelSelecting || isPhoneVariant ? null : /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		loading: workAreaRequest.loading,
		workAreas,
		selectedWorkAreaId,
		getText,
		translation,
		onCreate: openCreateModal,
		onSelect: handleSelectWorkArea,
		onEdit: openEditModal,
		onDelete: openDeleteModal
	}), /* @__PURE__ */ react.default.createElement("main", { className: (0, classnames.default)("saas-app-device-planning__content", { "saas-app-device-planning__content--model-selecting": isModelSelecting }) }, isModelSelecting ? renderModelSelection() : showInitialLoading ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__empty-workarea" }, /* @__PURE__ */ react.default.createElement(antd.Spin, null)) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__page-header" }, /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__title" }, modelName), modelDescription ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__subtitle" }, modelDescription) : null), /* @__PURE__ */ react.default.createElement("div", { className: "saas-app-device-planning__page-actions" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "default",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.ReloadOutlined, null),
		loading: deviceRequest.loading,
		onClick: reloadDevices
	}, getText("pisell2.saas.device-planning.refresh")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.PlusOutlined, null),
		onClick: () => setAddDeviceOpen(true)
	}, getText("pisell2.saas.device-planning.add-device")))), isPhoneVariant ? /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: "saas-app-device-planning__workarea-trigger",
		onClick: () => setWorkAreaDrawerOpen(true)
	}, /* @__PURE__ */ react.default.createElement("span", { className: "saas-app-device-planning__workarea-trigger-label" }, getText("pisell2.saas.device-planning.work-areas")), /* @__PURE__ */ react.default.createElement("span", { className: "saas-app-device-planning__workarea-trigger-value" }, selectedWorkAreaName), /* @__PURE__ */ react.default.createElement(_ant_design_icons.DownOutlined, null)) : null, /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		data: tableData,
		loading: deviceRequest.loading,
		search,
		page,
		pageSize,
		linkingDeviceId,
		getText,
		translation,
		onSearchChange: (value) => {
			setSearch(value);
			setPage(1);
		},
		onPageChange: (nextPage, nextPageSize) => {
			setPage(nextPage);
			setPageSize(nextPageSize);
		},
		onAdd: () => setAddDeviceOpen(true),
		onLink: handleLinkDevice,
		onTakeover: setTakeoverDevice,
		onEdit: (record) => {
			setSelectedDevice(record);
			setEditDeviceOpen(true);
		},
		onDelete: (record) => {
			setSelectedDevice(record);
			setDeleteDeviceOpen(true);
		}
	})))), /* @__PURE__ */ react.default.createElement(require_index$4.default, {
		open: addDeviceOpen,
		variant: currentVariant,
		model: modelDetail || void 0,
		fallbackModelCode: modelCode,
		workAreaId: selectedWorkAreaId,
		workAreas,
		submitting: createDeviceRequest.loading,
		getText,
		translation,
		onClose: () => setAddDeviceOpen(false),
		onSubmit: handleCreateDevice
	}), /* @__PURE__ */ react.default.createElement(require_index$5.default, {
		open: editDeviceOpen,
		variant: currentVariant,
		record: selectedDevice,
		workAreas,
		model: modelDetail || void 0,
		submitting: updateDeviceRequest.loading,
		getText,
		translation,
		onCancel: () => setEditDeviceOpen(false),
		onSubmit: handleUpdateDevice
	}), /* @__PURE__ */ react.default.createElement(require_index$6.default, {
		open: deleteDeviceOpen,
		variant: currentVariant,
		record: selectedDevice,
		submitting: removeDeviceRequest.loading,
		getText,
		onCancel: () => setDeleteDeviceOpen(false),
		onConfirm: handleDeleteDevice
	}), /* @__PURE__ */ react.default.createElement(require_index$7.default, {
		open: workAreaModalOpen,
		variant: currentVariant,
		mode: workAreaModalMode,
		record: editingWorkArea,
		submitting: saveRequest.loading,
		getText,
		onCancel: () => {
			setWorkAreaModalOpen(false);
			setEditingWorkArea(void 0);
		},
		onSubmit: handleSaveWorkArea
	}), /* @__PURE__ */ react.default.createElement(require_index$8.default, {
		open: deleteModalOpen,
		variant: currentVariant,
		loading: getWorkAreaDetailRequest.loading,
		submitting: deleteRequest.loading,
		record: deleteRecord,
		counts: deleteCounts,
		translation,
		getText,
		onClose: () => {
			setDeleteModalOpen(false);
			setDeleteRecord(void 0);
			setDeleteCounts({
				smart: 0,
				dumb: 0,
				integrated: 0
			});
		},
		onConfirm: handleConfirmDelete
	}), renderTakeoverModal(), renderWorkAreaDrawer());
};
SaasAppDevicePlanning.displayName = "SaasAppDevicePlanning";
var appDevicePlanning_default = require_withResponsive.default(SaasAppDevicePlanning);
//#endregion
exports.default = appDevicePlanning_default;
