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.push.apply(ownKeys, 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); Object.defineProperty(Constructor, "prototype", { writable: false }); 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 no-console */

/* eslint-disable no-unused-expressions */
import classnames from 'classnames';
import isEmpty from 'lodash/isEmpty';
import PropTypes from 'prop-types';
import React from 'react';
import Carousel from '../../../common/Carousel';
import MBaseComponent from '../../../composite-comp/dito/components/MBaseComponent'; // import { defaultImage } from '../../../assets/IconBase64';

import MagicBox from '../../../core/MagicBox';
import { ensure, fishFetch } from '../../../utils/commonUtil';
import { getComponentLocale } from '../../../utils/getLocale';
import { getMagicStore, setMagicStore } from '../../../utils/storeUtil';
import image1 from './images/image1.png';
import image2 from './images/image2.png';
import image3 from './images/image3.png';
import zeroSaleBgImg from './images/zeroSaleBgImg.png';
import './index.less';
import TimeClock from './TimeClock';
var demoData = {
  posterList: [{
    postImage: zeroSaleBgImg,
    href: null,
    imageList: [image1, image2, image3]
  }]
}; // const posterList = [{}, {}, {}, {}];

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

  var _super = _createSuper(MZeroSale);

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

    _classCallCheck(this, MZeroSale);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "state", {
      imageIndex: 0,
      ready: false
    });

    _defineProperty(_assertThisInitialized(_this), "imageTimer", null);

    _defineProperty(_assertThisInitialized(_this), "posterList", []);

    _defineProperty(_assertThisInitialized(_this), "timerClockRef", React.createRef());

    _defineProperty(_assertThisInitialized(_this), "getInit", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var campaignList;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return fishFetch("".concat(window.MParams.MBaseUrl, "/h5/winner/specials/list"));

            case 2:
              campaignList = _context.sent;

              /** 查找有效的活动 */
              _this.posterList = ensure(function () {
                return _this.props.posterList.filter(function (p) {
                  var findOne = campaignList.find(function (c) {
                    return c.campaignId === p.campaignId && c.cmpState === 'B';
                  });
                  p.campaign = findOne;
                  return findOne;
                });
              }, []);

              if (_this.props.mode === 'renderer') {
                setMagicStore("zeroSale_".concat(_this.props.id), _this.posterList);
              }

              _this.setState({
                ready: true
              });
              /** 中间图片轮播 */


              _this.imageTimer = setInterval(function () {
                _this.setState({
                  imageIndex: _this.state.imageIndex + 1
                });
              }, 2000);

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

    _defineProperty(_assertThisInitialized(_this), "calcLastTime", function () {
      _this.timerClockRef.current.calcLastTime();
    });

    if (props.mode === 'renderer') {
      var cache = getMagicStore("zeroSale_".concat(_this.props.id));

      if (cache) {
        _this.posterList = cache;
        _this.state.ready = true;
      }
    }

    return _this;
  }

  _createClass(MZeroSale, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.getInit();
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps() {
      this.getInit();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.imageTimer && clearInterval(this.imageTimer);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

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

      var _posterList = isEmpty(this.posterList || []) ? demoData.posterList : this.posterList;

      if (this.props.mode === 'renderer') {
        _posterList = this.posterList || [];
      }

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

      var posterList = tempProps.posterList;
      var _this$state = this.state,
          ready = _this$state.ready,
          imageIndex = _this$state.imageIndex;
      if (!ready) return null;
      return /*#__PURE__*/React.createElement(MagicBox, tempProps, /*#__PURE__*/React.createElement("div", {
        className: "zero-sale-wrap"
      }, /*#__PURE__*/React.createElement(Carousel, {
        autoplay: true,
        infinite: true,
        autoplayInterval: 5000,
        dots: posterList.length > 1
      }, posterList.map(function (poster, i) {
        return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
          key: i,
          className: "poster-wrap",
          onClick: function onClick() {
            if (poster.campaignId) {
              _this2.clickItem("specialMission-".concat(poster.campaignId));
            }
          },
          style: {
            backgroundImage: "url(".concat(poster.postImage, ")")
          }
        }, /*#__PURE__*/React.createElement("div", {
          className: "poster-timer"
        }, /*#__PURE__*/React.createElement(TimeClock, {
          locale: locale,
          ref: _this2.timerClockRef,
          data: poster.campaign
        })), /*#__PURE__*/React.createElement("div", {
          className: "image-wrap"
        }, /*#__PURE__*/React.createElement("div", {
          className: "item-wrap"
        }, poster.imageList.map(function (img, index) {
          return /*#__PURE__*/React.createElement("div", {
            key: index,
            className: classnames('wrap-item', {
              active: imageIndex % poster.imageList.length === index
            })
          }, /*#__PURE__*/React.createElement("img", {
            src: img,
            alt: ""
          }));
        })))));
      }))));
    }
  }]);

  return MZeroSale;
}(MBaseComponent);

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

_defineProperty(MZeroSale, "contextTypes", {
  antLocale: PropTypes.object
});

export { MZeroSale as default };