"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.styles = exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactCustomScrollbars = require("react-custom-scrollbars");
var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
var _get = _interopRequireDefault(require("lodash/get"));
var _styles = require("@material-ui/core/styles");
var _Icon = _interopRequireDefault(require("@material-ui/core/Icon"));
var _ResizeDetect = require("../../../../../Shared/HOCs/ResizeDetect/ResizeDetect");
var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));
var _withWidth = _interopRequireWildcard(require("@material-ui/core/withWidth"));
var _AccessibleButton = _interopRequireDefault(require("../../../../Shared/Components/AccessibleButton/AccessibleButton"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var styles = exports.styles = function styles(theme) {
  var _thumbnailCarouselWra, _dots, _wrapContainer;
  return {
    thumbnailCarouselWrapper: (_thumbnailCarouselWra = {
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
      paddingBottom: 16
    }, _defineProperty(_thumbnailCarouselWra, theme.breakpoints.down('xs'), {
      paddingBottom: '0'
    }), _defineProperty(_thumbnailCarouselWra, theme.breakpoints.between('sm', 'md'), {
      maxWidth: 300,
      margin: '0 auto'
    }), _thumbnailCarouselWra),
    fullSizeCarouselWrapper: {
      width: '100%',
      minWidth: 90,
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center'
    },
    arrowScrollIcon: {
      '&:hover': {
        cursor: 'pointer',
        fontWeight: 'bold'
      }
    },
    designerThumbnail: {
      width: '100%',
      display: 'flex',
      alignItems: 'center',
      '& img': {
        boxSizing: 'border-box',
        height: 70,
        maxWidth: 70,
        cursor: 'pointer',
        objectFit: 'cover',
        '&:hover': {
          cursor: 'pointer'
        }
      }
    },
    activeThumbnail: {
      border: '2px solid ' + theme.palette.primary.main,
      borderRadius: '4px'
    },
    inactiveThumbnail: {
      border: '1px solid #C4C4C4',
      borderRadius: '4px'
    },
    dots: (_dots = {
      position: 'inherit',
      textAlign: 'center',
      display: 'flex',
      justifyContent: 'center',
      maxHeight: 70,
      minHeight: 70,
      flexWrap: 'wrap',
      overflow: 'auto'
    }, _defineProperty(_dots, theme.breakpoints.down('sm'), {
      textAlign: 'initial'
    }), _defineProperty(_dots, theme.breakpoints.down('xs'), {
      maxHeight: 50,
      minHeight: 50,
      bottom: '0',
      textAlign: 'center',
      alignItems: 'center'
    }), _dots),
    thumbnailImageContainer: {
      height: 75,
      width: 75,
      paddingRight: 6,
      boxSizing: 'border-box',
      borderRadius: '4px'
    },
    dotContainer: {
      height: 40,
      width: 40,
      marginRight: '8px',
      '&.wrap-container': (_wrapContainer = {
        marginRight: 0,
        height: 32,
        width: 32,
        marginTop: 8
      }, _defineProperty(_wrapContainer, theme.breakpoints.down('sm'), {
        height: 20,
        width: 28,
        marginTop: 5
      }), _defineProperty(_wrapContainer, '& > div', {
        margin: 0
      }), _wrapContainer),
      '& > div': {
        textIndent: 1,
        color: '#FFFFFF',
        fontSize: '12px',
        lineHeight: '20px',
        textAlign: 'center',
        margin: '10px',
        cursor: 'pointer',
        height: 20,
        width: 20,
        borderRadius: '50%',
        display: 'inline-block',
        transition: 'background-color 0.6s ease'
      },
      '&:last-child': {
        marginRight: 0
      }
    },
    inactiveDotMode: {
      backgroundColor: '#D8D8D8'
    },
    activeDotMode: {
      backgroundColor: '#757575'
    }
  };
};
var Thumbnail = /*#__PURE__*/function (_React$Component) {
  _inherits(Thumbnail, _React$Component);
  var _super = _createSuper(Thumbnail);
  function Thumbnail() {
    var _this;
    _classCallCheck(this, Thumbnail);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this), "state", {
      isScrollingAvailable: true,
      availableWidth: 0
    });
    _defineProperty(_assertThisInitialized(_this), "scrollbars", /*#__PURE__*/_react["default"].createRef());
    _defineProperty(_assertThisInitialized(_this), "thumbnailCarouselWrapperRef", /*#__PURE__*/_react["default"].createRef());
    _defineProperty(_assertThisInitialized(_this), "onThumbnailClick", function (thumbnails, thumbnail) {
      return function () {
        if ((0, _isFunction["default"])(_this.props.thumbnailChange)) {
          _this.props.thumbnailChange(thumbnails, thumbnail.thumbnailImage, !thumbnail.active);
        }
      };
    });
    _defineProperty(_assertThisInitialized(_this), "updateScrollingAvailable", function () {
      if (_this.scrollbars && _this.scrollbars.current !== null) {
        var _this$scrollbars$curr = _this.scrollbars.current.getValues(),
          scrollWidth = _this$scrollbars$curr.scrollWidth,
          clientWidth = _this$scrollbars$curr.clientWidth;
        var isScrollingAvailable = _this.state.isScrollingAvailable;
        var nextIsScrollingAvailable = scrollWidth - clientWidth > 0;
        if (nextIsScrollingAvailable !== isScrollingAvailable) {
          _this.setState({
            isScrollingAvailable: nextIsScrollingAvailable
          });
        }
      }
    });
    _defineProperty(_assertThisInitialized(_this), "onScrollStop", function () {
      if (_this.scrollbars && _this.scrollbars.current !== null) {
        var scrollLeftValue = _this.scrollbars.current.getScrollLeft();
        var scrollWidth = _this.scrollbars.current.getScrollWidth();
        var childWidth = (0, _get["default"])(_this.scrollbars.current, 'view.children[0].children[0].clientWidth');
        var scrolledElements = Math.round(scrollLeftValue / childWidth);
        var scrollToValue = Math.ceil(scrolledElements * childWidth);
        if (scrollWidth - scrollLeftValue <= childWidth) {
          _this.scrollbars.current.scrollLeft(scrollWidth);
        } else {
          _this.scrollbars.current.scrollLeft(scrollToValue);
        }
      }
    });
    return _this;
  }
  _createClass(Thumbnail, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.updateScrollingAvailable();
      this.calculateAvailableWidth();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      this.updateScrollingAvailable();
      var size = this.props.size;
      if (size !== prevProps.size) {
        this.calculateAvailableWidth();
      }
    }
  }, {
    key: "calculateAvailableWidth",
    value: function calculateAvailableWidth() {
      if (this.scrollbars && this.scrollbars.current !== null) {
        var childWidth = (0, _get["default"])(this.scrollbars.current, 'view.children[0].children[0].clientWidth');
        var availableWidth = this.thumbnailCarouselWrapperRef.current.clientWidth - 40;
        var containerElements = Math.floor(availableWidth / childWidth);
        this.setState({
          availableWidth: Math.ceil(containerElements * childWidth)
        });
      }
    }
  }, {
    key: "handleClickLeftArrow",
    value: function handleClickLeftArrow() {
      this.scrollbars.current.scrollLeft(this.scrollbars.current.getScrollLeft() - this.scrollbars.current.view.children[0].children[0].clientWidth);
    }
  }, {
    key: "handleClickRightArrow",
    value: function handleClickRightArrow() {
      this.scrollbars.current.scrollLeft(this.scrollbars.current.getScrollLeft() + this.scrollbars.current.view.children[0].children[0].clientWidth);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        thumbnails = _this$props.thumbnails,
        selectedDesignViewIdentifier = _this$props.selectedDesignViewIdentifier,
        classes = _this$props.classes,
        onThumbnailClick = _this$props.onThumbnailClick,
        previewOpen = _this$props.previewOpen,
        scrollbarHeight = _this$props.scrollbarHeight,
        width = _this$props.width;
      var _this$state = this.state,
        isScrollingAvailable = _this$state.isScrollingAvailable,
        availableWidth = _this$state.availableWidth;
      var items = thumbnails.items,
        dotMode = thumbnails.dotMode;
      var isSmallScreen = (0, _withWidth.isWidthDown)('md', width);
      if (items && items.length <= 1) {
        return null;
      }
      if (dotMode || isSmallScreen) {
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: classes.dots
        }, items && items.map(function (thumbnail, index) {
          return /*#__PURE__*/_react["default"].createElement(_AccessibleButton["default"], {
            component: "div",
            className: "".concat(classes.dotContainer, " ").concat(isSmallScreen ? 'wrap-container' : ''),
            key: thumbnail.identifier,
            onClick: function onClick() {
              return onThumbnailClick(thumbnail.identifier);
            }
          }, /*#__PURE__*/_react["default"].createElement(_Typography["default"], {
            component: "div",
            className: selectedDesignViewIdentifier === thumbnail.identifier ? classes.activeDotMode : classes.inactiveDotMode
          }, index + 1));
        }));
      }
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: previewOpen ? classes.fullSizeCarouselWrapper : classes.thumbnailCarouselWrapper,
        ref: this.thumbnailCarouselWrapperRef
      }, isScrollingAvailable && /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
        onClick: this.handleClickLeftArrow.bind(this),
        className: classes.arrowScrollIcon
      }, "keyboard_arrow_left"), /*#__PURE__*/_react["default"].createElement(_reactCustomScrollbars.Scrollbars, {
        style: {
          height: scrollbarHeight || 84,
          width: availableWidth
        },
        autoHide: true,
        ref: this.scrollbars,
        onScrollStop: this.onScrollStop
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.designerThumbnail,
        style: {
          justifyContent: !isScrollingAvailable ? 'center' : 'initial'
        }
      }, items && items.map(function (thumbnail) {
        return /*#__PURE__*/_react["default"].createElement(_AccessibleButton["default"], {
          component: "div",
          className: classes.thumbnailImageContainer,
          key: thumbnail.identifier,
          onClick: function onClick() {
            return onThumbnailClick(thumbnail.identifier);
          },
          value: thumbnail.identifier
        }, /*#__PURE__*/_react["default"].createElement("img", {
          src: thumbnail.thumbnailImage,
          alt: thumbnail.identifier,
          key: thumbnail.identifier,
          className: selectedDesignViewIdentifier === thumbnail.identifier ? classes.activeThumbnail : classes.inactiveThumbnail
        }));
      }))), isScrollingAvailable && /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
        onClick: this.handleClickRightArrow.bind(this),
        className: classes.arrowScrollIcon
      }, "keyboard_arrow_right"));
    }
  }]);
  return Thumbnail;
}(_react["default"].Component);
var VariantCarouselWithResizeDetectWrapper = (0, _ResizeDetect.withResizeDetectWrapper)((0, _withWidth["default"])({
  initialWidth: 'lg'
})(Thumbnail));
var _default = exports["default"] = (0, _styles.withStyles)(styles, {
  name: 'Thumbnail'
})(function (props) {
  return /*#__PURE__*/_react["default"].createElement(VariantCarouselWithResizeDetectWrapper, _extends({
    wrapperClassName: props.classes.resizeDetectWrapper
  }, props));
});