import React, { useMemo } from 'react';
import classNames from 'classnames';
import { image as imageAli, formatAmount } from '@pisell/utils';
import Specs from "../specs";
import Holders from "../holders";
import Weighing from "../weighing";
import { PREFIX } from "../../index";
import EditButton from "../../../components/Header/EditButton";
import "./index.less";
var BasicInfo = function BasicInfo(props) {
  var _origin;
  var isShowImage = props.isShowImage,
    _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? {} : _props$dataSource,
    style = props.style,
    isMainProduct = props.isMainProduct,
    type = props.type,
    _props$maxSpecsCount = props.maxSpecsCount,
    maxSpecsCount = _props$maxSpecsCount === void 0 ? 1 : _props$maxSpecsCount,
    isBundle = props.isBundle,
    showFooter = props.showFooter,
    _props$isShowOriginal = props.isShowOriginalPrice,
    isShowOriginalPrice = _props$isShowOriginal === void 0 ? false : _props$isShowOriginal,
    _props$isShowEditProd = props.isShowEditProduct,
    isShowEditProduct = _props$isShowEditProd === void 0 ? false : _props$isShowEditProd,
    onEditProduct = props.onEditProduct,
    _props$disabledEditCl = props.disabledEditClick,
    disabledEditClick = _props$disabledEditCl === void 0 ? false : _props$disabledEditCl;
  var image = dataSource.image,
    bundle = dataSource.bundle,
    options = dataSource.options,
    product_option_string = dataSource.product_option_string,
    name = dataSource.name,
    price = dataSource.price,
    symbol = dataSource.symbol,
    _dataSource$total = dataSource.total,
    total = _dataSource$total === void 0 ? 0 : _dataSource$total,
    _dataSource$origin_to = dataSource.origin_total,
    origin_total = _dataSource$origin_to === void 0 ? 0 : _dataSource$origin_to,
    _dataSource$num = dataSource.num,
    num = _dataSource$num === void 0 ? 1 : _dataSource$num,
    holder_id = dataSource.holder_id,
    isFormSubject = dataSource.isFormSubject,
    _dataSource$totalDiff = dataSource.totalDifference,
    totalDifference = _dataSource$totalDiff === void 0 ? 0 : _dataSource$totalDiff;
  var renderNumTag = function renderNumTag() {
    return /*#__PURE__*/React.createElement("div", {
      className: classNames("num-tag", {
        'is-main': isMainProduct
      })
    }, num < 100 ? num : '99+');
  };
  var hasSpecs = (bundle === null || bundle === void 0 ? void 0 : bundle.length) || (options === null || options === void 0 ? void 0 : options.length) || product_option_string;
  var weighingInfo = dataSource === null || dataSource === void 0 || (_origin = dataSource._origin) === null || _origin === void 0 || (_origin = _origin._extend) === null || _origin === void 0 ? void 0 : _origin.weighing;
  var hasWeighingInfo = Boolean(weighingInfo && (Number.isFinite(Number(weighingInfo === null || weighingInfo === void 0 ? void 0 : weighingInfo.weight)) || Number.isFinite(Number(weighingInfo === null || weighingInfo === void 0 ? void 0 : weighingInfo.net_weight))));
  var hasProductMeta = hasSpecs || hasWeighingInfo;
  var renderProductMeta = function renderProductMeta() {
    if (!hasProductMeta) {
      return null;
    }
    return /*#__PURE__*/React.createElement(React.Fragment, null, hasWeighingInfo ? /*#__PURE__*/React.createElement(Weighing, {
      dataSource: weighingInfo,
      symbol: symbol
    }) : null, hasSpecs ? /*#__PURE__*/React.createElement(Specs, {
      dataSource: dataSource,
      maxSpecsCount: maxSpecsCount,
      type: type
    }) : null);
  };
  var renderProductPrice = function renderProductPrice() {
    if (weighingInfo) {
      return null;
    }
    return formatAmount(price, 2, symbol);
  };
  var renderImage = function renderImage() {
    if (!isShowImage) return null;
    if (image) {
      return /*#__PURE__*/React.createElement("img", {
        src: imageAli.ali(image, 64),
        className: "product-cover"
      });
    }

    // 如果没有图片，显示文字图片
    var displayText = name ? name.slice(0, 2) : '';
    return /*#__PURE__*/React.createElement("div", {
      className: "product-cover-text"
    }, displayText);
  };

  /**
   * @description: 渲染编辑功能
   * @param {*} useMemo
   * @return {*}
   */
  var renderEdit = useMemo(function () {
    if (!isShowEditProduct) return null;
    return /*#__PURE__*/React.createElement(EditButton, {
      onEdit: function onEdit() {
        return onEditProduct === null || onEditProduct === void 0 ? void 0 : onEditProduct(dataSource);
      },
      disabledEditClick: disabledEditClick
    });
  }, [disabledEditClick]);
  var _isShowOriginalPrice = useMemo(function () {
    if (!isShowOriginalPrice) return false;
    if (hasSpecs || hasWeighingInfo) return false;
    var _originalTotal = parseFloat(String(origin_total || 0));
    var _total = parseFloat(String(total || 0));
    if (isNaN(_originalTotal) || isNaN(total)) return false;
    return _originalTotal !== 0 && _originalTotal != _total;
  }, [isShowOriginalPrice, origin_total, total, hasSpecs, hasWeighingInfo]);
  if (type === 'a5') {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__basic-info"),
      style: style || {}
    }, renderNumTag(), renderImage(), !isShowImage || !image && !name ? /*#__PURE__*/React.createElement("div", {
      style: {
        width: 20
      }
    }) : null, /*#__PURE__*/React.createElement("div", {
      className: "product-info product-info-a5"
    }, /*#__PURE__*/React.createElement("div", {
      className: "product-basic-info-wrap"
    }, /*#__PURE__*/React.createElement("div", {
      className: "product-name ".concat(renderEdit ? 'product-name-edit' : '')
    }, name), renderProductMeta(), (!holder_id || !(holder_id !== null && holder_id !== void 0 && holder_id.length)) && !isFormSubject ? /*#__PURE__*/React.createElement("div", {
      className: "product-price"
    }, renderProductPrice()) : /*#__PURE__*/React.createElement(Holders, {
      dataSource: dataSource
    })), renderEdit));
  }
  if (type === 'a9') {
    var showAmount = isBundle && !showFooter;
    // 套餐列表中，如果没有规格，则显示总价
    var _price = showAmount ? Number(price !== null && price !== void 0 ? price : 0) * num : price;
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__basic-info"),
      style: style || {}
    }, renderNumTag(), renderImage(), !isShowImage || !image && !name ? /*#__PURE__*/React.createElement("div", {
      style: {
        width: 20
      }
    }) : null, /*#__PURE__*/React.createElement("div", {
      className: "product-info product-info-a9"
    }, /*#__PURE__*/React.createElement("div", {
      className: "product-basic-info-wrap"
    }, /*#__PURE__*/React.createElement("div", {
      className: "product-basic-info ".concat(isBundle ? 'row' : '')
    }, /*#__PURE__*/React.createElement("div", {
      className: "product-name ".concat(renderEdit ? 'product-name-edit' : '')
    }, name), /*#__PURE__*/React.createElement("div", {
      className: "product-price"
    }, /*#__PURE__*/React.createElement("span", null, renderProductPrice()), _isShowOriginalPrice ? /*#__PURE__*/React.createElement("span", {
      className: "original-price"
    }, formatAmount(origin_total, 2, symbol)) : null)), renderProductMeta()), renderEdit));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__basic-info"),
    style: style || {}
  }, renderNumTag(), renderImage(), !isShowImage || !image && !name ? /*#__PURE__*/React.createElement("div", {
    style: {
      width: 20
    }
  }) : null, /*#__PURE__*/React.createElement("div", {
    className: "product-info"
  }, /*#__PURE__*/React.createElement("div", {
    className: "product-name"
  }, name), hasProductMeta ? renderProductMeta() : /*#__PURE__*/React.createElement("div", {
    className: "product-price"
  }, renderProductPrice())), /*#__PURE__*/React.createElement("div", {
    className: "product-amount"
  }, /*#__PURE__*/React.createElement("div", {
    className: "total-amont"
  }, formatAmount(total * num - (totalDifference !== null && totalDifference !== void 0 ? totalDifference : 0), 2, symbol)), origin_total && Number(total) !== Number(origin_total) ? /*#__PURE__*/React.createElement("div", {
    className: "origin-amount"
  }, formatAmount(origin_total * num, 2, symbol)) : null));
};
export default BasicInfo;