Liferay.Loader.define("commerce-frontend-js@3.0.33/components/gallery/Gallery", ['module', 'exports', 'require', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../utilities/eventsDefinitions', './MainImage', './Overlay', './Thumbnails'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

    var _react = _interopRequireDefault(require("frontend-js-react-web$react"));

    var _eventsDefinitions = require("../../utilities/eventsDefinitions");

    var _MainImage = _interopRequireDefault(require("./MainImage"));

    var _Overlay = _interopRequireDefault(require("./Overlay"));

    var _Thumbnails = _interopRequireDefault(require("./Thumbnails"));

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

    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    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 _possibleConstructorReturn(self, call) {
      if (call && (_typeof(call) === "object" || typeof call === "function")) {
        return call;
      }return _assertThisInitialized(self);
    }

    function _getPrototypeOf(o) {
      _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
      };return _getPrototypeOf(o);
    }

    function _assertThisInitialized(self) {
      if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
      }return self;
    }

    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 fetchImage(url) {
      return new Promise(function (resolve) {
        var img = new Image();
        img.src = url;

        img.onload = function () {
          return resolve(url);
        };
      });
    }

    var Gallery = /*#__PURE__*/function (_React$Component) {
      _inherits(Gallery, _React$Component);

      function Gallery(props) {
        var _this;

        _classCallCheck(this, Gallery);

        _this = _possibleConstructorReturn(this, _getPrototypeOf(Gallery).call(this, props));
        _this.state = {
          fullscreen: false,
          images: _this.props.images,
          loaded: new Set(),
          loading: false,
          selected: 0
        };
        _this.fullscreenClose = _this.fullscreenClose.bind(_assertThisInitialized(_this));
        _this.fullscreenOpen = _this.fullscreenOpen.bind(_assertThisInitialized(_this));
        _this.goTo = _this.goTo.bind(_assertThisInitialized(_this));
        _this.goToNext = _this.goToNext.bind(_assertThisInitialized(_this));
        _this.goToPrev = _this.goToPrev.bind(_assertThisInitialized(_this));
        _this.imageLoad = _this.imageLoad.bind(_assertThisInitialized(_this));
        _this.imageSelect = _this.imageSelect.bind(_assertThisInitialized(_this));
        _this._handleImagesUpdate = _this._handleImagesUpdate.bind(_assertThisInitialized(_this));
        return _this;
      }

      _createClass(Gallery, [{
        key: "componentDidMount",
        value: function componentDidMount() {
          Liferay.on(_eventsDefinitions.PRODUCT_OPTIONS_CHANGED, this._handleImagesUpdate, this);
        }
      }, {
        key: "componentWillUnmount",
        value: function componentWillUnmount() {
          Liferay.detach(_eventsDefinitions.PRODUCT_OPTIONS_CHANGED, this._handleImagesUpdate, this);
        }
      }, {
        key: "_handleImagesUpdate",
        value: function _handleImagesUpdate(e) {
          if (e.images) {
            this.setState({
              images: e.images,
              selected: 0
            });
          }
        }
      }, {
        key: "fullscreenOpen",
        value: function fullscreenOpen() {
          var _this2 = this;

          if (!this.state.loading) {
            this.imageLoad(this.state.images[this.state.selected].url).then(function () {
              _this2.setState({
                fullscreen: true
              });
            });
          }
        }
      }, {
        key: "fullscreenClose",
        value: function fullscreenClose() {
          this.setState({
            fullscreen: false
          });
        }
      }, {
        key: "goTo",
        value: function goTo(pos) {
          this.imageSelect((this.state.images.length + pos) % this.state.images.length);
        }
      }, {
        key: "goToPrev",
        value: function goToPrev(e) {
          e.stopPropagation();
          this.goTo(this.state.selected - 1);
        }
      }, {
        key: "goToNext",
        value: function goToNext(e) {
          e.stopPropagation();
          this.goTo(this.state.selected + 1);
        }
      }, {
        key: "imageLoad",
        value: function imageLoad(imageUrl) {
          var _this3 = this;

          return new Promise(function (resolve) {
            if (_this3.state.loaded.has(imageUrl)) {
              resolve(imageUrl);
            } else {
              _this3.setState({
                loading: true
              });

              fetchImage(imageUrl).then(function () {
                _this3.setState({
                  loaded: new Set(_this3.state.loaded).add(imageUrl),
                  loading: false
                }, function () {
                  resolve(imageUrl);
                });
              });
            }
          });
        }
      }, {
        key: "imageSelect",
        value: function imageSelect(toSelect) {
          var _this4 = this;

          if (toSelect !== this.state.selected && !this.state.loading) {
            this.imageLoad(this.state.images[toSelect].url).then(function () {
              _this4.setState({
                selected: toSelect
              });
            });
          }
        }
      }, {
        key: "render",
        value: function render() {
          var background = this.props.background;
          var _this$state = this.state,
              fullscreen = _this$state.fullscreen,
              images = _this$state.images,
              loading = _this$state.loading,
              selected = _this$state.selected;
          return (/*#__PURE__*/_react.default.createElement("div", {
              className: "product-gallery"
            }, images && images.length > 0 && /*#__PURE__*/_react.default.createElement(_MainImage.default, {
              background: background,
              loading: loading,
              onNext: images.length > 1 ? this.goToNext : null,
              onPrev: images.length > 1 ? this.goToPrev : null,
              onZoom: this.fullscreenOpen,
              src: images[selected].url,
              title: images[selected].title
            }), images.length > 1 ? /*#__PURE__*/_react.default.createElement(_Thumbnails.default, {
              background: background,
              images: images,
              onChange: this.imageSelect,
              selected: selected
            }) : null, fullscreen ? /*#__PURE__*/_react.default.createElement(_Overlay.default, {
              background: background,
              onClose: this.fullscreenClose,
              onNext: images.length > 1 ? this.goToNext : null,
              onPrev: images.length > 1 ? this.goToPrev : null,
              src: images[selected].url,
              title: images[selected].title
            }) : null)
          );
        }
      }]);

      return Gallery;
    }(_react.default.Component);

    exports.default = Gallery;
    //# sourceMappingURL=Gallery.js.map
  }
});
//# sourceMappingURL=Gallery.js.map