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_useEngineContext = require("../../../../../hooks/useEngineContext.js");
const require_index = require("../../../../../utils/index.js");
const require_serve = require("./serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_materials = require("@pisell/materials");
//#region src/components/systemSettings/registry/components/DataSourceTreeSelect/index.tsx
/**
* 辅助函数：检查是否为字符串
*/
const isString = (value) => {
	return typeof value === "string";
};
const defaultArr = [];
/**
* 数据源选择器组件
* @param props 组件属性
*/
const DataSourceTreeSelect = (props) => {
	var _context$appHelper, _context$appHelper$ge;
	const { value, onChange, disabled, mode, variables = defaultArr, isShowOriginalDataSource = true, filters, dataSource = [] } = props;
	console.log("dataSource>>>>", dataSource);
	console.log("value>>>", value);
	const context = require_useEngineContext.default();
	const { request: apiRequest } = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getApp) === null || _context$appHelper$ge === void 0 ? void 0 : _context$appHelper$ge.call(_context$appHelper)) || {};
	require_index.request.setRequest(apiRequest);
	const valueRef = (0, react.useRef)(value);
	valueRef.current = value;
	const [treeData, setTreeData] = (0, react.useState)([]);
	/**
	* 获取表格数据源
	*/
	const getTableDataSource = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			if (!isShowOriginalDataSource) return [];
			return dataSource.map((item) => {
				let collections = item.collections;
				if (item.type === "custom-api") {
					if (mode === "add") collections = item.collections.filter((collection) => {
						var _collection$types;
						return collection === null || collection === void 0 || (_collection$types = collection.types) === null || _collection$types === void 0 ? void 0 : _collection$types.includes("create");
					});
					else if (mode === "edit") collections = item.collections.filter((collection) => {
						var _collection$types2;
						return collection === null || collection === void 0 || (_collection$types2 = collection.types) === null || _collection$types2 === void 0 ? void 0 : _collection$types2.includes("update");
					});
					else if (mode === "view") collections = item.collections.filter((collection) => {
						var _collection$types3;
						return collection === null || collection === void 0 || (_collection$types3 = collection.types) === null || _collection$types3 === void 0 ? void 0 : _collection$types3.includes("get");
					});
					else if (mode === "list") collections = item.collections.filter((collection) => {
						var _collection$types4;
						return collection === null || collection === void 0 || (_collection$types4 = collection.types) === null || _collection$types4 === void 0 ? void 0 : _collection$types4.includes("list");
					});
				}
				const children = collections === null || collections === void 0 ? void 0 : collections.map((collection) => {
					var _item$options;
					return {
						title: collection.title,
						value: collection.key || collection.tableName,
						item: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, collection), {}, {
							headerDataSource: item.key,
							options: item.type === "mysql" ? {} : item === null || item === void 0 || (_item$options = item.options) === null || _item$options === void 0 ? void 0 : _item$options.options
						})
					};
				});
				return {
					title: item.displayName,
					value: item.key,
					selectable: false,
					children
				};
			});
		});
		return function getTableDataSource() {
			return _ref.apply(this, arguments);
		};
	}();
	(function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (filterItem) {
			if (filterItem !== "all") {
				if (filterItem) {}
			}
			return {
				title: "变量",
				value: "variables",
				selectable: false,
				children: variables.map((item) => {
					const treeNode = {
						title: item.label,
						value: item.value,
						item: item.item,
						isVariable: true
					};
					if (item.children) treeNode.children = item.children.map((child) => ({
						title: child.label,
						value: child.value,
						item: child.item,
						isVariable: true
					}));
					return treeNode;
				})
			};
		});
		return function getVariablesDataSource(_x) {
			return _ref2.apply(this, arguments);
		};
	})();
	/**
	* 获取数据源映射对象
	*/
	const dataSources = { tables: getTableDataSource };
	/**
	* 获取菜单数据源
	*/
	const getMenuDataSource = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (_treeData) {
			if ((filters === null || filters === void 0 ? void 0 : filters.variables) && typeof filters.variables !== "string") {
				if (filters.variables.menu !== "all") return;
			}
			const res = yield require_serve.getNocobaseMenuDataSource();
			setTreeData(_treeData.map((d) => {
				if (d.value === "variables") {
					d.children = d.children || [];
					d.children.push(res);
				}
				return d;
			}));
		});
		return function getMenuDataSource(_x2) {
			return _ref3.apply(this, arguments);
		};
	}();
	/**
	* 初始化数据源
	*/
	const initDataSource = function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* () {
			let _treeData = [];
			for (const key in dataSources) {
				const fn = dataSources[key];
				const filterKey = key;
				if (!filters || filters[filterKey]) {
					const res = yield fn(filters ? filters[filterKey] : "all");
					_treeData = _treeData.concat(res);
				}
			}
			setTreeData(_treeData);
			getMenuDataSource(_treeData);
		});
		return function initDataSource() {
			return _ref4.apply(this, arguments);
		};
	}();
	/**
	* 处理值变化事件
	* @description 外部需要的是item
	*/
	const _onChange = function() {
		var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (value) {
			onChange((value === null || value === void 0 ? void 0 : value.item) || null);
		});
		return function _onChange(_x3) {
			return _ref5.apply(this, arguments);
		};
	}();
	/**
	* 处理选择事件
	*/
	const handleSelect = function() {
		var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (val, option) {
			_onChange({
				value: val,
				title: option.title,
				item: option.item,
				isSelect: true,
				isVariable: option.isVariable
			});
		});
		return function handleSelect(_x4, _x5) {
			return _ref6.apply(this, arguments);
		};
	}();
	/**
	* 处理值变化事件
	*/
	const handleChange = function() {
		var _ref7 = require_asyncToGenerator._asyncToGenerator(function* (val, option) {
			if (!val) _onChange({
				value: "",
				item: null,
				title: ""
			});
		});
		return function handleChange(_x6, _x7) {
			return _ref7.apply(this, arguments);
		};
	}();
	const currentValue = (0, react.useMemo)(() => {
		if (isString(value)) return value;
		return (value === null || value === void 0 ? void 0 : value.key) || (value === null || value === void 0 ? void 0 : value.tableName);
	}, [JSON.stringify(value)]);
	(0, react.useEffect)(() => {
		initDataSource();
	}, [dataSource, variables]);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_pisell_materials.TreeSelect, {
		treeExpandAction: "click",
		disabled,
		showSearch: true,
		popupMatchSelectWidth: false,
		treeNodeFilterProp: "title",
		style: { width: "100%" },
		value: currentValue,
		allowClear: true,
		onChange: handleChange,
		treeData,
		onSelect: handleSelect
	}));
};
//#endregion
exports.default = DataSourceTreeSelect;
