"use strict";

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

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

var _antd = require("antd");

var _macy = _interopRequireDefault(require("macy"));

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

var _ProductItem = _interopRequireDefault(require("../../../../common/components/ProductItem"));

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

var _commonUtil = require("../../../../../utils/commonUtil");

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 __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }

  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }

    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }

    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }

    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};

var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
  var _ = {
    label: 0,
    sent: function sent() {
      if (t[0] & 1) throw t[1];
      return t[1];
    },
    trys: [],
    ops: []
  },
      f,
      y,
      t,
      g;
  return g = {
    next: verb(0),
    "throw": verb(1),
    "return": verb(2)
  }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
    return this;
  }), g;

  function verb(n) {
    return function (v) {
      return step([n, v]);
    };
  }

  function step(op) {
    if (f) throw new TypeError("Generator is already executing.");

    while (_) {
      try {
        if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
        if (y = 0, t) op = [op[0] & 2, t.value];

        switch (op[0]) {
          case 0:
          case 1:
            t = op;
            break;

          case 4:
            _.label++;
            return {
              value: op[1],
              done: false
            };

          case 5:
            _.label++;
            y = op[1];
            op = [0];
            continue;

          case 7:
            op = _.ops.pop();

            _.trys.pop();

            continue;

          default:
            if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
              _ = 0;
              continue;
            }

            if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
              _.label = op[1];
              break;
            }

            if (op[0] === 6 && _.label < t[1]) {
              _.label = t[1];
              t = op;
              break;
            }

            if (t && _.label < t[2]) {
              _.label = t[2];

              _.ops.push(op);

              break;
            }

            if (t[2]) _.ops.pop();

            _.trys.pop();

            continue;
        }

        op = body.call(thisArg, _);
      } catch (e) {
        op = [6, e];
        y = 0;
      } finally {
        f = t = 0;
      }
    }

    if (op[0] & 5) throw op[1];
    return {
      value: op[0] ? op[1] : void 0,
      done: true
    };
  }
};

var __spreadArray = void 0 && (void 0).__spreadArray || function (to, from, pack) {
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
    if (ar || !(i in from)) {
      if (!ar) ar = Array.prototype.slice.call(from, 0, i);
      ar[i] = from[i];
    }
  }
  return to.concat(ar || Array.prototype.slice.call(from));
};

var TabPane = _antd.Tabs.TabPane;

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

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

    _this.state = {
      tabIndex: 0
    };

    _this.getTabBarActiveColor = function (_panelProps) {
      var tabIndex = _this.state.tabIndex;
      return (0, _commonUtil.ensure)(function () {
        return _panelProps.groupSource[tabIndex].hover.groupName.content.color;
      }, '#1890ff');
    };

    _this.getProductList = function (ids, index) {
      return __awaiter(_this, void 0, void 0, function () {
        var newData, res;

        var _a;

        var _this = this;

        return __generator(this, function (_b) {
          switch (_b.label) {
            case 0:
              newData = [];
              return [4
              /*yield*/
              , (0, _commonUtil.commonFetch)("".concat(window.MParams.MBaseUrl, "/h5/products"), {
                products: ids
              }, 'GET')];

            case 1:
              res = _b.sent();
              newData = __spreadArray([], res.list.filter(function (f) {
                return f.state === 'A';
              }), true);
              this.setState((_a = {}, _a["products".concat(index)] = newData, _a), function () {
                _this.getMacy(".tab-item-".concat(index));
              });
              return [2
              /*return*/
              ];
          }
        });
      });
    };

    _this.getMacy = function (container) {
      var masonryNew = new _macy["default"]({
        container: container,
        trueOrder: false,
        waitForImages: false,
        useOwnImageLoader: false,
        margin: {
          x: 12,
          y: 12
        },
        columns: 2 // 设置列数

      });
      masonryNew.runOnImageLoad(function () {
        masonryNew.recalculate(true);
      }, true);
    };

    _this.onTabChange = function (tabIndex) {
      var _a;

      var panelProps = _this.props.panelProps;
      if (tabIndex === _this.state.tabIndex) return;

      _this.setState((_a = {
        tabIndex: tabIndex
      }, _a["products".concat(tabIndex)] = [], _a));

      var curTab = panelProps.groupSource[tabIndex];
      if (curTab.prodIds === '') return;

      _this.getProductList(curTab.prodIds, tabIndex);
    };

    return _this;
  }

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

    var tabIndex = this.state.tabIndex;
    document.querySelector('.ant-tabs-ink-bar').style.background = this.getTabBarActiveColor(this.props.panelProps);
    var prodIds = (0, _commonUtil.ensure)(function () {
      return _this.props.panelProps.groupSource[tabIndex].prodIds;
    }, '');
    this.getProductList(prodIds, tabIndex);
  };

  RecommendMobile.prototype.componentWillReceiveProps = function (nextProps) {
    var tabIndex = this.state.tabIndex;
    document.querySelector('.ant-tabs-ink-bar').style.background = this.getTabBarActiveColor(nextProps.panelProps);
    var prodIds = (0, _commonUtil.ensure)(function () {
      return nextProps.panelProps.groupSource[tabIndex].prodIds;
    }, '');
    this.getProductList(prodIds, tabIndex);
  };

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

    var panelProps = this.props.panelProps;
    var tabIndex = this.state.tabIndex;

    var _panelProps = JSON.parse(JSON.stringify(panelProps));

    var bgStyle = {};

    if (_panelProps.background.bgType === 'color') {
      bgStyle = {
        backgroundColor: _panelProps.background.color,
        opacity: _panelProps.background.opacity / 100
      };
    } else {
      bgStyle = {
        background: "url(".concat(_panelProps.background.image, ") no-repeat center"),
        backgroundSize: '100%'
      };
    }

    return /*#__PURE__*/_react["default"].createElement("div", {
      className: _indexModule["default"].recommendWrap,
      style: bgStyle
    }, /*#__PURE__*/_react["default"].createElement(_antd.Tabs, {
      onChange: this.onTabChange,
      tabBarGutter: 0,
      tabBarStyle: {
        background: '#fff'
      }
    }, _panelProps.groupSource.map(function (tab, index) {
      var _products = _this.state["products".concat(index)] || [];

      var _image = index == tabIndex ? tab.hover.image : tab.image;

      var _groupName = index == tabIndex ? tab.hover.groupName : tab.groupName;

      var _tab = /*#__PURE__*/_react["default"].createElement("div", {
        className: "tabTitle"
      }, /*#__PURE__*/_react["default"].createElement(_components.MetaImage, {
        data: _image
      }), /*#__PURE__*/_react["default"].createElement(_components.MetaText, {
        data: _groupName
      }));

      return /*#__PURE__*/_react["default"].createElement(TabPane, {
        key: index,
        tab: _tab
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "tab-item-".concat(index)
      }, _products.map(function (item) {
        return /*#__PURE__*/_react["default"].createElement(_ProductItem["default"], {
          key: item.productId,
          data: item,
          onItemClick: function onItemClick() {
            return onClick("/product/detail/".concat(item.offerId, "?productId=").concat(item.sppProductId));
          },
          panelProps: _panelProps.content,
          showProgress: false
        });
      })));
    })));
  };

  RecommendMobile.defaultProps = {
    panelProps: {}
  };
  return RecommendMobile;
}(_react["default"].Component);

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