var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saasDevice/devicePlanning/index.tsx
var devicePlanning_exports = {};
__export(devicePlanning_exports, {
  default: () => devicePlanning_default
});
module.exports = __toCommonJS(devicePlanning_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_antd = require("antd");
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_useLocale = __toESM(require("../../../hooks/useLocale"));
var import_utils2 = require("../../../utils");
var import_hooks = require("./hooks");
var import_useDeviceList = require("./hooks/useDeviceList");
var import_useDeviceModels = require("./hooks/useDeviceModels");
var import_useDevicePlanningDialogs = require("./hooks/useDevicePlanningDialogs");
var import_useWorkAreas = require("./hooks/useWorkAreas");
var import_locales = __toESM(require("./locales"));
var import_constants = require("./constants");
var import_WorkAreaList = __toESM(require("./components/WorkAreaList"));
var import_DeviceTable = __toESM(require("./components/DeviceTable"));
var import_AddDeviceModal = __toESM(require("./components/AddDeviceModal"));
var import_DeviceDetailDrawer = __toESM(require("./components/DeviceDetailDrawer"));
var import_EditDeviceModal = __toESM(require("./components/EditDeviceModal"));
var import_DeleteDeviceModal = __toESM(require("./components/DeleteDeviceModal"));
var import_WorkAreaModal = __toESM(require("./components/WorkAreaModal"));
var import_DeleteWorkAreaModal = __toESM(require("./components/DeleteWorkAreaModal"));
var import_types = require("./types");
var import_index = require("./index.less");
var SaasDevicePlanning = ({
  className,
  style
}) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  const locale = (0, import_useLocale.default)();
  import_utils.locales.init(import_locales.default, locale || "en");
  const getText = import_utils.locales.getText;
  const translation = (0, import_hooks.useTranslation)();
  const utils = ((_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) || {};
  const [activeKind, setActiveKind] = (0, import_react.useState)("smart");
  const dialogs = (0, import_useDevicePlanningDialogs.useDevicePlanningDialogs)();
  (0, import_react.useEffect)(() => {
    if (utils == null ? void 0 : utils.request) {
      import_utils2.request.setRequest(utils.request);
    }
  }, [utils == null ? void 0 : utils.request]);
  const workArea = (0, import_useWorkAreas.useWorkAreas)({
    getText,
    workAreaModalMode: dialogs.workAreaModalMode,
    editingWorkArea: dialogs.editingWorkArea,
    closeWorkAreaModal: dialogs.closeWorkAreaModal
  });
  const modelState = (0, import_useDeviceModels.useDeviceModels)({
    activeKind,
    selectedWorkAreaId: workArea.selectedWorkAreaId,
    devices: [],
    translation
  });
  const deviceList = (0, import_useDeviceList.useDeviceList)({
    activeKind,
    selectedWorkAreaId: workArea.selectedWorkAreaId,
    translation,
    profileNameMap: modelState.profileNameMap
  });
  const openAddDeviceModal = () => {
    modelState.loadBrandsIfNeeded();
    dialogs.setAddDeviceOpen(true);
  };
  const handleCreateDevice = async (payload) => {
    try {
      const created = await deviceList.createDeviceRequest.runAsync(payload);
      (0, import_materials.PisellToast)({
        content: getText("pisell2.saas.device-planning.add.create-success"),
        duration: 2
      });
      dialogs.setAddDeviceOpen(false);
      deviceList.setPage(1);
      const refreshedDevices = await deviceList.reloadDevicesAsync();
      if (created == null ? 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) {
    }
  };
  const handleUpdateDevice = async (id, payload) => {
    try {
      await deviceList.updateDeviceRequest.runAsync({ id, payload });
      (0, import_materials.PisellToast)({
        content: getText("pisell2.saas.device-planning.edit.success"),
        duration: 2
      });
      dialogs.setEditDeviceOpen(false);
      dialogs.setDetailOpen(false);
      dialogs.clearSelectedDevice();
      deviceList.reloadDevices();
    } catch (e) {
    }
  };
  const handleDeleteDevice = async () => {
    if (!dialogs.selectedDevice) return;
    try {
      await deviceList.removeDeviceRequest.runAsync(dialogs.selectedDevice.id);
      (0, import_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 /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("saas-device-planning", className),
      style
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_WorkAreaList.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__ */ import_react.default.createElement("main", { className: "saas-device-planning__content" }, workArea.showWorkAreaLoading ? /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-device-planning__empty-workarea" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, null)) : workArea.showEmptyWorkArea ? /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-device-planning__empty-workarea" }, /* @__PURE__ */ import_react.default.createElement(
      import_materials.Empty,
      {
        image: import_materials.Empty.PRESENTED_IMAGE_SIMPLE,
        description: getText(
          "pisell2.saas.device-planning.empty.workarea-first"
        )
      }
    )) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-device-planning__page-header" }, /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-device-planning__title" }, workArea.selectedWorkArea ? translation(workArea.selectedWorkArea.name) : "-"), ((_b = workArea.selectedWorkArea) == null ? void 0 : _b.description) ? /* @__PURE__ */ import_react.default.createElement("div", { className: "saas-device-planning__subtitle" }, translation(workArea.selectedWorkArea.description)) : null)), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Tabs,
      {
        activeKey: activeKind,
        onChange: (key) => {
          setActiveKind(key);
          deviceList.resetDeviceView();
        },
        items: import_types.DEVICE_KIND_OPTIONS.map((kind) => ({
          key: kind,
          label: getText(import_constants.DEVICE_KIND_LABEL_KEY[kind])
        }))
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      import_DeviceTable.default,
      {
        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__ */ import_react.default.createElement(
      import_DeviceDetailDrawer.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__ */ import_react.default.createElement(
      import_EditDeviceModal.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__ */ import_react.default.createElement(
      import_DeleteDeviceModal.default,
      {
        open: true,
        record: dialogs.selectedDevice,
        submitting: deviceList.removeDeviceRequest.loading,
        getText,
        onCancel: () => dialogs.setDeleteDeviceOpen(false),
        onConfirm: handleDeleteDevice
      }
    ) : null,
    dialogs.addDeviceOpen ? /* @__PURE__ */ import_react.default.createElement(
      import_AddDeviceModal.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,
    /* @__PURE__ */ import_react.default.createElement(
      import_WorkAreaModal.default,
      {
        open: dialogs.workAreaModalOpen,
        mode: dialogs.workAreaModalMode,
        record: dialogs.editingWorkArea,
        submitting: workArea.saveRequest.loading,
        getText,
        onCancel: dialogs.closeWorkAreaModal,
        onSubmit: workArea.handleSaveWorkArea
      }
    ),
    /* @__PURE__ */ import_react.default.createElement(
      import_DeleteWorkAreaModal.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
      }
    )
  );
};
var devicePlanning_default = SaasDevicePlanning;
