"use strict";

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _antd = require("antd");

require("./index.less");

var _mobx = require("../../../../../mobx");

var _coreUtil = require("../../../../../utils/coreUtil");

var _AlignSelector = require("../../../../../common/AlignSelector");

var _components = require("../../../../../meta-comp/components");

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

var __extends = void 0 && (void 0).__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (b.hasOwnProperty(p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var BannerMobile =
/** @class */
function (_super) {
  __extends(BannerMobile, _super);

  function BannerMobile() {
    var _this = _super !== null && _super.apply(this, arguments) || this;

    _this.state = {
      carouseIndex: 0,
      hoverState: false,
      clickedGroup: ''
    };
    _this.carouselRef = /*#__PURE__*/_react["default"].createRef();
    _this.destroy = null;

    _this.bannerGoto = function (groupSource) {
      var nextIndex = groupSource.findIndex(function (item) {
        return item.id === _mobx.store.clickedGroup;
      });

      if (nextIndex != -1 && nextIndex != _this.state.carouseIndex) {
        _this.carouselRef.current.goTo(nextIndex);
      }
    };

    _this.getItemStyle = function (item) {
      var data = _this.props.data;
      return {
        justifyContent: _AlignSelector.alignItemMap[item.customize.layout],
        height: data.setting.mobileHeight
      };
    };

    _this.mouseEnterWrap = function () {
      _this.setState({
        hoverState: true
      });
    };

    _this.mouseLeaveWrap = function () {
      _this.setState({
        hoverState: false
      });
    };

    return _this;
  }

  BannerMobile.prototype.componentDidMount = function () {
    var _this = this;

    this.destroy = (0, _mobx.autorun)(function () {
      _this.bannerGoto(_this.props.data.groupSource);
    });
  };

  BannerMobile.prototype.componentWillReceiveProps = function (nextProps) {
    this.bannerGoto(nextProps.data.groupSource);
  };

  BannerMobile.prototype.componentWillUnmount = function () {
    this.destroy();
  };

  BannerMobile.prototype.render = function () {
    var _this = this;

    var data = this.props.data;
    var _a = this.state,
        carouseIndex = _a.carouseIndex,
        hoverState = _a.hoverState,
        clickedGroup = _a.clickedGroup;
    var autoplay = data.setting.autoplay.open;
    /** 设计器模式下 如果选中特定group 自动播放关闭 */

    if ((0, _coreUtil.isDesignMode)() && clickedGroup) {
      autoplay = false;
    }

    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "m-banner-mobile"
    }, /*#__PURE__*/_react["default"].createElement(_antd.Carousel, {
      ref: this.carouselRef,
      afterChange: function afterChange(n) {
        return _this.setState({
          carouseIndex: n
        });
      },
      autoplay: autoplay,
      autoplaySpeed: data.setting.autoplay.interval * 1000,
      dots: false
    }, data.groupSource.map(function (item, index) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        key: item.id
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "carouselItem",
        onMouseEnter: _this.mouseEnterWrap,
        onMouseLeave: _this.mouseLeaveWrap,
        style: _this.getItemStyle(item)
      }, item.image.open && /*#__PURE__*/_react["default"].createElement(_components.MetaImage, {
        data: item.image,
        hoverState: hoverState && carouseIndex === index,
        isBackground: true,
        style: {
          position: 'absolute',
          ZIndex: 1,
          inset: '0px',
          height: '100%'
        }
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "carouseContent",
        style: {
          alignItems: _AlignSelector.alignItemMap[item.customize.align]
        }
      }, item.title.open && /*#__PURE__*/_react["default"].createElement(_components.MetaText, {
        className: "title",
        data: item.title
      }), item.text.open && /*#__PURE__*/_react["default"].createElement(_components.MetaText, {
        className: "sub-title",
        data: item.text
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "btn-wrap"
      }, item.button.open && /*#__PURE__*/_react["default"].createElement(_components.MetaButton, {
        className: "btn",
        data: item.button
      }), item.secondButton.open && /*#__PURE__*/_react["default"].createElement(_components.MetaButton, {
        className: "btn",
        data: item.secondButton
      })))));
    })), data.setting.dots.open && /*#__PURE__*/_react["default"].createElement("div", {
      className: "swiper-ban"
    }, data.groupSource.length > 1 && data.groupSource.map(function (item, i) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])('ban', {
          active: i == carouseIndex
        }),
        onClick: function onClick() {
          var _a;

          return (_a = _this.carouselRef.current) === null || _a === void 0 ? void 0 : _a.goTo(i);
        },
        style: {
          backgroundColor: i == carouseIndex ? data.setting.dots.hoverColor : data.setting.dots.color,
          fontSize: i == carouseIndex ? data.setting.dots.hoverSize : data.setting.dots.size
        }
      });
    })));
  };

  BannerMobile.defaultProps = {
    data: {
      setting: {},
      groupSource: []
    }
  };
  return BannerMobile;
}(_react["default"].Component);

var _default = BannerMobile;
exports["default"] = _default;