"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import MerchandisingAssistantSheet from "../../../backend/catalog/products/MerchandisingAssistantSheet.js";
function readString(value) {
  return typeof value === "string" && value.length > 0 ? value : null;
}
function readNumber(value) {
  if (typeof value === "number" && Number.isFinite(value)) return value;
  if (typeof value === "string") {
    const parsed = Number.parseInt(value, 10);
    if (Number.isFinite(parsed)) return parsed;
  }
  return 0;
}
function normalizeFilters(context) {
  const rawCategories = context?.filters?.categoryIds;
  const categoryIds = Array.isArray(rawCategories) ? rawCategories : [];
  const firstCategoryId = categoryIds.map(readString).find((value) => value !== null && value.length > 0) ?? null;
  const rawTags = context?.filters?.tagIds;
  const tags = Array.isArray(rawTags) ? rawTags.map(readString).filter((value) => value !== null) : [];
  const status = readString(context?.filters?.status);
  return {
    categoryId: firstCategoryId,
    priceRange: null,
    tags,
    status
  };
}
function computeCatalogMerchandisingPageContext(context) {
  const totalMatching = readNumber(context?.totalMatching ?? context?.total);
  const selectedRowIds = Array.isArray(context?.selectedRowIds) ? context.selectedRowIds : [];
  const selectedCount = selectedRowIds.length > 0 ? selectedRowIds.length : readNumber(context?.selectedCount);
  return {
    view: "catalog.products.list",
    entityType: "catalog.products.list",
    recordType: null,
    recordId: selectedRowIds.join(","),
    extra: {
      filter: normalizeFilters(context),
      totalMatching,
      selectedCount
    }
  };
}
function MerchandisingAssistantTriggerWidget({
  context
}) {
  const pageContext = React.useMemo(
    () => computeCatalogMerchandisingPageContext(context),
    [context]
  );
  return /* @__PURE__ */ jsx(MerchandisingAssistantSheet, { pageContext });
}
export {
  computeCatalogMerchandisingPageContext,
  MerchandisingAssistantTriggerWidget as default
};
//# sourceMappingURL=widget.client.js.map
