import _regeneratorRuntime from "@babel/runtime/regenerator";

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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; }

/* eslint-disable */
import { MultipleBanners, ProductList } from '@whalecloud/ecommerce';
import { fetch } from 'fishx';
import React from 'react';
import { defaultImage } from '../../../assets/IconBase64';
import MagicBox from '../../../core/MagicBox';
import { formatCurrencyWithSymbol } from '../../../utils/currencyUtil';
import MBaseComponent from '../../MBaseComponent';
import './index.module.less';

var formatData = function formatData(list) {
  var res = [];
  list.map(function (item) {
    var data = {};

    if (item.productId) {
      data.name = item.productName;
      data.href = "/offer-detail/".concat(item.offerId);
      data.price = item.preSalesPrice;
      data.costn = item.preMarketPrice;
      data.imgSrc = item.productImageUrl;
    } else {
      data.name = item.offerName;
      data.href = "/offer-detail/".concat(item.offerId);
      data.price = formatCurrencyWithSymbol(item.salesPrice);
      data.costn = formatCurrencyWithSymbol(item.marketPrice);
      data.imgSrc = item.defImg;
    }

    res.push(data);
  });
  return res;
};

var MProductList = /*#__PURE__*/function (_MBaseComponent) {
  _inherits(MProductList, _MBaseComponent);

  var _super = _createSuper(MProductList);

  /**
   * @required
   * React 会根据 type 动态创建组件的实例。
   */
  function MProductList(props) {
    var _this;

    _classCallCheck(this, MProductList);

    _this = _super.call(this, props);
    _this.state = {
      dataList: []
    };
    return _this;
  }

  _createClass(MProductList, [{
    key: "getData",
    value: function () {
      var _getData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params, obj) {
        var data;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (!obj.url) {
                  _context.next = 5;
                  break;
                }

                _context.next = 3;
                return fetch("".concat(window.MParams.MBaseUrl, "/").concat(obj.url), {
                  method: 'GET',
                  params: params
                });

              case 3:
                data = _context.sent;

                if (data && data instanceof Array) {
                  this.setState({
                    dataList: data
                  });
                }

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

      function getData(_x, _x2) {
        return _getData.apply(this, arguments);
      }

      return getData;
    }()
  }, {
    key: "getDataList",
    value: function getDataList(mProps) {
      if (mProps.selection === 'auto') {
        this.getData({
          sceneCode: '1001',
          recommendType: '1',
          itemType: [],
          topN: '5',
          needFootprint: true
        }, mProps);
      } else {
        this.setState({
          dataList: mProps.productList || []
        });
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.getDataList(this.props);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if (this.props.url !== nextProps.url || this.props.selection !== nextProps.selection || JSON.stringify(nextProps.productList || []) !== JSON.stringify(this.props.productList || [])) {
        this.getDataList(nextProps);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var tempProps = _objectSpread({}, this.props, {
        onComponentClick: this.onComponentClick
      });

      var dataTest = [{
        key: '1',
        imgUrl: defaultImage
      }, {
        key: '2',
        imgUrl: defaultImage
      }, {
        key: '3',
        imgUrl: defaultImage
      }, {
        key: '4',
        imgUrl: defaultImage
      }, {
        key: '5',
        imgUrl: defaultImage
      }];
      return /*#__PURE__*/React.createElement(MagicBox, tempProps, /*#__PURE__*/React.createElement("div", {
        style: tempProps.mode !== 'designer' ? {
          width: '1200px',
          margin: 'auto'
        } : {}
      }, this.state.dataList && this.state.dataList.length > 0 ? /*#__PURE__*/React.createElement(ProductList, {
        clickItem: function clickItem(item) {
          return _this2.clickItem(item.href);
        },
        data: formatData(this.state.dataList),
        showWidth: "100%"
      }) : /*#__PURE__*/React.createElement(MultipleBanners, {
        data: dataTest,
        showNum: 5
      })));
    }
  }]);

  return MProductList;
}(MBaseComponent);

_defineProperty(MProductList, "type", 'MProductList');

export { MProductList as default };