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