import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { locales } from "@pisell/utils";
//#region src/plus/productSelect/utils.ts
/**
* 按目录对商品进行分组，产出平级的数据结构用于虚拟列表渲染
* @param products 商品列表
* @returns 包含目录项和商品项的平级数组
*/
function groupProductsByCategory(products) {
	if (!products || products.length === 0) return [];
	const categoryMap = /* @__PURE__ */ new Map();
	const categoryProductsMap = /* @__PURE__ */ new Map();
	const uncategorizedProducts = [];
	products.forEach((product) => {
		if (product.category && Array.isArray(product.category) && product.category.length > 0) product.category.forEach((cat) => {
			if (!categoryMap.has(cat.id)) categoryMap.set(cat.id, {
				__typename: "category",
				id: cat.id,
				name: cat.name,
				icon: cat.icon,
				slug: cat.slug,
				sort: cat.sort
			});
			if (!categoryProductsMap.has(cat.id)) categoryProductsMap.set(cat.id, []);
			categoryProductsMap.get(cat.id).push(_objectSpread2(_objectSpread2({}, product), {}, {
				__typename: "product",
				__categoryId: cat.id
			}));
		});
		else uncategorizedProducts.push(_objectSpread2(_objectSpread2({}, product), {}, {
			__typename: "product",
			__categoryId: 0
		}));
	});
	const sortedCategories = Array.from(categoryMap.values()).sort((a, b) => b.sort - a.sort);
	const result = [];
	sortedCategories.forEach((category) => {
		result.push(category);
		const sortedProducts = (categoryProductsMap.get(category.id) || []).sort((a, b) => {
			const sortA = a.sort || 0;
			return (b.sort || 0) - sortA;
		});
		result.push(...sortedProducts);
	});
	if (uncategorizedProducts.length > 0) {
		const otherCategory = {
			__typename: "category",
			id: 0,
			name: locales.getText("pisell2.text.other"),
			icon: "",
			slug: "other",
			sort: 99999999
		};
		result.push(otherCategory);
		const sortedUncategorizedProducts = uncategorizedProducts.sort((a, b) => {
			const sortA = a.sort || 0;
			return (b.sort || 0) - sortA;
		});
		result.push(...sortedUncategorizedProducts);
	}
	return result;
}
/**
* 根据传入的元素标识符获取DOM元素
* @param elementIdentifier 元素标识符，可以是HTMLElement、带'#'的id、带'.'的类名或CSS选择器
* @returns 找到的DOM元素或null
*/
function getElementByIdentifier(elementIdentifier) {
	if (!elementIdentifier) return null;
	if (elementIdentifier instanceof HTMLElement) return elementIdentifier;
	if (typeof elementIdentifier === "string") {
		if (elementIdentifier.startsWith("#")) return document.getElementById(elementIdentifier.slice(1));
		if (elementIdentifier.startsWith(".")) return document.querySelector(elementIdentifier);
		return document.querySelector(elementIdentifier);
	}
	return null;
}
const getOffsetTopRelativeToScrollContainer = (targetElement, scrollContainer) => {
	if (!targetElement || !scrollContainer) return 0;
	if (targetElement === scrollContainer) return 0;
	const bodyTransform = getComputedStyle(document.body).transform;
	let scaleY = 1;
	if (bodyTransform !== "none") scaleY = new DOMMatrix(bodyTransform).d;
	const targetRect = targetElement.getBoundingClientRect();
	const containerRect = scrollContainer.getBoundingClientRect();
	return targetRect.top / scaleY - containerRect.top / scaleY + scrollContainer.scrollTop;
};
//#endregion
export { getElementByIdentifier, getOffsetTopRelativeToScrollContainer, groupProductsByCategory };
