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_useLocale = require("../../../hooks/useLocale.js");
const require_index = require("../../../utils/index.js");
const require_index$1 = require("./hooks/index.js");
const require_useDevicePlanningDialogs = require("./hooks/useDevicePlanningDialogs.js");
const require_index$2 = require("./locales/index.js");
const require_types = require("./model/types.js");
const require_constants = require("./model/constants.js");
const require_index$3 = require("./modules/deviceDetail/components/DeviceDetailDrawer/index.js");
const require_index$4 = require("./modules/devices/components/DeviceTable/index.js");
const require_index$5 = require("./modules/devices/components/AddDeviceModal/index.js");
const require_index$6 = require("./modules/devices/components/EditDeviceModal/index.js");
const require_index$7 = require("./modules/devices/components/DeleteDeviceModal/index.js");
const require_useDeviceList = require("./modules/devices/hooks/useDeviceList.js");
const require_useDeviceModels = require("./modules/devices/hooks/useDeviceModels.js");
const require_index$8 = require("./modules/workAreas/components/WorkAreaList/index.js");
const require_index$9 = require("./modules/workAreas/components/WorkAreaModal/index.js");
const require_index$10 = require("./modules/workAreas/components/DeleteWorkAreaModal/index.js");
const require_useWorkAreas = require("./modules/workAreas/hooks/useWorkAreas.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);
require("./index.less");
//#region src/plus/saasDevice/devicePlanning/index.tsx
const SaasDevicePlanning = ({ className, style }) => {
	var _context$appHelper, _workArea$selectedWor;
	const context = require_useEngineContext.default();
	const locale = require_useLocale.default();
	_pisell_utils.locales.init(require_index$2.default, locale || "en");
	const getText = _pisell_utils.locales.getText;
	const translation = require_index$1.useTranslation();
	const utils = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const [activeKind, setActiveKind] = (0, react.useState)("smart");
	const dialogs = require_useDevicePlanningDialogs.useDevicePlanningDialogs();
	(0, react.useEffect)(() => {
		if (utils === null || utils === void 0 ? void 0 : utils.request) require_index.request.setRequest(utils.request);
	}, [utils === null || utils === void 0 ? void 0 : utils.request]);
	const workArea = require_useWorkAreas.useWorkAreas({
		getText,
		workAreaModalMode: dialogs.workAreaModalMode,
		editingWorkArea: dialogs.editingWorkArea,
		closeWorkAreaModal: dialogs.closeWorkAreaModal
	});
	const modelState = require_useDeviceModels.useDeviceModels({
		activeKind,
		selectedWorkAreaId: workArea.selectedWorkAreaId,
		devices: [],
		translation
	});
	const deviceList = require_useDeviceList.useDeviceList({
		activeKind,
		selectedWorkAreaId: workArea.selectedWorkAreaId,
		translation,
		profileNameMap: modelState.profileNameMap
	});
	const openAddDeviceModal = () => {
		modelState.loadBrandsIfNeeded();
		dialogs.setAddDeviceOpen(true);
	};
	const handleCreateDevice = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (payload) {
			try {
				const created = yield deviceList.createDeviceRequest.runAsync(payload);
				(0, _pisell_materials.PisellToast)({
					content: getText("pisell2.saas.device-planning.add.create-success"),
					duration: 2
				});
				dialogs.setAddDeviceOpen(false);
				deviceList.setPage(1);
				const refreshedDevices = yield deviceList.reloadDevicesAsync();
				if (created === null || created === void 0 ? void 0 : created.id) {
					const createdDevice = refreshedDevices.find((item) => item.id === created.id) || created;
					dialogs.setSelectedDevice(deviceList.toDeviceRow(createdDevice));
					dialogs.setDetailInitialTab(createdDevice.type_kind === "integrated" ? "info" : "connections");
					dialogs.setDetailOpen(true);
				}
			} catch (e) {}
		});
		return function handleCreateDevice(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const handleUpdateDevice = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (id, payload) {
			try {
				yield deviceList.updateDeviceRequest.runAsync({
					id,
					payload
				});
				(0, _pisell_materials.PisellToast)({
					content: getText("pisell2.saas.device-planning.edit.success"),
					duration: 2
				});
				dialogs.setEditDeviceOpen(false);
				dialogs.setDetailOpen(false);
				dialogs.clearSelectedDevice();
				deviceList.reloadDevices();
			} catch (e) {}
		});
		return function handleUpdateDevice(_x2, _x3) {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleDeleteDevice = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
			if (!dialogs.selectedDevice) return;
			try {
				yield deviceList.removeDeviceRequest.runAsync(dialogs.selectedDevice.id);
				(0, _pisell_materials.PisellToast)({
					content: getText("pisell2.saas.device-planning.delete-device.success"),
					duration: 2
				});
				dialogs.setDeleteDeviceOpen(false);
				dialogs.setDetailOpen(false);
				dialogs.clearSelectedDevice();
				deviceList.reloadDevices();
			} catch (e) {}
		});
		return function handleDeleteDevice() {
			return _ref3.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("saas-device-planning", className),
		style
	}, /* @__PURE__ */ react.default.createElement(require_index$8.default, {
		loading: workArea.workAreaRequest.loading,
		workAreas: workArea.workAreas,
		selectedWorkAreaId: workArea.selectedWorkAreaId,
		getText,
		translation,
		onCreate: dialogs.openCreateWorkArea,
		onSelect: (id) => {
			workArea.setSelectedWorkAreaId(id);
			deviceList.resetDeviceView();
		},
		onEdit: dialogs.openEditWorkArea,
		onDelete: workArea.openDeleteModal,
		onSortEnd: workArea.handleDragEnd
	}), /* @__PURE__ */ react.default.createElement("main", { className: "saas-device-planning__content" }, workArea.showWorkAreaLoading ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-device-planning__empty-workarea" }, /* @__PURE__ */ react.default.createElement(antd.Spin, null)) : workArea.showEmptyWorkArea ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-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.workarea-first")
	})) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-device-planning__page-header" }, /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "saas-device-planning__title" }, workArea.selectedWorkArea ? translation(workArea.selectedWorkArea.name) : "-"), ((_workArea$selectedWor = workArea.selectedWorkArea) === null || _workArea$selectedWor === void 0 ? void 0 : _workArea$selectedWor.description) ? /* @__PURE__ */ react.default.createElement("div", { className: "saas-device-planning__subtitle" }, translation(workArea.selectedWorkArea.description)) : null)), /* @__PURE__ */ react.default.createElement(antd.Tabs, {
		activeKey: activeKind,
		onChange: (key) => {
			setActiveKind(key);
			deviceList.resetDeviceView();
		},
		items: require_types.DEVICE_KIND_OPTIONS.map((kind) => ({
			key: kind,
			label: getText(require_constants.DEVICE_KIND_LABEL_KEY[kind])
		}))
	}), /* @__PURE__ */ react.default.createElement(require_index$4.default, {
		activeKind,
		data: deviceList.tableData,
		loading: deviceList.deviceRequest.loading,
		search: deviceList.search,
		typeFilter: deviceList.typeFilter,
		structureFilter: deviceList.structureFilter,
		typeOptions: modelState.typeOptions,
		structureOptions: deviceList.structureOptions,
		page: deviceList.page,
		pageSize: deviceList.pageSize,
		getText,
		onSearchChange: (value) => {
			deviceList.setSearch(value);
			deviceList.setPage(1);
		},
		onTypeFilterChange: (value) => {
			deviceList.setTypeFilter(value);
			deviceList.setPage(1);
		},
		onStructureFilterChange: (value) => {
			deviceList.setStructureFilter(value);
			deviceList.setPage(1);
		},
		onPageChange: (nextPage, nextPageSize) => {
			deviceList.setPage(nextPage);
			deviceList.setPageSize(nextPageSize);
		},
		onAdd: openAddDeviceModal,
		onSortChange: deviceList.handleDeviceSort,
		onRowClick: (record) => dialogs.openDeviceDetail(record, "info")
	}))), dialogs.detailOpen && dialogs.selectedDevice ? /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		key: dialogs.selectedDevice.id,
		open: dialogs.detailOpen,
		record: dialogs.selectedDevice,
		initialTab: dialogs.detailInitialTab,
		getText,
		translation,
		onClose: dialogs.closeDeviceDetail,
		onEdit: () => dialogs.setEditDeviceOpen(true),
		onDelete: () => dialogs.setDeleteDeviceOpen(true),
		onConnectionsChanged: deviceList.reloadDevices
	}) : null, dialogs.editDeviceOpen && dialogs.selectedDevice ? /* @__PURE__ */ react.default.createElement(require_index$6.default, {
		open: true,
		record: dialogs.selectedDevice,
		workAreas: workArea.workAreas,
		models: modelState.activeModels,
		submitting: deviceList.updateDeviceRequest.loading,
		getText,
		translation,
		onCancel: () => dialogs.setEditDeviceOpen(false),
		onSubmit: handleUpdateDevice
	}) : null, dialogs.deleteDeviceOpen && dialogs.selectedDevice ? /* @__PURE__ */ react.default.createElement(require_index$7.default, {
		open: true,
		record: dialogs.selectedDevice,
		submitting: deviceList.removeDeviceRequest.loading,
		getText,
		onCancel: () => dialogs.setDeleteDeviceOpen(false),
		onConfirm: handleDeleteDevice
	}) : null, dialogs.addDeviceOpen ? /* @__PURE__ */ react.default.createElement(require_index$5.default, {
		open: true,
		activeKind,
		models: modelState.activeModels,
		brands: modelState.brands,
		workAreaId: workArea.selectedWorkAreaId,
		submitting: deviceList.createDeviceRequest.loading,
		getText,
		translation,
		onClose: () => dialogs.setAddDeviceOpen(false),
		onSubmit: handleCreateDevice
	}) : null, dialogs.workAreaModalOpen ? /* @__PURE__ */ react.default.createElement(require_index$9.default, {
		open: dialogs.workAreaModalOpen,
		mode: dialogs.workAreaModalMode,
		record: dialogs.editingWorkArea,
		submitting: workArea.saveRequest.loading,
		getText,
		onCancel: dialogs.closeWorkAreaModal,
		onSubmit: workArea.handleSaveWorkArea
	}) : null, workArea.deleteModalOpen ? /* @__PURE__ */ react.default.createElement(require_index$10.default, {
		open: workArea.deleteModalOpen,
		loading: workArea.getWorkAreaDetailRequest.loading,
		submitting: workArea.deleteRequest.loading,
		record: workArea.deleteRecord,
		counts: workArea.deleteCounts,
		translation,
		getText,
		onClose: workArea.closeDeleteModal,
		onConfirm: workArea.handleConfirmDelete
	}) : null);
};
//#endregion
exports.default = SaasDevicePlanning;
