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 AppearanceShow from "../AppearanceShow/index.js";
import { searchWorkspace } from "../../server.js";
import React, { useEffect, useState } from "react";
import { AutoComplete, Input } from "@pisell/materials";
import SearchLg from "@pisell/icon/es/SearchLg";
import { useRequest } from "ahooks";
import "../../index.less";
//#region src/components/workSpaceList/components/SearchWorkspaceAndBase/index.tsx
const renderWorkspaceItem = (value, label) => ({
	value: label,
	id: value,
	type: "workspace",
	label: /* @__PURE__ */ React.createElement("div", null, label)
});
const renderBaseItem = (detail) => ({
	value: detail.name,
	id: detail.id,
	type: "base",
	label: /* @__PURE__ */ React.createElement("div", { className: "render-base-item-wrap" }, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(AppearanceShow, {
		icon: detail.icon,
		color: detail.color,
		name: detail.name,
		size: 40,
		fontSize: 16
	})), /* @__PURE__ */ React.createElement("div", { className: "right-detail" }, /* @__PURE__ */ React.createElement("div", { className: "line-1" }, detail.name), /* @__PURE__ */ React.createElement("div", { className: "line-2" }, detail.created_at)))
});
const Search = () => {
	const context = useEngineContext();
	request.setRequest(context.appHelper.utils.request);
	const [options, setOptions] = useState([]);
	new URLSearchParams(window.location.search);
	const { runAsync, data = {} } = useRequest(searchWorkspace, {
		manual: true,
		debounceWait: 300
	});
	const handleSearch = function() {
		var _ref = _asyncToGenerator(function* (searchText) {
			if (!searchText) return;
			yield runAsync({
				tenant_id: "0",
				search: searchText
			});
		});
		return function handleSearch(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useEffect(() => {
		const { workspace, base } = (data === null || data === void 0 ? void 0 : data.data) || {};
		if ((workspace === null || workspace === void 0 ? void 0 : workspace.length) || (base === null || base === void 0 ? void 0 : base.length)) setOptions([{
			label: "Workspace",
			options: (workspace === null || workspace === void 0 ? void 0 : workspace.map((item) => {
				return renderWorkspaceItem(item.id, item.name);
			})) || []
		}, {
			label: "Base",
			options: (base === null || base === void 0 ? void 0 : base.map((item) => {
				return renderBaseItem(item);
			})) || []
		}]);
	}, [data]);
	const handleSelect = (value, option) => {
		if (option.type === "workspace") {} else if (option.type === "base") {}
	};
	return /* @__PURE__ */ React.createElement(AutoComplete, {
		popupMatchSelectWidth: 400,
		style: { width: 240 },
		options,
		onSearch: handleSearch,
		onSelect: handleSelect
	}, /* @__PURE__ */ React.createElement(Input, {
		prefix: /* @__PURE__ */ React.createElement(SearchLg, { className: "search-icon" }),
		placeholder: "Search",
		size: "large"
	}));
};
//#endregion
export { Search as default };
