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/devicePlanning/hooks/useDeviceList.ts
var useDeviceList_exports = {};
__export(useDeviceList_exports, {
  useDeviceList: () => useDeviceList
});
module.exports = __toCommonJS(useDeviceList_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var import_constants = require("../constants");
var import_serve = require("../serve");
var import_utils = require("../utils");
var EMPTY_DEVICES = [];
var isSameIds = (a, b) => a.length === b.length && a.every((id, index) => id === b[index]);
var useDeviceList = ({
  activeKind,
  selectedWorkAreaId,
  translation,
  profileNameMap
}) => {
  const [search, setSearch] = (0, import_react.useState)("");
  const [typeFilter, setTypeFilter] = (0, import_react.useState)(void 0);
  const [deviceOrderIds, setDeviceOrderIds] = (0, import_react.useState)([]);
  const [page, setPage] = (0, import_react.useState)(1);
  const [pageSize, setPageSize] = (0, import_react.useState)(import_constants.DEFAULT_PAGE_SIZE);
  const deviceRequest = (0, import_ahooks.useRequest)(import_serve.getDevices, { manual: true });
  const createDeviceRequest = (0, import_ahooks.useRequest)(import_serve.createDevice, { manual: true });
  const updateDeviceRequest = (0, import_ahooks.useRequest)(
    (params) => (0, import_serve.updateDevice)(params.id, params.payload),
    { manual: true }
  );
  const removeDeviceRequest = (0, import_ahooks.useRequest)(import_serve.deleteDevice, { manual: true });
  const sortDeviceRequest = (0, import_ahooks.useRequest)(import_serve.sortDevices, { manual: true });
  const getDeviceListParams = (0, import_react.useCallback)(
    () => ({
      workarea_id: selectedWorkAreaId,
      type_kind: activeKind,
      with: ["type", "model", "workarea"]
    }),
    [activeKind, selectedWorkAreaId]
  );
  (0, import_react.useEffect)(() => {
    if (!selectedWorkAreaId) return;
    deviceRequest.run(getDeviceListParams());
  }, [getDeviceListParams, selectedWorkAreaId]);
  const devices = deviceRequest.data || EMPTY_DEVICES;
  (0, import_react.useEffect)(() => {
    const nextIds = devices.map((item) => item.id);
    setDeviceOrderIds((prev) => isSameIds(prev, nextIds) ? prev : nextIds);
  }, [devices]);
  const toDeviceRow = (0, import_react.useCallback)(
    (item) => (0, import_utils.createDeviceRow)(item, translation, profileNameMap),
    [profileNameMap, translation]
  );
  const tableData = (0, import_react.useMemo)(() => {
    const keyword = (0, import_constants.normalizeText)(search);
    const orderMap = new Map(deviceOrderIds.map((id, index) => [id, index]));
    return [...devices].sort(
      (a, b) => (orderMap.get(a.id) ?? Number.MAX_SAFE_INTEGER) - (orderMap.get(b.id) ?? Number.MAX_SAFE_INTEGER)
    ).map(toDeviceRow).filter((item) => {
      const matchesKeyword = keyword ? (0, import_constants.normalizeText)(item.name).includes(keyword) : true;
      const matchesType = typeFilter ? item.modelCode === typeFilter : true;
      return matchesKeyword && matchesType;
    });
  }, [deviceOrderIds, devices, search, toDeviceRow, typeFilter]);
  const resetDeviceView = () => {
    setSearch("");
    setTypeFilter(void 0);
    setPage(1);
  };
  const reloadDevices = () => {
    if (!selectedWorkAreaId) return;
    deviceRequest.run(getDeviceListParams());
  };
  const reloadDevicesAsync = () => {
    if (!selectedWorkAreaId) return Promise.resolve(EMPTY_DEVICES);
    return deviceRequest.runAsync(getDeviceListParams());
  };
  const handleDeviceSort = async (orderedRows) => {
    const ids = orderedRows.map((item) => item.id);
    setDeviceOrderIds(ids);
    try {
      await sortDeviceRequest.runAsync(ids);
      reloadDevices();
    } catch (e) {
      reloadDevices();
    }
  };
  return {
    search,
    typeFilter,
    page,
    pageSize,
    devices,
    tableData,
    deviceRequest,
    createDeviceRequest,
    updateDeviceRequest,
    removeDeviceRequest,
    setSearch,
    setTypeFilter,
    setPage,
    setPageSize,
    resetDeviceView,
    reloadDevices,
    reloadDevicesAsync,
    toDeviceRow,
    handleDeviceSort
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useDeviceList
});
