const require_index = require("../../../locales/index.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_index$1 = require("../../pisellToast/index.js");
const require_utils = require("../../table/utils.js");
(function() {
	var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
		return require_utils.request.getRequest().post("/dataSources:listEnabled?paginate=false&appends%5B%5D=collections", {}, { isNocobase: true });
	});
	return function getNocobaseDataSource() {
		return _ref.apply(this, arguments);
	};
})();
(function() {
	var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
		return require_utils.request.getRequest().post("/collections:list?paginate=false&appends%5B%5D=fields&sort%5B%5D=sort&filter=%7B%7D", {}, { isNocobase: true });
	});
	return function getNocobaseMainDataSource() {
		return _ref2.apply(this, arguments);
	};
})();
(function() {
	var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
		return require_utils.request.getRequest().post("/dataSources:list?appends=collections", {}, { isNocobase: true });
	});
	return function getNocobaseDataSourceOptions() {
		return _ref3.apply(this, arguments);
	};
})();
/**
* 创建nocobase数据
*/
const createNocobaseData = function() {
	var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (params) {
		const { key, data, headers } = params;
		return require_utils.request.getRequest().post(`/${key}:create`, data, {
			isNocobase: true,
			headers
		});
	});
	return function createNocobaseData(_x) {
		return _ref4.apply(this, arguments);
	};
}();
const formatQueryObjString = (query = {}) => {
	return Object.entries(query || {}).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&");
};
/**
* nocobase get数据
*/
const getNocobaseData = function() {
	var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (params) {
		const { key, query, headers } = params;
		return require_utils.request.getRequest().get(`/${key}:get?${formatQueryObjString(query)}`, {}, {
			isNocobase: true,
			headers
		});
	});
	return function getNocobaseData(_x2) {
		return _ref5.apply(this, arguments);
	};
}();
const methodMap = {
	GET: "get",
	POST: "post",
	PUT: "put",
	DELETE: "remove"
};
const formatHeaders = (headers) => {
	return headers === null || headers === void 0 ? void 0 : headers.reduce((acc, curr) => {
		acc[curr.name] = curr.value;
		return acc;
	}, {});
};
const formatQueryString = (params = []) => params.map(({ name, value }) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`).join("&");
const formatRequestUrl = (url, baseUrl, queryString) => {
	const baseRequestUrl = baseUrl ? `${baseUrl}${url}` : url;
	return queryString ? `${baseRequestUrl}${baseRequestUrl.includes("?") ? "&" : "?"}${queryString}` : baseRequestUrl;
};
/**
* 自定义api接口
*/
const customApi = function() {
	var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (url, data, options) {
		const { method, baseUrl, headers = [], params = [], body = {}, dataSourceKey } = options;
		const requestHeaders = formatHeaders(headers);
		const requestUrl = formatRequestUrl(url, baseUrl, formatQueryString(params));
		const requestData = method === "GET" ? data : require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, body), data);
		const requestMethod = methodMap[method];
		return require_utils.request.getRequest()[requestMethod](requestUrl, requestData, {
			headers: requestHeaders,
			isCustomApi: true,
			isShopApi: dataSourceKey === "mypisell-api"
		});
	});
	return function customApi(_x3, _x4, _x5) {
		return _ref6.apply(this, arguments);
	};
}();
/**
* 更新nocobase数据
*/
const updateNocobaseData = function() {
	var _ref7 = require_asyncToGenerator._asyncToGenerator(function* (params) {
		const { key, data, headers, query } = params;
		return require_utils.request.getRequest().post(`/${key}:update?${formatQueryObjString(query)}`, data, {
			isNocobase: true,
			headers
		});
	});
	return function updateNocobaseData(_x6) {
		return _ref7.apply(this, arguments);
	};
}();
/**
* 获取nocobase数据列表
*/
const getNocobaseDataList = function() {
	var _ref8 = require_asyncToGenerator._asyncToGenerator(function* (allParams) {
		const { key, params, headers } = allParams;
		return require_utils.request.getRequest().get(`/${key}:list`, params, {
			isNocobase: true,
			fullResult: true,
			headers
		});
	});
	return function getNocobaseDataList(_x7) {
		return _ref8.apply(this, arguments);
	};
}();
/**
* 删除nocobase数据
*/
const deleteNocobaseData = function() {
	var _ref9 = require_asyncToGenerator._asyncToGenerator(function* (key, id, headers) {
		return require_utils.request.getRequest().post(`/${key}:destroy?filterByTk=${id}`, {}, {
			isNocobase: true,
			headers
		});
	});
	return function deleteNocobaseData(_x8, _x9, _x10) {
		return _ref9.apply(this, arguments);
	};
}();
/**
* 获取formSettingMeta数据
* @param params 请求参数
* @param params.sectionCodes 配置段代码数组，如 ['payment']
* @param params.businessCode 业务代码
* @param params.channelCode 渠道代码
* @param params.isBoard 是否包含board_code参数
* @returns Promise<any> 返回设置元数据
*/
const getFormSettingMetaData = function() {
	var _ref10 = require_asyncToGenerator._asyncToGenerator(function* (params) {
		console.log("getFormSettingMetaData", params);
		const { sectionCodes, scope, target } = params;
		const url = `/tenant/setting`;
		if (!scope) {
			require_index$1.default({ content: require_index.getText("pisell-data-source-form-scope-message") });
			return {};
		}
		const payload = {
			scope,
			section_code: sectionCodes,
			target
		};
		return require_utils.request.getRequest().post(url, payload, { headers: { "Content-Type": "application/json" } });
	});
	return function getFormSettingMetaData(_x11) {
		return _ref10.apply(this, arguments);
	};
}();
/**
* 更新租户设置数据
* @param params 请求参数
* @param params.businessCode 业务代码
* @param params.channelCode 渠道代码
* @param params.boardCode 板块代码
* @param params.data 要更新的数据对象，如 {"order.prefix":"","order.suffix":"","payment.number":""}
* @returns Promise<any> 返回更新结果
*/
const updateTenantSetting = function() {
	var _ref11 = require_asyncToGenerator._asyncToGenerator(function* (params) {
		console.log("updateTenantSetting", params);
		const { scope, target, data } = params;
		const requestBody = {
			scope,
			target,
			data
		};
		if (!scope) {
			require_index$1.default({ content: require_index.getText("pisell-data-source-form-scope-message") });
			return {};
		}
		return require_utils.request.getRequest().put("/tenant/setting", requestBody, { headers: { "Content-Type": "application/json" } });
	});
	return function updateTenantSetting(_x12) {
		return _ref11.apply(this, arguments);
	};
}();
const buildDeviceSettingUrl = (scene = "template", id) => {
	switch (scene) {
		case "profile": return `/tenant/device/profile/${id}/setting`;
		case "instance": return `/tenant/device/${id}/setting`;
		default: return `/admin/device/profile/template/${id}/setting`;
	}
};
/**
* 获取设备设置元数据（POST 类型 GET）
* @param params.scene 场景，默认 template
* @param params.sceneRequestId URL 中 {id} 占位符的实际取值
* @param params.items 需要获取的字段列表，例如 ['order.name', 'order.id']
*/
const getDeviceSettingMetaData = function() {
	var _ref12 = require_asyncToGenerator._asyncToGenerator(function* (params) {
		const { scene, sceneRequestId, items } = params;
		console.log("getDeviceSettingMetaData", params);
		if (!sceneRequestId) {
			require_index$1.default({ content: require_index.getText("pisell-data-source-form-scene-request-id-message") });
			return {};
		}
		const url = buildDeviceSettingUrl(scene, sceneRequestId);
		return require_utils.request.getRequest().post(url, { items }, {
			headers: { "Content-Type": "application/json" },
			isPisell2: true
		});
	});
	return function getDeviceSettingMetaData(_x13) {
		return _ref12.apply(this, arguments);
	};
}();
/**
* 更新设备设置数据
* @param params.scene 场景，默认 template
* @param params.sceneRequestId URL 中 {id} 占位符的实际取值
* @param params.data 需要更新的字段值对象
*/
const updateDeviceSetting = function() {
	var _ref13 = require_asyncToGenerator._asyncToGenerator(function* (params) {
		const { scene, sceneRequestId, data } = params;
		console.log("updateDeviceSetting", params);
		if (!sceneRequestId) {
			require_index$1.default({ content: require_index.getText("pisell-data-source-form-scene-request-id-message") });
			return {};
		}
		const url = buildDeviceSettingUrl(scene, sceneRequestId);
		return require_utils.request.getRequest().put(url, { items: data }, {
			headers: { "Content-Type": "application/json" },
			isPisell2: true
		});
	});
	return function updateDeviceSetting(_x14) {
		return _ref13.apply(this, arguments);
	};
}();
//#endregion
exports.createNocobaseData = createNocobaseData;
exports.customApi = customApi;
exports.deleteNocobaseData = deleteNocobaseData;
exports.getDeviceSettingMetaData = getDeviceSettingMetaData;
exports.getFormSettingMetaData = getFormSettingMetaData;
exports.getNocobaseData = getNocobaseData;
exports.getNocobaseDataList = getNocobaseDataList;
exports.updateDeviceSetting = updateDeviceSetting;
exports.updateNocobaseData = updateNocobaseData;
exports.updateTenantSetting = updateTenantSetting;
