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_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_index = require("../../utils/index.js");
const require_server = require("./server.js");
const require_index$1 = require("./components/SearchWorkspaceAndBase/index.js");
const require_index$2 = require("./components/Sort/index.js");
const require_index$3 = require("./components/WorkspaceItem/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/workSpaceList/index.tsx
const _excluded = [
	"title",
	"onAddWorkspace",
	"onSort",
	"className"
], _excluded2 = ["className"];
const SearchBar = (props) => {
	const { title, onAddWorkspace, onSort, className } = props, rest = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({ className: "search-bar-wrap" }, rest), /* @__PURE__ */ react.default.createElement("div", { className: "line-1" }, /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("span", { className: "title" }, title)), /* @__PURE__ */ react.default.createElement(_pisell_materials.Space, { size: 24 }, /* @__PURE__ */ react.default.createElement(require_index$1.default, null), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		onClick: onAddWorkspace
	}, "Create a workspace"))), /* @__PURE__ */ react.default.createElement("div", { className: "line-2" }, /* @__PURE__ */ react.default.createElement(require_index$2.default, { onChange: onSort })));
};
const defaultSize = 15;
const getTenantId = () => {
	return Number(new URLSearchParams(window.location.search).get("tenant_id") || 0);
};
const WorkspacesList = (props) => {
	const { className } = props, rest = require_objectWithoutProperties._objectWithoutProperties(props, _excluded2);
	const [sort, setSort] = (0, react.useState)("");
	const [refreshKey, setRefreshKey] = (0, react.useState)({});
	const context = require_useEngineContext.default();
	require_index.request.setRequest(context.appHelper.utils.request);
	const tenant_id = getTenantId();
	const scrollRef = (0, react.useRef)(null);
	const { data = {}, loading, mutate } = (0, ahooks.useRequest)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (params) {
			const newParams = params || {
				tenant_id,
				page: 1,
				size: defaultSize,
				order_by: sort
			};
			const { code, data } = yield require_server.getWorkspaces({
				tenant_id,
				page: newParams.page,
				size: newParams.size,
				order_by: newParams.order_by
			});
			return {
				tenant_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,
			tenant_id,
			order_by: sort
		}],
		ref: scrollRef,
		isNoMore: (d) => {
			return d ? d.list.length >= d.count : false;
		},
		refreshDeps: [sort, refreshKey]
	});
	const { runAsync } = (0, ahooks.useRequest)(require_server.addWorkspace, { manual: true });
	const add = (0, ahooks.useRequest)(require_server.addBase, { manual: true });
	const handleAdd = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
			var _workspace$data;
			const workspace = yield runAsync({ tenant_id });
			context.appHelper.utils.getApp().history.push(`/workspaces/${(workspace === null || workspace === void 0 || (_workspace$data = workspace.data) === null || _workspace$data === void 0 ? void 0 : _workspace$data.id) || "0"}?create=true`);
		});
		return function handleAdd() {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleSort = (val) => {
		setSort((val === null || val === void 0 ? void 0 : val[0]) || "");
	};
	const handleAddBase = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (params) {
			const { name, color, icon } = params;
			yield add.run({
				name,
				color,
				icon,
				workspace_id: params.id,
				tenant_id: params.tenant_id
			});
			setRefreshKey({});
		});
		return function handleAddBase(_x2) {
			return _ref3.apply(this, arguments);
		};
	}();
	const handleDelete = (id) => {
		mutate((oldList) => {
			if (!oldList) return oldList;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, oldList), {}, { list: oldList.list.filter((item) => item.id !== id) });
		});
	};
	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.workspaceId) {
						var _item$base;
						return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { base: (_item$base = item.base) === null || _item$base === void 0 ? void 0 : _item$base.map((bas) => {
							if (bas.id === params.id) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bas), params);
							return bas;
						}) });
					}
					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(_x3) {
			return _ref4.apply(this, arguments);
		};
	}();
	const renderWorkspaceItem = (item) => /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		key: item.id,
		id: item.id,
		title: item.name,
		loading: false,
		describe: item.description,
		data: item.base || [],
		onRefresh: () => setRefreshKey({}),
		onDelete: () => handleDelete(item.id),
		baseCount: item.base_count,
		onUpdateBase: (params) => handleUpdateBase(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, params), {}, { workspaceId: item.id })),
		onAddBase: (params) => handleAddBase(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), params))
	});
	const content = (0, react.useMemo)(() => {
		var _data$list;
		if (loading) return /* @__PURE__ */ react.default.createElement(_pisell_materials.Skeleton, null);
		if ((_data$list = data.list) === null || _data$list === void 0 ? void 0 : _data$list.length) return data.list.map(renderWorkspaceItem);
		return /* @__PURE__ */ react.default.createElement(antd.Empty, null);
	}, [loading, data]);
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({ className: "workspaces-wrap" }, rest), /* @__PURE__ */ react.default.createElement(SearchBar, {
		title: "All Workspaces",
		onAddWorkspace: handleAdd,
		onSort: handleSort
	}), /* @__PURE__ */ react.default.createElement("div", {
		className: "workspaces-list",
		ref: scrollRef
	}, content));
};
//#endregion
exports.default = WorkspacesList;
