var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saasDevice/appDevicePlanning/hooks/useAppWorkAreas.ts
var useAppWorkAreas_exports = {};
__export(useAppWorkAreas_exports, {
  useAppWorkAreas: () => useAppWorkAreas
});
module.exports = __toCommonJS(useAppWorkAreas_exports);
var import_react = require("react");
var import_materials = require("@pisell/materials");
var import_ahooks = require("ahooks");
var import_api = require("../api");
var import_model = require("../model");
var EMPTY_COUNTS = {
  smart: 0,
  dumb: 0,
  integrated: 0
};
var useAppWorkAreas = ({
  getText,
  workAreaModalMode,
  editingWorkArea,
  closeWorkAreaModal
}) => {
  const [workAreas, setWorkAreas] = (0, import_react.useState)([]);
  const [selectedWorkAreaId, setSelectedWorkAreaId] = (0, import_react.useState)();
  const [deleteModalOpen, setDeleteModalOpen] = (0, import_react.useState)(false);
  const [deleteRecord, setDeleteRecord] = (0, import_react.useState)();
  const [deleteCounts, setDeleteCounts] = (0, import_react.useState)(EMPTY_COUNTS);
  const workAreaRequest = (0, import_ahooks.useRequest)(import_api.getWorkAreas, {
    onSuccess: (list) => {
      setWorkAreas((0, import_model.sortWorkAreaList)(list));
    }
  });
  const saveRequest = (0, import_ahooks.useRequest)(
    (payload) => workAreaModalMode === "create" ? (0, import_api.createWorkArea)(payload) : (0, import_api.updateWorkArea)((editingWorkArea == null ? void 0 : editingWorkArea.id) || "", payload),
    { manual: true }
  );
  const getWorkAreaDetailRequest = (0, import_ahooks.useRequest)(import_api.getWorkAreaDetail, {
    manual: true
  });
  const deleteRequest = (0, import_ahooks.useRequest)(import_api.deleteWorkArea, { manual: true });
  (0, import_react.useEffect)(() => {
    if (!selectedWorkAreaId) return;
    const exists = workAreas.some((item) => item.id === selectedWorkAreaId);
    if (!exists) {
      setSelectedWorkAreaId(void 0);
    }
  }, [selectedWorkAreaId, workAreas]);
  const selectedWorkArea = workAreas.find((item) => item.id === selectedWorkAreaId);
  const reloadWorkAreas = async (selectedId) => {
    const list = await workAreaRequest.runAsync();
    const sorted = (0, import_model.sortWorkAreaList)(list);
    setWorkAreas(sorted);
    if (selectedId) {
      setSelectedWorkAreaId(selectedId);
    }
    return sorted;
  };
  const handleSaveWorkArea = async (payload) => {
    try {
      const saved = await saveRequest.runAsync(payload);
      (0, import_materials.PisellToast)({
        content: getText(
          workAreaModalMode === "create" ? "pisell2.saas.device-planning.message.create-success" : "pisell2.saas.device-planning.message.update-success"
        ),
        duration: 2
      });
      closeWorkAreaModal();
      await reloadWorkAreas((saved == null ? void 0 : saved.id) || (editingWorkArea == null ? void 0 : editingWorkArea.id));
    } catch (e) {
    }
  };
  const openDeleteModal = (record) => {
    setDeleteRecord(record);
    setDeleteCounts(EMPTY_COUNTS);
    setDeleteModalOpen(true);
    getWorkAreaDetailRequest.runAsync({
      id: record.id,
      relation_count: ["smartDevices", "dumbDevices", "integratedDevices"]
    }).then((res) => {
      var _a;
      const workArea = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a[0];
      setDeleteCounts({
        smart: (workArea == null ? void 0 : workArea.smart_devices_count) || 0,
        dumb: (workArea == null ? void 0 : workArea.dumb_devices_count) || 0,
        integrated: (workArea == null ? void 0 : workArea.integrated_devices_count) || 0
      });
    }).catch(() => setDeleteCounts(EMPTY_COUNTS));
  };
  const closeDeleteModal = () => {
    setDeleteModalOpen(false);
    setDeleteRecord(void 0);
    setDeleteCounts(EMPTY_COUNTS);
  };
  const handleConfirmDelete = async () => {
    if (!deleteRecord) return false;
    try {
      await deleteRequest.runAsync(deleteRecord.id);
      (0, import_materials.PisellToast)({
        content: getText("pisell2.saas.device-planning.message.delete-success"),
        duration: 2
      });
      closeDeleteModal();
      await reloadWorkAreas();
      return true;
    } catch (e) {
      return false;
    }
  };
  return {
    workAreas,
    selectedWorkArea,
    selectedWorkAreaId,
    deleteModalOpen,
    deleteRecord,
    deleteCounts,
    workAreaRequest,
    saveRequest,
    getWorkAreaDetailRequest,
    deleteRequest,
    setSelectedWorkAreaId,
    reloadWorkAreas,
    handleSaveWorkArea,
    openDeleteModal,
    closeDeleteModal,
    handleConfirmDelete
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useAppWorkAreas
});
