function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import classNames from 'classnames';
import { image as imageAli, locales } from '@pisell/utils';
import { PREFIX } from "../constants";
import { toDisplayText } from "../utils/display";
import { formatMoney } from "../utils/render";
import AmountFooter from "./AmountFooter";
import ProductSpecs from "./ProductSpecs";
var getQuantity = function getQuantity(value) {
  var quantity = Number(value);
  return Number.isFinite(quantity) && quantity > 0 ? quantity : 1;
};
var BundleImage = function BundleImage(_ref) {
  var image = _ref.image,
    title = _ref.title,
    isShowImage = _ref.isShowImage;
  if (!isShowImage) return null;
  if (image) {
    return /*#__PURE__*/React.createElement("img", {
      className: "".concat(PREFIX, "__product-image"),
      src: imageAli.ali(image, 72),
      alt: ""
    });
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-image-text")
  }, title.slice(0, 2));
};
var ProductBundleItem = function ProductBundleItem(_ref2) {
  var bundle = _ref2.bundle,
    symbol = _ref2.symbol,
    _ref2$type = _ref2.type,
    type = _ref2$type === void 0 ? 'a2' : _ref2$type,
    isShowImage = _ref2.isShowImage,
    showFooter = _ref2.showFooter,
    maxSpecsCount = _ref2.maxSpecsCount;
  var title = toDisplayText(bundle.bundle_title);
  var quantity = getQuantity(bundle.num);
  var unitPrice = bundle.bundle_selling_price;
  var displayPrice = type === 'a9' && !showFooter ? Number(unitPrice || 0) * quantity : unitPrice;
  var quantityClassName = classNames("".concat(PREFIX, "__quantity"), "".concat(PREFIX, "__quantity--main"), _defineProperty(_defineProperty({}, "".concat(PREFIX, "__quantity--with-image"), Boolean(isShowImage)), "".concat(PREFIX, "__quantity--without-image"), !isShowImage));
  if (type === 'a5') {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__bundle-item-info ").concat(PREFIX, "__bundle-item-info--a5")
    }, /*#__PURE__*/React.createElement("span", {
      className: quantityClassName
    }, quantity < 100 ? quantity : '99+'), /*#__PURE__*/React.createElement(BundleImage, {
      image: bundle.cover,
      title: title,
      isShowImage: isShowImage
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__bundle-item-content")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__bundle-item-title")
    }, title), /*#__PURE__*/React.createElement(ProductSpecs, {
      options: bundle.option,
      maxSpecsCount: maxSpecsCount,
      symbol: symbol,
      type: type
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__bundle-item-price")
    }, formatMoney(unitPrice, symbol))));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__bundle-item-info ").concat(PREFIX, "__bundle-item-info--a9")
  }, /*#__PURE__*/React.createElement("span", {
    className: quantityClassName
  }, quantity < 100 ? quantity : '99+'), /*#__PURE__*/React.createElement(BundleImage, {
    image: bundle.cover,
    title: title,
    isShowImage: isShowImage
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__bundle-item-content")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__bundle-item-main")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__bundle-item-title")
  }, title), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__bundle-item-price")
  }, formatMoney(displayPrice, symbol))), /*#__PURE__*/React.createElement(ProductSpecs, {
    options: bundle.option,
    maxSpecsCount: maxSpecsCount,
    symbol: symbol,
    type: type
  })));
};
var ProductBundleInfo = function ProductBundleInfo(_ref3) {
  var _product$product_bund;
  var product = _ref3.product,
    symbol = _ref3.symbol,
    type = _ref3.type,
    isShowImage = _ref3.isShowImage,
    _ref3$maxSpecsCount = _ref3.maxSpecsCount,
    maxSpecsCount = _ref3$maxSpecsCount === void 0 ? 3 : _ref3$maxSpecsCount;
  if (!((_product$product_bund = product.product_bundle) !== null && _product$product_bund !== void 0 && _product$product_bund.length)) return null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-bundle")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-bundle-title")
  }, locales.getText('pisell2.sale-cart.line-item.packages')), /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__product-bundle-list")
  }, product.product_bundle.map(function (bundle, index) {
    var _bundle$option, _bundle$bundle_id;
    var showFooter = type === 'a9' && Boolean((_bundle$option = bundle.option) === null || _bundle$option === void 0 ? void 0 : _bundle$option.length);
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__product-bundle-item"),
      key: "".concat((_bundle$bundle_id = bundle.bundle_id) !== null && _bundle$bundle_id !== void 0 ? _bundle$bundle_id : index, "-").concat(index)
    }, /*#__PURE__*/React.createElement(ProductBundleItem, {
      bundle: bundle,
      symbol: symbol,
      type: type,
      isShowImage: isShowImage,
      showFooter: showFooter,
      maxSpecsCount: maxSpecsCount
    }), showFooter ? /*#__PURE__*/React.createElement(AmountFooter, {
      product: product,
      bundle: bundle,
      symbol: symbol
    }) : null);
  })));
};
export default ProductBundleInfo;