import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import Search from "../SearchWorkspaceAndBase/index.js";
import Sort$1 from "../Sort/index.js";
import { antdConfirmPromise } from "../Modal/antdConfirmPromise.js";
import EditableString from "../EditableString/index.js";
import CreateBaseModal from "../CreateBaseModal/index.js";
import React, { useState } from "react";
import { Button, Dropdown, Space } from "@pisell/materials";
import ArrowLeft from "@pisell/icon/es/ArrowLeft";
import classNames from "classnames";
import { EllipsisOutlined } from "@ant-design/icons";
import Edit03 from "@pisell/icon/es/Edit03";
import InfoCircle from "@pisell/icon/es/InfoCircle";
import Trash01 from "@pisell/icon/es/Trash01";
import "./index.less";
//#region src/components/workSpaceList/components/SearchBar/index.tsx
const operateItems = [
	{
		key: "rename",
		label: "Rename workspace",
		icon: /* @__PURE__ */ React.createElement(Edit03, null)
	},
	{
		key: "editDescription",
		label: "Edit description",
		icon: /* @__PURE__ */ React.createElement(InfoCircle, null)
	},
	{ type: "divider" },
	{
		key: "delete",
		label: "Delete workspace",
		icon: /* @__PURE__ */ React.createElement(Trash01, null)
	}
];
const SearchBar = (props) => {
	const { title, onTitleChange, onDescribeChange, describe, isDetail, onDelete, onSort, onAddBase, editingTitle: editingTitleProps } = props;
	const [editingTitle, setEditingTitle] = useState(editingTitleProps);
	const [editingDescribe, setEditingDescribe] = useState(false);
	const [open, setOpen] = useState(false);
	const handleTitleChange = (val) => {
		if (!val) return;
		setEditingTitle(false);
		if (title === val) return;
		onTitleChange === null || onTitleChange === void 0 || onTitleChange(val);
	};
	const handleDescribeChange = (val) => {
		setEditingDescribe(false);
		if (val === describe) return;
		onDescribeChange === null || onDescribeChange === void 0 || onDescribeChange(val);
	};
	const handleGoBack = () => {
		window.history.back();
	};
	const onDropdownClick = function() {
		var _ref = _asyncToGenerator(function* ({ key }) {
			if (key === "rename") setEditingTitle(true);
			else if (key === "editDescription") setEditingDescribe(true);
			else if (key === "delete") {
				yield antdConfirmPromise({
					type: "confirm",
					title: "Are you sure you want to delete this workspace?",
					content: "There is base in this Workspace, and deletion can cause all of the data in base and base to be deleted.",
					okText: "Delete workspace"
				});
				onDelete === null || onDelete === void 0 || onDelete();
			}
		});
		return function onDropdownClick(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement("div", { className: classNames("search-bar-wrap", { ["detail-wrap"]: isDetail }) }, /* @__PURE__ */ React.createElement("div", { className: "line-1" }, /* @__PURE__ */ React.createElement("div", {
		className: "left",
		classNames: { item: "left-item" }
	}, isDetail && /* @__PURE__ */ React.createElement(ArrowLeft, {
		onClick: handleGoBack,
		className: "back-icon"
	}), /* @__PURE__ */ React.createElement(EditableString, {
		editing: editingTitle || editingTitleProps,
		editable: isDetail,
		text: title,
		onChange: handleTitleChange,
		maxLength: 200,
		ellipsis: { row: 1 },
		className: "title"
	})), /* @__PURE__ */ React.createElement(Space, {
		className: "right",
		size: isDetail ? 24 : 16
	}, isDetail && /* @__PURE__ */ React.createElement(Search, null), /* @__PURE__ */ React.createElement(Button, {
		type: isDetail ? "primary" : void 0,
		onClick: () => setOpen(true)
	}, "Create a base"), /* @__PURE__ */ React.createElement(Dropdown, { menu: {
		items: operateItems,
		onClick: onDropdownClick
	} }, /* @__PURE__ */ React.createElement(Button, { icon: /* @__PURE__ */ React.createElement(EllipsisOutlined, null) })))), /* @__PURE__ */ React.createElement(EditableString, {
		editing: editingDescribe,
		editable: false,
		text: describe || "",
		onChange: handleDescribeChange,
		className: "describe",
		ellipsis: { row: 3 },
		maxLength: 500
	}), isDetail && /* @__PURE__ */ React.createElement("div", { className: "line-2" }, /* @__PURE__ */ React.createElement(Sort$1, { onChange: onSort })), /* @__PURE__ */ React.createElement(CreateBaseModal, {
		open,
		onCancel: () => setOpen(false),
		onAddBase
	}));
};
//#endregion
export { SearchBar as default };
