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 { request } from "../../../../../utils/index.js";
//#region src/components/systemSettings/registry/components/DataSourceTreeSelect/serve.ts
/**
* GET 请求封装
* @param url - 请求 URL
* @param params - 请求参数
* @param config - 额外配置
*/
const get = function() {
	var _ref = _asyncToGenerator(function* (url, params = {}, config = {}) {
		try {
			return yield request.getRequest().get(url, params, config);
		} catch (error) {
			console.error("GET request failed:", error);
			return null;
		}
	});
	return function get(_x) {
		return _ref.apply(this, arguments);
	};
}();
/**
* POST 请求封装
* @param url - 请求 URL
* @param data - 请求数据
* @param config - 额外配置
*/
const post = function() {
	var _ref2 = _asyncToGenerator(function* (url, data = {}, config = {}) {
		try {
			return yield request.getRequest().post(url, data, config);
		} catch (error) {
			console.error("POST request failed:", error);
			return null;
		}
	});
	return function post(_x2) {
		return _ref2.apply(this, arguments);
	};
}();
/**
* 获取 Nocobase 自定义数据源
* @returns 自定义数据源列表（如 API 数据源等）
*/
const getNocobaseDataSource = function() {
	var _ref3 = _asyncToGenerator(function* () {
		try {
			const res = yield post("/dataSources:listEnabled?paginate=false&appends%5B%5D=collections", {}, { isNocobase: true });
			return Array.isArray(res) ? res : (res === null || res === void 0 ? void 0 : res.data) || [];
		} catch (error) {
			console.error("Failed to fetch nocobase data sources:", error);
			return [];
		}
	});
	return function getNocobaseDataSource() {
		return _ref3.apply(this, arguments);
	};
}();
/**
* 获取 Nocobase 主数据源（集合列表）
* @returns 主数据源的集合列表
*/
const getNocobaseMainDataSource = function() {
	var _ref4 = _asyncToGenerator(function* () {
		try {
			const res = yield post("/collections:list?paginate=false&appends%5B%5D=fields&sort%5B%5D=sort&filter=%7B%7D", {}, { isNocobase: true });
			return Array.isArray(res) ? res : (res === null || res === void 0 ? void 0 : res.data) || [];
		} catch (error) {
			console.error("Failed to fetch nocobase main data source:", error);
			return [];
		}
	});
	return function getNocobaseMainDataSource() {
		return _ref4.apply(this, arguments);
	};
}();
/**
* 获取 Nocobase 数据源配置选项
* @returns 数据源配置选项列表
*/
const getNocobaseDataSourceOptions = function() {
	var _ref5 = _asyncToGenerator(function* () {
		try {
			const res = yield post("/dataSources:list?appends=collections", {}, { isNocobase: true });
			return Array.isArray(res) ? res : (res === null || res === void 0 ? void 0 : res.data) || [];
		} catch (error) {
			console.error("Failed to fetch nocobase data source options:", error);
			return [];
		}
	});
	return function getNocobaseDataSourceOptions() {
		return _ref5.apply(this, arguments);
	};
}();
/**
* 合并主数据源和其他数据源
* @param main - 主数据源集合列表
* @param other - 其他数据源列表
* @param options - 数据源配置选项
* @returns 合并后的数据源列表
*/
const mergeDataSources = (main, other, options) => {
	const safeMain = Array.isArray(main) ? main : [];
	const safeOther = Array.isArray(other) ? other : [];
	const optionsMap = (Array.isArray(options) ? options : []).reduce((acc, item) => {
		if (item && item.key) acc[item.key] = item;
		return acc;
	}, {});
	const otherDataSource = safeOther.map((item) => _objectSpread2(_objectSpread2({}, item), {}, { options: optionsMap[item.key] }));
	return [{
		displayName: "主数据源",
		key: "main",
		type: "main",
		collections: safeMain
	}, ...otherDataSource];
};
/**
* 获取所有数据源（主数据源 + 其他数据源）
* 自动调用三个接口并合并数据
* @returns 合并后的数据源列表
*/
const getAllDataSources = function() {
	var _ref6 = _asyncToGenerator(function* () {
		try {
			const [other, main, options] = yield Promise.all([
				getNocobaseDataSource(),
				getNocobaseMainDataSource(),
				getNocobaseDataSourceOptions()
			]);
			console.log("Data sources fetched:", {
				other: Array.isArray(other) ? `${other.length} items` : typeof other,
				main: Array.isArray(main) ? `${main.length} items` : typeof main,
				options: Array.isArray(options) ? `${options.length} items` : typeof options
			});
			const merged = mergeDataSources(main, other, options);
			console.log("Merged data sources:", merged.length, "items");
			return merged;
		} catch (error) {
			console.error("Failed to fetch all data sources:", error);
			return [];
		}
	});
	return function getAllDataSources() {
		return _ref6.apply(this, arguments);
	};
}();
/**
* 获取菜单数据源
* @returns 菜单树节点数据
*/
const getNocobaseMenuDataSource = function() {
	var _ref7 = _asyncToGenerator(function* () {
		try {
			const res = yield get("/navigation_menu:list", {
				page: 1,
				pageSize: 100,
				filter: {}
			}, {
				isNocobase: true,
				headers: { "x-data-source": "pisell2" }
			});
			const data = Array.isArray(res) ? res : (res === null || res === void 0 ? void 0 : res.data) || [];
			console.log("Menu data source fetched:", {
				type: typeof res,
				isArray: Array.isArray(res),
				dataLength: data.length
			});
			return {
				title: "菜单",
				value: "menu",
				selectable: false,
				children: data.map((item) => {
					var _item$name;
					return {
						title: ((_item$name = item.name) === null || _item$name === void 0 ? void 0 : _item$name["original"]) || item.name || "Untitled",
						value: `{{global.value.menu.${item.code}}}`
					};
				})
			};
		} catch (error) {
			console.error("Failed to fetch menu data source:", error);
			return {
				title: "菜单",
				value: "menu",
				selectable: false,
				children: []
			};
		}
	});
	return function getNocobaseMenuDataSource() {
		return _ref7.apply(this, arguments);
	};
}();
//#endregion
export { getAllDataSources, getNocobaseMenuDataSource };
