import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import { request } from "../../../utils/index.js";
import useLocale from "../../../hooks/useLocale.js";
import { useTranslation } from "./hooks/index.js";
import { DEVICE_KIND_LABEL_KEY } from "./constants.js";
import { useDeviceList } from "./hooks/useDeviceList.js";
import { useDeviceModels } from "./hooks/useDeviceModels.js";
import { useDevicePlanningDialogs } from "./hooks/useDevicePlanningDialogs.js";
import { useWorkAreas } from "./hooks/useWorkAreas.js";
import locales_default from "./locales.js";
import WorkAreaList from "./components/WorkAreaList/index.js";
import DeviceTable from "./components/DeviceTable/index.js";
import { DEVICE_KIND_OPTIONS } from "./types.js";
import AddDeviceModal from "./components/AddDeviceModal/index.js";
import DeviceDetailDrawer from "./components/DeviceDetailDrawer/index.js";
import EditDeviceModal from "./components/EditDeviceModal/index.js";
import DeleteDeviceModal from "./components/DeleteDeviceModal/index.js";
import WorkAreaModal from "./components/WorkAreaModal/index.js";
import DeleteWorkAreaModal from "./components/DeleteWorkAreaModal/index.js";
import React, { useEffect, useState } from "react";
import { Spin, Tabs } from "antd";
import { locales } from "@pisell/utils";
import { Empty as Empty$1, PisellToast } from "@pisell/materials";
import classNames from "classnames";
import "./index.less";
//#region src/plus/saasDevice/devicePlanning/index.tsx
const SaasDevicePlanning = ({ className, style }) => {
	var _context$appHelper, _workArea$selectedWor;
	const context = useEngineContext();
	const locale = useLocale();
	locales.init(locales_default, locale || "en");
	const getText = locales.getText;
	const translation = 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] = useState("smart");
	const dialogs = useDevicePlanningDialogs();
	useEffect(() => {
		if (utils === null || utils === void 0 ? void 0 : utils.request) request.setRequest(utils.request);
	}, [utils === null || utils === void 0 ? void 0 : utils.request]);
	const workArea = useWorkAreas({
		getText,
		workAreaModalMode: dialogs.workAreaModalMode,
		editingWorkArea: dialogs.editingWorkArea,
		closeWorkAreaModal: dialogs.closeWorkAreaModal
	});
	const modelState = useDeviceModels({
		activeKind,
		selectedWorkAreaId: workArea.selectedWorkAreaId,
		devices: [],
		translation
	});
	const deviceList = useDeviceList({
		activeKind,
		selectedWorkAreaId: workArea.selectedWorkAreaId,
		translation,
		profileNameMap: modelState.profileNameMap
	});
	const openAddDeviceModal = () => {
		modelState.loadBrandsIfNeeded();
		dialogs.setAddDeviceOpen(true);
	};
	const handleCreateDevice = function() {
		var _ref = _asyncToGenerator(function* (payload) {
			try {
				const created = yield deviceList.createDeviceRequest.runAsync(payload);
				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 = _asyncToGenerator(function* (id, payload) {
			try {
				yield deviceList.updateDeviceRequest.runAsync({
					id,
					payload
				});
				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 = _asyncToGenerator(function* () {
			if (!dialogs.selectedDevice) return;
			try {
				yield deviceList.removeDeviceRequest.runAsync(dialogs.selectedDevice.id);
				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.createElement("div", {
		className: classNames("saas-device-planning", className),
		style
	}, /* @__PURE__ */ React.createElement(WorkAreaList, {
		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.createElement("main", { className: "saas-device-planning__content" }, workArea.showWorkAreaLoading ? /* @__PURE__ */ React.createElement("div", { className: "saas-device-planning__empty-workarea" }, /* @__PURE__ */ React.createElement(Spin, null)) : workArea.showEmptyWorkArea ? /* @__PURE__ */ React.createElement("div", { className: "saas-device-planning__empty-workarea" }, /* @__PURE__ */ React.createElement(Empty$1, {
		image: Empty$1.PRESENTED_IMAGE_SIMPLE,
		description: getText("pisell2.saas.device-planning.empty.workarea-first")
	})) : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "saas-device-planning__page-header" }, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.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.createElement("div", { className: "saas-device-planning__subtitle" }, translation(workArea.selectedWorkArea.description)) : null)), /* @__PURE__ */ React.createElement(Tabs, {
		activeKey: activeKind,
		onChange: (key) => {
			setActiveKind(key);
			deviceList.resetDeviceView();
		},
		items: DEVICE_KIND_OPTIONS.map((kind) => ({
			key: kind,
			label: getText(DEVICE_KIND_LABEL_KEY[kind])
		}))
	}), /* @__PURE__ */ React.createElement(DeviceTable, {
		activeKind,
		data: deviceList.tableData,
		loading: deviceList.deviceRequest.loading,
		search: deviceList.search,
		typeFilter: deviceList.typeFilter,
		typeOptions: modelState.typeOptions,
		page: deviceList.page,
		pageSize: deviceList.pageSize,
		getText,
		onSearchChange: (value) => {
			deviceList.setSearch(value);
			deviceList.setPage(1);
		},
		onTypeFilterChange: (value) => {
			deviceList.setTypeFilter(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.createElement(DeviceDetailDrawer, {
		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.createElement(EditDeviceModal, {
		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.createElement(DeleteDeviceModal, {
		open: true,
		record: dialogs.selectedDevice,
		submitting: deviceList.removeDeviceRequest.loading,
		getText,
		onCancel: () => dialogs.setDeleteDeviceOpen(false),
		onConfirm: handleDeleteDevice
	}) : null, dialogs.addDeviceOpen ? /* @__PURE__ */ React.createElement(AddDeviceModal, {
		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, /* @__PURE__ */ React.createElement(WorkAreaModal, {
		open: dialogs.workAreaModalOpen,
		mode: dialogs.workAreaModalMode,
		record: dialogs.editingWorkArea,
		submitting: workArea.saveRequest.loading,
		getText,
		onCancel: dialogs.closeWorkAreaModal,
		onSubmit: workArea.handleSaveWorkArea
	}), /* @__PURE__ */ React.createElement(DeleteWorkAreaModal, {
		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
	}));
};
//#endregion
export { SaasDevicePlanning as default };
