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 { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { request } from "../../utils/index.js";
import { addBase, addWorkspace, getWorkspaces, updateBaseDetail } from "./server.js";
import Search from "./components/SearchWorkspaceAndBase/index.js";
import Sort$1 from "./components/Sort/index.js";
import WorkspaceItem from "./components/WorkspaceItem/index.js";
import React, { useMemo, useRef, useState } from "react";
import { Empty } from "antd";
import { Button as Button$1, Skeleton as Skeleton$1, Space as Space$1 } from "@pisell/materials";
import { useRequest } from "ahooks";
import "./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 = _objectWithoutProperties(props, _excluded);
	return /* @__PURE__ */ React.createElement("div", _objectSpread2({ className: "search-bar-wrap" }, rest), /* @__PURE__ */ React.createElement("div", { className: "line-1" }, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement("span", { className: "title" }, title)), /* @__PURE__ */ React.createElement(Space$1, { size: 24 }, /* @__PURE__ */ React.createElement(Search, null), /* @__PURE__ */ React.createElement(Button$1, {
		type: "primary",
		onClick: onAddWorkspace
	}, "Create a workspace"))), /* @__PURE__ */ React.createElement("div", { className: "line-2" }, /* @__PURE__ */ React.createElement(Sort$1, { 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 = _objectWithoutProperties(props, _excluded2);
	const [sort, setSort] = useState("");
	const [refreshKey, setRefreshKey] = useState({});
	const context = useEngineContext();
	request.setRequest(context.appHelper.utils.request);
	const tenant_id = getTenantId();
	const scrollRef = useRef(null);
	const { data = {}, loading, mutate } = useRequest(function() {
		var _ref = _asyncToGenerator(function* (params) {
			const newParams = params || {
				tenant_id,
				page: 1,
				size: defaultSize,
				order_by: sort
			};
			const { code, data } = yield 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 } = useRequest(addWorkspace, { manual: true });
	const add = useRequest(addBase, { manual: true });
	const handleAdd = function() {
		var _ref2 = _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 = _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 _objectSpread2(_objectSpread2({}, oldList), {}, { list: oldList.list.filter((item) => item.id !== id) });
		});
	};
	const handleUpdateBase = function() {
		var _ref4 = _asyncToGenerator(function* (params) {
			mutate((oldBase) => {
				if (!oldBase) return oldBase;
				const newList = oldBase.list.map((item) => {
					if (item.id === params.workspaceId) {
						var _item$base;
						return _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 _objectSpread2(_objectSpread2({}, bas), params);
							return bas;
						}) });
					}
					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(_x3) {
			return _ref4.apply(this, arguments);
		};
	}();
	const renderWorkspaceItem = (item) => /* @__PURE__ */ React.createElement(WorkspaceItem, {
		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(_objectSpread2(_objectSpread2({}, params), {}, { workspaceId: item.id })),
		onAddBase: (params) => handleAddBase(_objectSpread2(_objectSpread2({}, item), params))
	});
	const content = useMemo(() => {
		var _data$list;
		if (loading) return /* @__PURE__ */ React.createElement(Skeleton$1, null);
		if ((_data$list = data.list) === null || _data$list === void 0 ? void 0 : _data$list.length) return data.list.map(renderWorkspaceItem);
		return /* @__PURE__ */ React.createElement(Empty, null);
	}, [loading, data]);
	return /* @__PURE__ */ React.createElement("div", _objectSpread2({ className: "workspaces-wrap" }, rest), /* @__PURE__ */ React.createElement(SearchBar, {
		title: "All Workspaces",
		onAddWorkspace: handleAdd,
		onSort: handleSort
	}), /* @__PURE__ */ React.createElement("div", {
		className: "workspaces-list",
		ref: scrollRef
	}, content));
};
//#endregion
export { WorkspacesList as default };
