var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/productSelect/utils.ts
var utils_exports = {};
__export(utils_exports, {
  getElementByIdentifier: () => getElementByIdentifier,
  getOffsetTopRelativeToScrollContainer: () => getOffsetTopRelativeToScrollContainer,
  groupProductsByCategory: () => groupProductsByCategory
});
module.exports = __toCommonJS(utils_exports);
var import_utils = require("@pisell/utils");
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({
          ...product,
          __typename: "product",
          __categoryId: cat.id
        });
      });
    } else {
      uncategorizedProducts.push({
        ...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 categoryProducts = categoryProductsMap.get(category.id) || [];
    const sortedProducts = categoryProducts.sort((a, b) => {
      const sortA = a.sort || 0;
      const sortB = b.sort || 0;
      return sortB - sortA;
    });
    result.push(...sortedProducts);
  });
  if (uncategorizedProducts.length > 0) {
    const otherCategory = {
      __typename: "category",
      id: 0,
      name: import_utils.locales.getText("pisell2.text.other"),
      icon: "",
      slug: "other",
      sort: 99999999
      // 确保other分组排在最后
    };
    result.push(otherCategory);
    const sortedUncategorizedProducts = uncategorizedProducts.sort((a, b) => {
      const sortA = a.sort || 0;
      const sortB = b.sort || 0;
      return sortB - sortA;
    });
    result.push(...sortedUncategorizedProducts);
  }
  console.log("xxxxxx", result);
  return result;
}
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;
}
var 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") {
    const matrix = new DOMMatrix(bodyTransform);
    scaleY = matrix.d;
  }
  const targetRect = targetElement.getBoundingClientRect();
  const containerRect = scrollContainer.getBoundingClientRect();
  return targetRect.top / scaleY - containerRect.top / scaleY + scrollContainer.scrollTop;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getElementByIdentifier,
  getOffsetTopRelativeToScrollContainer,
  groupProductsByCategory
});
