"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");

var _indexModule = _interopRequireDefault(require("./index.module.less"));

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

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

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

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

var _common = require("../../../../../common");

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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");

    _extendStatics(d, b);

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

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

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

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

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

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

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

    _this.mouseEnterBtn = function (e) {
      var _a, _b;

      var data = _this.props.data;

      if ((_a = data.setting.navigation) === null || _a === void 0 ? void 0 : _a.hoverColor) {
        e.target.style.color = data.setting.navigation.hoverColor;
      }

      if ((_b = data.setting.navigation) === null || _b === void 0 ? void 0 : _b.hoverSize) {
        e.target.style.fontSize = data.setting.navigation.hoverSize;
      }
    };

    _this.mouseLeaveBtn = function (e) {
      var _a, _b;

      var data = _this.props.data;

      if ((_a = data.setting.navigation) === null || _a === void 0 ? void 0 : _a.color) {
        e.target.style.color = data.setting.navigation.color;
      }

      if ((_b = data.setting.navigation) === null || _b === void 0 ? void 0 : _b.size) {
        e.target.style.fontSize = data.setting.navigation.size;
      }
    };

    _this.getBgStyle = function (item) {
      var data = _this.props.data;
      var bgStyle = {
        justifyContent: _AlignSelector.alignItemMap[item.customize.layout]
      };

      if (item.background.bgType === 'color') {
        bgStyle.opacity = item.background.opacity ? item.background.opacity / 100 : 1;
        bgStyle.backgroundColor = item.background.color;
      } else {
        bgStyle.backgroundImage = "url(".concat(item.background.image, ")");
      }

      if (data.setting.pcHeight) bgStyle.height = data.setting.pcHeight;
      return bgStyle;
    };

    return _this;
  }

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

    this.distroy = (0, _mobx.autorun)(function () {
      _this.setState({
        clickedGroup: _mobx.store.clickedGroup
      });
    });
  };

  BannerPc.prototype.componentWillReceiveProps = function (nextProps) {
    var _this = this;

    var nextIndex = nextProps.data.groupSource.findIndex(function (item) {
      return item.id === _this.state.clickedGroup;
    });

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

  BannerPc.prototype.componentWillUnmount = function () {
    this.distroy();
  };

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

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

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

    return /*#__PURE__*/_react["default"].createElement("div", {
      className: _indexModule["default"].container
    }, data.setting.navigation.open && /*#__PURE__*/_react["default"].createElement(_common.Iconfont, {
      className: (0, _classnames["default"])(_indexModule["default"].turnPage, _indexModule["default"].leftBtn),
      onClick: function onClick() {
        _this.carouselRef.current.prev();
      },
      onMouseEnter: this.mouseEnterBtn,
      onMouseLeave: this.mouseLeaveBtn,
      style: navStyle,
      type: "icon-outlined-left"
    }), /*#__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: _indexModule["default"].carouselItem,
        onMouseEnter: _this.mouseEnterWrap,
        onMouseLeave: _this.mouseLeaveWrap,
        style: _this.getBgStyle(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: '1px',
          height: '100%'
        }
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: _indexModule["default"].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
        }
      });
    })), data.setting.navigation.open && /*#__PURE__*/_react["default"].createElement(_common.Iconfont, {
      className: (0, _classnames["default"])(_indexModule["default"].turnPage, _indexModule["default"].rightBtn),
      onClick: function onClick() {
        _this.carouselRef.current.next();
      },
      onMouseEnter: this.mouseEnterBtn,
      onMouseLeave: this.mouseLeaveBtn,
      style: navStyle,
      type: "icon-outlined-right"
    }));
  };

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

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