const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_server = require("../workSpaceList/server.js");
const require_index = require("../workSpaceList/components/WorkspaceItem/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/workSpaceListDetail/index.tsx
const defaultSize = 50;
const WorkSpaceListDetail = () => {
	const { useParams } = require_useEngineContext.default().appHelper.utils.getApp().history;
	const { id = 0 } = useParams();
	const [order_by, setOrder_by] = (0, react.useState)("");
	const [refreshKey, setRefreshKey] = (0, react.useState)({});
	const scrollRef = (0, react.useRef)(null);
	const { data: detail } = (0, ahooks.useRequest)(require_server.getWorkspaceDetail, {
		defaultParams: [Number(id)],
		refreshDeps: [id]
	});
	const { data: baseDetail, loading, mutate } = (0, ahooks.useRequest)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (params) {
			const newParams = params || {
				workspace_id: Number(id),
				page: 1,
				size: defaultSize,
				order_by
			};
			const { code, data } = yield require_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
			};
		});
		return function(_x) {
			return _ref.apply(this, arguments);
		};
	}(), {
		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, ahooks.useRequest)(require_server.addBase, { manual: true });
	const { name, id: workspace_id, tenant_id, description } = (detail === null || detail === void 0 ? void 0 : detail.data) || {};
	const { list = [] } = baseDetail || {};
	const handleSort = (val) => {
		setOrder_by((val === null || val === void 0 ? void 0 : val[0]) || "");
	};
	const handleAddBase = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (params) {
			const { name, color, icon } = params;
			yield add.run({
				name,
				color,
				icon,
				workspace_id,
				tenant_id
			});
			setRefreshKey({});
		});
		return function handleAddBase(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleDeleteBase = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (id) {
			mutate((oldBase) => {
				if (!oldBase) return oldBase;
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, oldBase), {}, { list: oldBase.list.filter((item) => item.id !== id) });
			});
			yield require_server.deleteBaseDetail(id);
		});
		return function handleDeleteBase(_x3) {
			return _ref3.apply(this, arguments);
		};
	}();
	const handleUpdateBase = function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (params) {
			mutate((oldBase) => {
				if (!oldBase) return oldBase;
				const newList = oldBase.list.map((item) => {
					if (item.id === params.id) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), params);
					return item;
				});
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, oldBase), {}, { list: newList });
			});
			yield require_server.updateBaseDetail({
				id: params.id,
				color: params === null || params === void 0 ? void 0 : params.color,
				icon: params === null || params === void 0 ? void 0 : params.icon,
				name: params === null || params === void 0 ? void 0 : params.name
			});
		});
		return function handleUpdateBase(_x4) {
			return _ref4.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement("div", { className: "workspace-detail-wrap" }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		loading,
		onAddBase: handleAddBase,
		id: Number(id),
		title: name,
		data: list,
		isDetail: true,
		onSort: handleSort,
		onDeleteBase: handleDeleteBase,
		onUpdateBase: handleUpdateBase,
		onRefresh: () => setRefreshKey({}),
		scrollRef,
		describe: description
	}));
};
//#endregion
exports.default = WorkSpaceListDetail;
