function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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); }
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); }
import React, { useMemo } from 'react';
import Decimal from 'decimal.js';
import { useStore } from "../../hooks";
import { SKUCard } from "../../../../pro/Cards";
var NormalCard = function NormalCard(props) {
  var _props$dataSource;
  var _ref = (props === null || props === void 0 ? void 0 : props.dataSource) || {},
    uniqueKey = _ref._key,
    ruleConfig = _ref.ruleConfig,
    _original = _ref._original,
    disabled = _ref.disabled,
    selected = _ref.selected;
  var _ref2 = _original || {},
    cover = _ref2.cover,
    title = _ref2.title,
    name = _ref2.name,
    product_subtitle = _ref2.product_subtitle,
    price = _ref2.price,
    price_type = _ref2.price_type,
    add_price = _ref2.add_price;
  var symbol = useStore(function (s) {
    return s.symbol;
  });
  var skuCardConfig = useStore(function (s) {
    return s.skuCardConfig;
  });
  var renderIndicator = function renderIndicator() {
    // 需要判断是否存在indicator，并且需要是一个合法的react元素
    if (!(props !== null && props !== void 0 && props.indicator) || _typeof(props === null || props === void 0 ? void 0 : props.indicator) !== 'object') return null;
    return /*#__PURE__*/React.cloneElement(props === null || props === void 0 ? void 0 : props.indicator, {
      onChange: handleChange
    });
  };
  var renderNumberSelector = function renderNumberSelector() {
    if (!(props !== null && props !== void 0 && props.numberSelector) || _typeof(props === null || props === void 0 ? void 0 : props.numberSelector) !== 'object') return null;
    return /*#__PURE__*/React.cloneElement(props === null || props === void 0 ? void 0 : props.numberSelector, {
      onChange: function onChange(v) {
        var _props$actions;
        var _price = add_price !== undefined && add_price !== null ? add_price : price;
        // 价格正负号
        var priceType = price_type === 'markdown' ? -1 : 1;
        var totalPrice = new Decimal(_price).times(new Decimal(v)).times(priceType).toNumber();
        var newOriginal = _objectSpread(_objectSpread({}, _original), {}, {
          quantity: v,
          num: v,
          price: _price,
          original_price: _price,
          original_total: _price
        });
        props === null || props === void 0 || (_props$actions = props.actions) === null || _props$actions === void 0 || _props$actions.update(uniqueKey, {
          quantity: v,
          data: newOriginal,
          totalPrice: totalPrice
        });
      }
    });
  };

  // 判断是否存在数量选择器
  var isNumberSelector = useMemo(function () {
    return !!(props !== null && props !== void 0 && props.numberSelector) && /*#__PURE__*/React.isValidElement(props === null || props === void 0 ? void 0 : props.numberSelector);
  }, [props === null || props === void 0 ? void 0 : props.numberSelector]);

  // 价格文本
  var priceText = useMemo(function () {
    // add_price有值优先展示
    if (add_price !== undefined && add_price !== null) {
      return "".concat(Number(add_price) > 0 ? '+' : '').concat(symbol).concat(Math.abs(Number(add_price || 0)));
    }
    return "".concat(price_type === 'markdown' ? '-' : '').concat(symbol).concat(price || 0);
  }, [price, price_type, symbol, add_price]);

  // 处理选中状态改变
  var handleChange = function handleChange() {
    if (disabled || isNumberSelector) return;
    if (selected) {
      var _props$actions2;
      props === null || props === void 0 || (_props$actions2 = props.actions) === null || _props$actions2 === void 0 || _props$actions2.update(uniqueKey, {
        quantity: 0
      });
    } else {
      var _props$actions3;
      // 需要在_original上附加信息
      var _price = add_price !== undefined && add_price !== null ? add_price : price;
      var priceType = price_type === 'markdown' ? -1 : 1;
      var newOriginal = _objectSpread(_objectSpread({}, _original), {}, {
        quantity: 1,
        num: 1,
        price: _price,
        original_price: _price,
        original_total: _price
      });
      // 价格正负号
      props === null || props === void 0 || (_props$actions3 = props.actions) === null || _props$actions3 === void 0 || _props$actions3.update(uniqueKey, {
        quantity: 1,
        data: newOriginal,
        totalPrice: new Decimal(_price).times(priceType).toNumber()
      });
    }
  };
  return /*#__PURE__*/React.createElement(SKUCard, {
    cover: cover,
    title: title || name || '',
    subtitle: product_subtitle,
    price: priceText,
    selected: selected,
    disabled: disabled,
    renderRight: isNumberSelector ? /*#__PURE__*/React.createElement("div", {
      style: {
        position: 'relative',
        height: '100%'
      }
    }, renderNumberSelector()) : /*#__PURE__*/React.createElement("div", {
      style: {
        position: 'relative',
        height: '100%',
        padding: '0 8px'
      }
    }, /*#__PURE__*/React.createElement(React.Fragment, null, renderIndicator(), /*#__PURE__*/React.createElement("div", {
      style: {
        position: 'absolute',
        top: 0,
        left: 0,
        width: '100%',
        height: '100%'
      }
    }))),
    onClick: handleChange,
    skuCardConfig: skuCardConfig,
    skuDisabled: (props === null || props === void 0 || (_props$dataSource = props.dataSource) === null || _props$dataSource === void 0 || (_props$dataSource = _props$dataSource._extra) === null || _props$dataSource === void 0 ? void 0 : _props$dataSource.disabled) === true
  });
};
export default NormalCard;