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");
//#region src/components/systemSettings/registry/services.ts
const URL_MAP = {
	default: {
		"group:create": "/api/setting_group:create",
		"group:update": (id) => `/api/setting_group:update?filterByTk=${id}`,
		"group:sort": "/admin/setting/group/sort",
		"section:list": "/api/section:list",
		"group:list": "/api/setting_group:list",
		"item": (id) => `/admin/setting/item${id !== void 0 ? `/${id}` : ""}`,
		"item:sort": (id) => `/admin/setting/item/${id}/sort`,
		"item:list": "/api/setting_item:list"
	},
	device: {
		"group:create": "/api/device_setting_group:create",
		"group:update": (id) => `/api/device_setting_group:update?filterByTk=${id}`,
		"group:sort": "/admin/device/setting/group/sort",
		"section:list": "/api/device_section:list",
		"group:list": "/api/device_setting_group:list",
		"item": (id) => `/admin/device/setting/item${id !== void 0 ? `/${id}` : ""}`,
		"item:sort": (id) => `/admin/device/setting/item/${id}/sort`,
		"item:list": "/api/device_setting_item:list"
	}
};
const buildUrl = (scene, key, id) => {
	const value = (scene && URL_MAP[scene] || URL_MAP.default)[key];
	return typeof value === "function" ? value(id) : value;
};
const createGroup = function() {
	var _ref = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			if (!params.title || !params.code || !params.section_code || params.sort === void 0) throw new Error("Missing required parameters for group creation");
			return yield request.post(buildUrl(scene, "group:create"), params, {
				isNocobase: true,
				headers: { "x-data-source": "pisell2" }
			});
		} catch (error) {
			console.error("Failed to create group:", error);
			throw error;
		}
	});
	return function createGroup(_x, _x2, _x3) {
		return _ref.apply(this, arguments);
	};
}();
const updateGroup = function() {
	var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			if (!params.id || !params.title || !params.code) throw new Error("Missing required parameters for group update");
			return yield request.post(buildUrl(scene, "group:update", params.id), params, {
				isNocobase: true,
				headers: { "x-data-source": "pisell2" }
			});
		} catch (error) {
			console.error("Failed to update group:", error);
			throw error;
		}
	});
	return function updateGroup(_x4, _x5, _x6) {
		return _ref2.apply(this, arguments);
	};
}();
const deleteGroup = function() {
	var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			if (!params.id) throw new Error("Missing required parameters for group deletion");
			return yield request.post(buildUrl(scene, "group:update", params.id), { deleted_at: (/* @__PURE__ */ new Date()).toISOString() }, {
				isNocobase: true,
				headers: { "x-data-source": "pisell2" }
			});
		} catch (error) {
			console.error("Failed to delete group:", error);
			throw error;
		}
	});
	return function deleteGroup(_x7, _x8, _x9) {
		return _ref3.apply(this, arguments);
	};
}();
const sortGroups = function() {
	var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			if (!params.section_code || !params.group_ids || !Array.isArray(params.group_ids)) throw new Error("Missing required parameters for group sorting");
			return yield request.post(buildUrl(scene, "group:sort"), {
				section_code: params.section_code,
				group_ids: params.group_ids
			}, { headers: { "Content-Type": "application/json" } });
		} catch (error) {
			console.error("Failed to sort groups:", error);
			throw error;
		}
	});
	return function sortGroups(_x10, _x11, _x12) {
		return _ref4.apply(this, arguments);
	};
}();
const getSectionList = function() {
	var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			const { page = 1, pageSize = 20, filter = {} } = params;
			const filterWithSoftDelete = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, filter), {}, { deleted_at: null });
			const queryParams = new URLSearchParams({
				page: page.toString(),
				pageSize: pageSize.toString(),
				filter: JSON.stringify(filterWithSoftDelete)
			});
			return yield request.get(`${buildUrl(scene, "section:list")}?${queryParams.toString()}`, {}, {
				isNocobase: true,
				headers: { "x-data-source": "pisell2" }
			});
		} catch (error) {
			console.error("Failed to fetch section list:", error);
			throw error;
		}
	});
	return function getSectionList(_x13, _x14, _x15) {
		return _ref5.apply(this, arguments);
	};
}();
const getGroupList = function() {
	var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			const { filter = {} } = params;
			const filterWithSoftDelete = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, filter), {}, { deleted_at: null });
			const queryParams = new URLSearchParams({
				paginate: "false",
				filter: JSON.stringify(filterWithSoftDelete),
				appends: "items",
				sort: "sort,id"
			});
			const response = yield request.get(`${buildUrl(scene, "group:list")}?${queryParams.toString()}`, {}, {
				isNocobase: true,
				headers: { "x-data-source": "pisell2" }
			});
			if (response.data.length > 0) response.data.forEach((item) => {
				item.items = item.items.filter((item) => item.deleted_at === null);
			});
			return response;
		} catch (error) {
			console.error("Failed to fetch group list:", error);
			throw error;
		}
	});
	return function getGroupList(_x16, _x17, _x18) {
		return _ref6.apply(this, arguments);
	};
}();
const createItem = function() {
	var _ref7 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			if (!params.section_code || !params.group_id || !params.title || !params.code || !params.type) throw new Error("Missing required parameters for item creation");
			return yield request.post(buildUrl(scene, "item"), params);
		} catch (error) {
			console.error("Failed to create item:", error);
			throw error;
		}
	});
	return function createItem(_x19, _x20, _x21) {
		return _ref7.apply(this, arguments);
	};
}();
const updateItem = function() {
	var _ref8 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			if (!params.id || !params.section_code || !params.group_id || !params.title || !params.code || !params.type) throw new Error("Missing required parameters for item update");
			return yield request.put(buildUrl(scene, "item", params.id), params);
		} catch (error) {
			console.error("Failed to update item:", error);
			throw error;
		}
	});
	return function updateItem(_x22, _x23, _x24) {
		return _ref8.apply(this, arguments);
	};
}();
const deleteItem = function() {
	var _ref9 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			console.log(request);
			if (!params.id) throw new Error("Missing required parameters for item deletion");
			return yield request.remove(buildUrl(scene, "item", params.id), {});
		} catch (error) {
			console.error("Failed to delete item:", error);
			throw error;
		}
	});
	return function deleteItem(_x25, _x26, _x27) {
		return _ref9.apply(this, arguments);
	};
}();
const sortItems = function() {
	var _ref10 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			if (!params.id || !params.group_id || !params.item_ids) throw new Error("Missing required parameters for item sorting");
			return yield request.post(buildUrl(scene, "item:sort", params.id), {
				group_id: params.group_id,
				item_ids: params.item_ids
			}, { headers: { "Content-Type": "application/json" } });
		} catch (error) {
			console.error("Failed to sort items:", error);
			throw error;
		}
	});
	return function sortItems(_x28, _x29, _x30) {
		return _ref10.apply(this, arguments);
	};
}();
(function() {
	var _ref11 = require_asyncToGenerator._asyncToGenerator(function* (request, params, scene) {
		try {
			const { page = 1, pageSize = 20, filter = {} } = params;
			const filterWithSoftDelete = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, filter), {}, { deleted_at: null });
			const queryParams = new URLSearchParams({
				page: page.toString(),
				pageSize: pageSize.toString(),
				filter: JSON.stringify(filterWithSoftDelete)
			});
			return yield request.get(`${buildUrl(scene, "item:list")}?${queryParams.toString()}`, {});
		} catch (error) {
			console.error("Failed to fetch item list:", error);
			throw error;
		}
	});
	return function getItemList(_x31, _x32, _x33) {
		return _ref11.apply(this, arguments);
	};
})();
//#endregion
exports.createGroup = createGroup;
exports.createItem = createItem;
exports.deleteGroup = deleteGroup;
exports.deleteItem = deleteItem;
exports.getGroupList = getGroupList;
exports.getSectionList = getSectionList;
exports.sortGroups = sortGroups;
exports.sortItems = sortItems;
exports.updateGroup = updateGroup;
exports.updateItem = updateItem;
