/**
 * 价格图例
 * 展示场地商品标题与表述（来自 venueProducts），最多一行四个，超出自动换行
 */
import React, { useMemo } from 'react';
import { locales } from '@pisell/utils';
import { useVenueBookingContext } from "../../../context";
import { useVenueBookingSymbol } from "../../../hooks";
export default function PriceLegend() {
  var _viewModel$timeSlotGr2;
  var symbol = useVenueBookingSymbol();
  var _useVenueBookingConte = useVenueBookingContext(),
    viewModel = _useVenueBookingConte.viewModel;
  var venueProducts = useMemo(function () {
    var _viewModel$timeSlotGr;
    var products = viewModel.venueProducts || [];
    if (!products.length) return [];
    var resources = ((_viewModel$timeSlotGr = viewModel.timeSlotGrid) === null || _viewModel$timeSlotGr === void 0 ? void 0 : _viewModel$timeSlotGr.resources) || [];
    var resourceProductIdSet = new Set();
    var resourceProductTitleSet = new Set();
    resources.forEach(function (resource) {
      var productId = Number(resource === null || resource === void 0 ? void 0 : resource.productId);
      if (!Number.isNaN(productId) && productId > 0) {
        resourceProductIdSet.add(productId);
      }
      var productTitle = String((resource === null || resource === void 0 ? void 0 : resource.productTitle) || '').trim();
      if (productTitle) {
        resourceProductTitleSet.add(productTitle);
      }
    });

    // 兜底：若当前 grid 还没拿到资源数据，先保持原有展示，避免闪空
    if (!resourceProductIdSet.size && !resourceProductTitleSet.size) return products;
    return products.filter(function (product) {
      var _ref, _product$id;
      var productId = Number((_ref = (_product$id = product === null || product === void 0 ? void 0 : product.id) !== null && _product$id !== void 0 ? _product$id : product === null || product === void 0 ? void 0 : product.product_id) !== null && _ref !== void 0 ? _ref : product === null || product === void 0 ? void 0 : product.productId);
      if (!Number.isNaN(productId) && resourceProductIdSet.has(productId)) return true;
      var productTitle = String((product === null || product === void 0 ? void 0 : product.title) || (product === null || product === void 0 ? void 0 : product.name) || '').trim();
      if (productTitle && resourceProductTitleSet.has(productTitle)) return true;
      return false;
    });
  }, [viewModel.venueProducts, (_viewModel$timeSlotGr2 = viewModel.timeSlotGrid) === null || _viewModel$timeSlotGr2 === void 0 ? void 0 : _viewModel$timeSlotGr2.resources]);
  if (!venueProducts.length) return null;
  var gridClassName = venueProducts.length === 1 ? 'grid grid-cols-1 gap-2 md:gap-3' : 'grid grid-cols-1 gap-2 md:grid-cols-2 md:gap-3 xl:grid-cols-4';
  return /*#__PURE__*/React.createElement("div", {
    className: gridClassName
  }, venueProducts.map(function (product, index) {
    var lines = getLegendLines(product, symbol);
    var fallbackTitlePrefix = locales.getText('pisell2.venue.legend.product.fallback') || '商品';
    var title = String((product === null || product === void 0 ? void 0 : product.title) || (product === null || product === void 0 ? void 0 : product.name) || "".concat(fallbackTitlePrefix, " ").concat(index + 1));
    var cardClassName = index % 2 === 0 ? 'bg-[#fff4f4] ring-[#f4caca]' : 'bg-[#fff7f7] ring-[#f4d7d7]';
    return /*#__PURE__*/React.createElement("div", {
      key: String((product === null || product === void 0 ? void 0 : product.id) || "".concat(title, "-").concat(index)),
      className: "min-w-0 rounded-[16px] px-3 py-3 ring-1 md:px-4 ".concat(cardClassName)
    }, /*#__PURE__*/React.createElement("div", {
      className: "truncate text-[11px] font-semibold text-zinc-900 md:text-sm"
    }, title), lines.length ? lines.map(function (line) {
      return /*#__PURE__*/React.createElement("div", {
        key: line,
        className: "mt-1 text-[10px] text-zinc-600 md:text-sm [&_p]:m-0 [&_br]:leading-tight",
        dangerouslySetInnerHTML: {
          __html: line
        }
      });
    }) : /*#__PURE__*/React.createElement("div", {
      className: "mt-1 text-[10px] text-zinc-500 md:text-sm"
    }, "-"));
  }));
}

/**
 * 提取商品表述文案：
 * 1) 优先读取 metadata 里配置的 legend 数组
 * 2) 再读取常见描述字段
 * 3) 最后回退显示价格
 */
function getLegendLines(product, symbol) {
  var _product$metadata;
  var metadataLines = product === null || product === void 0 || (_product$metadata = product.metadata) === null || _product$metadata === void 0 ? void 0 : _product$metadata.venue_booking_legend;
  if (Array.isArray(metadataLines) && metadataLines.length) {
    return metadataLines.map(function (item) {
      return String(item || '').trim();
    }).filter(Boolean).slice(0, 2);
  }
  var descriptionCandidates = [product === null || product === void 0 ? void 0 : product.description, product === null || product === void 0 ? void 0 : product.sub_title, product === null || product === void 0 ? void 0 : product.subtitle, product === null || product === void 0 ? void 0 : product.summary, product === null || product === void 0 ? void 0 : product.intro].map(function (item) {
    return String(item || '').trim();
  }).filter(Boolean);
  if (descriptionCandidates.length) return descriptionCandidates.slice(0, 2);
  var price = (product === null || product === void 0 ? void 0 : product.price) != null ? "".concat(symbol).concat(product.price) : '';
  var originalPrice = (product === null || product === void 0 ? void 0 : product.original_price) != null ? "".concat(symbol).concat(product.original_price) : '';
  var fallbackLine = [price, originalPrice ? "\xB7 ".concat(originalPrice) : ''].join('').trim();
  return fallbackLine ? [fallbackLine] : [];
}