import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../hooks/useEngineContext.js";
import { request } from "../../../../utils/index.js";
import { deleteWorkspace, updateWorkspace } from "../../server.js";
import CardList from "../CardList/index.js";
import SearchBar from "../SearchBar/index.js";
import React, { useEffect, useState } from "react";
import { Spin } from "antd";
import ArrowRight from "@pisell/icon/es/ArrowRight";
import "./index.less";
//#region src/components/workSpaceList/components/WorkspaceItem/index.tsx
const detailWrapStyle = {
	border: "none",
	padding: 0
};
const WorkspaceItem = (props) => {
	const { title, data, describe, isDetail, id = 0, onSort, onAddBase, loading, onRefresh, onDelete, onDeleteBase, onUpdateBase, scrollRef, baseCount } = props;
	const context = useEngineContext();
	request.setRequest(context.appHelper.utils.request);
	const searchParams = new URLSearchParams(window.location.search);
	const [editingTitle, setEditingTitle] = useState(false);
	useEffect(() => {
		if (searchParams.get("create") === "true") setEditingTitle(true);
	}, [searchParams]);
	const handleDetailClick = () => {
		context.appHelper.utils.getApp().history.push(`/workspaces/${id}`);
	};
	const handleDelete = function() {
		var _ref = _asyncToGenerator(function* () {
			yield deleteWorkspace(id);
			onDelete === null || onDelete === void 0 || onDelete();
		});
		return function handleDelete() {
			return _ref.apply(this, arguments);
		};
	}();
	const handleTitleChange = function() {
		var _ref2 = _asyncToGenerator(function* (name) {
			setEditingTitle(false);
			yield updateWorkspace({
				id,
				name
			});
		});
		return function handleTitleChange(_x) {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleDescribeChange = function() {
		var _ref3 = _asyncToGenerator(function* (description) {
			yield updateWorkspace({
				id,
				description
			});
		});
		return function handleDescribeChange(_x2) {
			return _ref3.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement("div", {
		className: "workspace-item-wrap",
		style: isDetail ? detailWrapStyle : {}
	}, /* @__PURE__ */ React.createElement(SearchBar, {
		title,
		describe,
		isDetail,
		onDelete: handleDelete,
		onTitleChange: handleTitleChange,
		onDescribeChange: handleDescribeChange,
		onSort,
		onAddBase,
		editingTitle
	}), /* @__PURE__ */ React.createElement(Spin, {
		spinning: loading,
		wrapperClassName: "loading"
	}, /* @__PURE__ */ React.createElement(CardList, {
		isDetail: isDetail || false,
		data,
		onAddBase,
		onRefresh,
		onDeleteBase,
		onUpdateBase,
		scrollRef
	})), !isDetail && /* @__PURE__ */ React.createElement("div", { className: "view-workspace-wrap" }, /* @__PURE__ */ React.createElement("span", {
		className: "view-go-detail",
		onClick: handleDetailClick
	}, /* @__PURE__ */ React.createElement("span", { className: "view-workspace-text" }, "View workspace"), /* @__PURE__ */ React.createElement(ArrowRight, null)), !!baseCount && /* @__PURE__ */ React.createElement("span", {
		className: "count",
		onClick: handleDetailClick
	}, baseCount, " bases in total")));
};
//#endregion
export { WorkspaceItem as default };
