"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _fishx = require("fishx");

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireDefault(require("react"));

var _currencyUtil = require("../../../../utils/currencyUtil");

var _getLocale = require("../../../../utils/getLocale");

var _avatarList = _interopRequireDefault(require("../common/avatar-list"));

var _NewImg = _interopRequireDefault(require("../common/NewImg"));

var _NewPrice = _interopRequireDefault(require("../common/NewPrice"));

var _RatingText = _interopRequireDefault(require("../common/RatingText"));

require("./index.less");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var GroupGoods = /*#__PURE__*/function (_React$Component) {
  _inherits(GroupGoods, _React$Component);

  var _super = _createSuper(GroupGoods);

  function GroupGoods(props) {
    var _this;

    _classCallCheck(this, GroupGoods);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "isAccessToken", function () {
      var accessToken = localStorage.getItem('h5Token');
      return accessToken ? {
        headers: {
          accessToken: localStorage.getItem('h5Token')
        }
      } : {};
    });

    _defineProperty(_assertThisInitialized(_this), "formatData", function (data) {
      var newData = {};

      if (data) {
        newData.name = data.productName;
        newData.rateValue = data.evaluationRate;
        newData.rateTotal = data.evaluationCnt;
        newData.price = (0, _currencyUtil.formatCurrency)(data.shareBuyPrice);
        newData.originalPrice = (0, _currencyUtil.formatCurrency)(data.marketPrice);
        newData.Sold = data.salesCnt;
        newData.total = data.shareByNum || 0;
        newData.list = data.groups && data.groups.length >= 0 && data.groups.map(function (v) {
          v.img = v.photo;
          return v;
        });
        newData.url = "/product/detail/".concat(data.offerId, "?productId=").concat(data.productId);
      }

      return newData;
    });

    _this.state = {
      nGoods: {}
    };
    return _this;
  }

  _createClass(GroupGoods, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          productId = _this$props.productId,
          data = _this$props.data;

      if (productId) {
        this.getGroupInfo(productId);
      } else {
        this.setState({
          nGoods: data.goods
        });
      }
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      var productId = props.productId,
          data = props.data;

      if (productId !== this.props.productId) {
        if (productId) {
          this.getGroupInfo(productId);
        } else {
          this.setState({
            nGoods: data.goods
          });
        }
      }
    } // const [nGoods, setGoods] = useState(null);

  }, {
    key: "getGroupInfo",
    value: function () {
      var _getGroupInfo = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee(productId) {
        var data;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return (0, _fishx.fetch)("/h5/sharebuy/product/homeview?productIds=".concat(productId), this.isAccessToken());

              case 2:
                data = _context.sent;
                this.setState({
                  nGoods: this.formatData(data[0])
                });

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function getGroupInfo(_x) {
        return _getGroupInfo.apply(this, arguments);
      }

      return getGroupInfo;
    }()
  }, {
    key: "render",
    value: // useEffect(() => {
    //   if (offerId) {
    //     getGroupInfo(offerId);
    //   } else {
    //     setGoods(goods);
    //   }
    // }, [offerId]);
    function render() {
      var _this$props2 = this.props,
          backgroundImage = _this$props2.data.backgroundImage,
          onClickItem = _this$props2.onClickItem;
      var nGoods = this.state.nGoods;

      var _locale = (0, _getLocale.getComponentLocale)(this.props, this.context, 'GroupGoods', function () {
        return require('./locale/en_US');
      });

      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, !(0, _isEmpty["default"])(nGoods) && /*#__PURE__*/_react["default"].createElement("div", {
        style: {
          padding: '0 16px'
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "new-group-goods",
        onClick: function onClick() {
          onClickItem && onClickItem(nGoods.url);
        }
      }, /*#__PURE__*/_react["default"].createElement(_NewImg["default"], {
        className: "group-coverImg",
        src: backgroundImage,
        lazy: true
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "group-info"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "title"
      }, nGoods.name), /*#__PURE__*/_react["default"].createElement("div", {
        className: "price-box"
      }, /*#__PURE__*/_react["default"].createElement(_NewPrice["default"], {
        style: {
          fontWeight: '600'
        },
        color: "#FF1659",
        price: nGoods.price,
        originalPrice: nGoods.originalPrice,
        showOriginalPrice: true
      }), /*#__PURE__*/_react["default"].createElement(_avatarList["default"], {
        list: nGoods.list,
        num: 3,
        showAdd: true
      })), /*#__PURE__*/_react["default"].createElement("div", {
        className: "btn-box"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "left"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "rate-box"
      }, /*#__PURE__*/_react["default"].createElement(_RatingText["default"], {
        text: (0, _currencyUtil.numberToFixed)(nGoods.rateValue || 5, 1)
      }), /*#__PURE__*/_react["default"].createElement("span", {
        className: "rate-number"
      }, "(", nGoods.rateTotal || 0, ")")), /*#__PURE__*/_react["default"].createElement("div", {
        className: "number"
      }, nGoods.total)), /*#__PURE__*/_react["default"].createElement("div", {
        className: "right"
      }, _locale.SlashNow))))));
    }
  }]);

  return GroupGoods;
}(_react["default"].Component);

exports["default"] = GroupGoods;

_defineProperty(GroupGoods, "contextTypes", {
  antLocale: _propTypes["default"].object
});