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/components/workSpaceListDetail/index.tsx
var workSpaceListDetail_exports = {};
__export(workSpaceListDetail_exports, {
  default: () => workSpaceListDetail_default
});
module.exports = __toCommonJS(workSpaceListDetail_exports);
var import_react = __toESM(require("react"));
var import_WorkspaceItem = __toESM(require("./../workSpaceList/components/WorkspaceItem"));
var import_useEngineContext = __toESM(require("./../../hooks/useEngineContext"));
var import_react2 = require("react");
var import_index = require("./index.less");
var import_server = require("./../workSpaceList/server");
var import_ahooks = require("ahooks");
var defaultSize = 50;
var WorkSpaceListDetail = () => {
  const context = (0, import_useEngineContext.default)();
  const { useParams } = context.appHelper.utils.getApp().history;
  const { id = 0 } = useParams();
  const [order_by, setOrder_by] = (0, import_react2.useState)("");
  const [refreshKey, setRefreshKey] = (0, import_react2.useState)({});
  const scrollRef = (0, import_react2.useRef)(null);
  const { data: detail } = (0, import_ahooks.useRequest)(import_server.getWorkspaceDetail, {
    defaultParams: [Number(id)],
    refreshDeps: [id]
  });
  const {
    data: baseDetail,
    loading,
    mutate
  } = (0, import_ahooks.useRequest)(
    async (params) => {
      const newParams = params || {
        workspace_id: Number(id),
        page: 1,
        size: defaultSize,
        order_by
      };
      const { code, data } = await (0, import_server.getBaseList)({
        workspace_id: newParams.workspace_id,
        page: newParams.page,
        size: newParams.size,
        order_by: newParams.order_by
      });
      return {
        workspace_id: newParams.workspace_id,
        list: code === 200 ? data.list || [] : [],
        page: newParams.page + 1,
        size: newParams.size,
        count: code === 200 ? data.count || 0 : 0,
        order_by: newParams.order_by
      };
    },
    {
      defaultParams: [
        {
          page: 1,
          size: defaultSize,
          workspace_id: Number(id)
        }
      ],
      loadMore: true,
      ref: scrollRef,
      isNoMore: (d) => {
        return d ? d.list.length >= d.count : false;
      },
      refreshDeps: [order_by, refreshKey]
    }
  );
  const add = (0, import_ahooks.useRequest)(import_server.addBase, {
    manual: true
  });
  const { name, id: workspace_id, tenant_id, description } = (detail == null ? void 0 : detail.data) || {};
  const { list = [] } = baseDetail || {};
  const handleSort = (val) => {
    setOrder_by((val == null ? void 0 : val[0]) || "");
  };
  const handleAddBase = async (params) => {
    const { name: name2, color, icon } = params;
    await add.run({
      name: name2,
      color,
      icon,
      workspace_id,
      tenant_id
    });
    setRefreshKey({});
  };
  const handleDeleteBase = async (id2) => {
    mutate((oldBase) => {
      if (!oldBase) return oldBase;
      return {
        ...oldBase,
        list: oldBase.list.filter((item) => item.id !== id2)
      };
    });
    await (0, import_server.deleteBaseDetail)(id2);
  };
  const handleUpdateBase = async (params) => {
    mutate((oldBase) => {
      if (!oldBase) return oldBase;
      const newList = oldBase.list.map((item) => {
        if (item.id === params.id) {
          return {
            ...item,
            ...params
          };
        }
        return item;
      });
      return {
        ...oldBase,
        list: newList
      };
    });
    await (0, import_server.updateBaseDetail)({
      id: params.id,
      color: params == null ? void 0 : params.color,
      icon: params == null ? void 0 : params.icon,
      name: params == null ? void 0 : params.name
    });
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "workspace-detail-wrap" }, /* @__PURE__ */ import_react.default.createElement(
    import_WorkspaceItem.default,
    {
      loading,
      onAddBase: handleAddBase,
      id: Number(id),
      title: name,
      data: list,
      isDetail: true,
      onSort: handleSort,
      onDeleteBase: handleDeleteBase,
      onUpdateBase: handleUpdateBase,
      onRefresh: () => setRefreshKey({}),
      scrollRef,
      describe: description
    }
  ));
};
var workSpaceListDetail_default = WorkSpaceListDetail;
