'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var classNames = require('clsx');
var useForwardRef = require('@prom-ui/core/useForwardRef');
var Button = require('@prom-ui/core/Button');
var Icon = require('@prom-ui/core/Icon');
var useBodyTTag = require('@prom-ui/core/useBodyTTag');
var Left = require('@prom-ui/icons/Left');
var Right = require('@prom-ui/icons/Right');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var classNames__default = /*#__PURE__*/_interopDefaultCompat(classNames);

function _iterableToArrayLimit(arr, i) {
  var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
  if (null != _i) {
    var _s,
      _e,
      _x,
      _r,
      _arr = [],
      _n = !0,
      _d = !1;
    try {
      if (_x = (_i = _i.call(arr)).next, 0 === i) {
        if (Object(_i) !== _i) return;
        _n = !1;
      } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
    } catch (err) {
      _d = !0, _e = err;
    } finally {
      try {
        if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
      } finally {
        if (_d) throw _e;
      }
    }
    return _arr;
  }
}
function _defineProperty(obj, key, value) {
  key = _toPropertyKey(key);
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}
function _extends() {
  _extends = Object.assign ? Object.assign.bind() : function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
  return _extends.apply(this, arguments);
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _taggedTemplateLiteral(strings, raw) {
  if (!raw) {
    raw = strings.slice(0);
  }
  return Object.freeze(Object.defineProperties(strings, {
    raw: {
      value: Object.freeze(raw)
    }
  }));
}
function _slicedToArray(arr, i) {
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === "Object" && o.constructor) n = o.constructor.name;
  if (n === "Map" || n === "Set") return Array.from(o);
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _toPrimitive(input, hint) {
  if (typeof input !== "object" || input === null) return input;
  var prim = input[Symbol.toPrimitive];
  if (prim !== undefined) {
    var res = prim.call(input, hint || "default");
    if (typeof res !== "object") return res;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
  var key = _toPrimitive(arg, "string");
  return typeof key === "symbol" ? key : String(key);
}

var css = {"root":"ScrollControls__root___mOlOq","buttonHolderCommon":"ScrollControls__buttonHolderCommon___8-zJW","buttonHolderPrev":"ScrollControls__buttonHolderPrev___3uXYG ScrollControls__buttonHolderCommon___8-zJW","buttonHolderNext":"ScrollControls__buttonHolderNext___L0VTc ScrollControls__buttonHolderCommon___8-zJW","buttonHideDisabled":"ScrollControls__buttonHideDisabled___1GCww","icon":"ScrollControls__icon___lzBzL","showOnHover":"ScrollControls__showOnHover___StbVV"};

var _templateObject, _templateObject2;
var DIRECTION_NEXT = 1;
var DIRECTION_PREV = -1;
var CONTROLS_UPDATE_DELAY = 100;
var ScrollControlsButton = function ScrollControlsButton(_ref) {
  var scrollRef = _ref.scrollRef,
    _ref$hideOnDisabled = _ref.hideOnDisabled,
    hideOnDisabled = _ref$hideOnDisabled === void 0 ? false : _ref$hideOnDisabled,
    _ref$isActive = _ref.isActive,
    isActive = _ref$isActive === void 0 ? true : _ref$isActive,
    onClickNext = _ref.onClickNext,
    onClickPrev = _ref.onClickPrev,
    zIndex = _ref.zIndex,
    children = _ref.children;
  var _useBodyTTag = useBodyTTag.useBodyTTag(),
    t = _useBodyTTag.t;
  var updateControlsTimer = React.useRef(null);
  var _useState = React.useState({
      prev: false,
      next: false
    }),
    _useState2 = _slicedToArray(_useState, 2),
    controlsVisible = _useState2[0],
    setControlsVisible = _useState2[1];
  var updateControlsVisible = function updateControlsVisible(nextScrollLeft) {
    if (!scrollRef.current) return;
    var _scrollRef$current = scrollRef.current,
      scrollLeft = _scrollRef$current.scrollLeft,
      scrollWidth = _scrollRef$current.scrollWidth,
      clientWidth = _scrollRef$current.clientWidth;
    var currentScrollLeft = (Number.isInteger(nextScrollLeft) ? nextScrollLeft : scrollLeft) || 0;
    setControlsVisible({
      prev: currentScrollLeft > 1,
      next: currentScrollLeft < scrollWidth - clientWidth - 1 // -1 hack detected in PhotoGallery
    });
  };

  var updateScrollLeft = function updateScrollLeft(direction) {
    if (!scrollRef.current) return;
    var _scrollRef$current2 = scrollRef.current,
      scrollLeft = _scrollRef$current2.scrollLeft,
      clientWidth = _scrollRef$current2.clientWidth;
    var offset = clientWidth * direction;
    requestAnimationFrame(function () {
      var nextScrollLeft = scrollLeft + offset;
      updateControlsVisible(nextScrollLeft);
      if (scrollRef.current) {
        scrollRef.current.scrollTo({
          left: nextScrollLeft,
          behavior: 'smooth'
        });
      }
    });
  };
  var handleOnClickPrev = function handleOnClickPrev() {
    if (typeof onClickPrev === 'function') {
      onClickPrev();
    }
    updateScrollLeft(DIRECTION_PREV);
  };
  var handleOnClickNext = function handleOnClickNext() {
    if (typeof onClickNext === 'function') {
      onClickNext();
    }
    updateScrollLeft(DIRECTION_NEXT);
  };
  var handleOnScroll = function handleOnScroll() {
    clearTimeout(updateControlsTimer.current || 0);
    updateControlsTimer.current = setTimeout(updateControlsVisible, CONTROLS_UPDATE_DELAY);
  };
  React.useEffect(function () {
    if (scrollRef.current && isActive) {
      requestAnimationFrame(function () {
        updateControlsVisible();
      });
      scrollRef.current.addEventListener('scroll', handleOnScroll);
    }
    return function () {
      if (scrollRef.current && isActive) {
        scrollRef.current.removeEventListener('scroll', handleOnScroll);
      }
    };
  }, [isActive, children]);
  if (isActive && (controlsVisible.prev || controlsVisible.next)) {
    return /*#__PURE__*/React__namespace.default.createElement(React__namespace.default.Fragment, null, /*#__PURE__*/React__namespace.default.createElement("div", {
      className: css.buttonHolderPrev,
      style: {
        zIndex: zIndex
      }
    }, /*#__PURE__*/React__namespace.default.createElement(Button.Button, {
      "button-theme": "white",
      "button-padding": "none",
      "button-round": "full",
      "button-width": "1-1",
      "button-height": "1-1",
      "button-blackhole": "scroll-button",
      type: "button",
      className: hideOnDisabled ? css.buttonHideDisabled : undefined,
      onClick: handleOnClickPrev,
      disabled: !controlsVisible.prev,
      title: t(_templateObject || (_templateObject = _taggedTemplateLiteral(["\u041D\u0430\u0437\u0430\u0434"])))
    }, /*#__PURE__*/React__namespace.default.createElement(Icon.Icon, {
      className: css.icon,
      "icon-events": "none",
      "icon-as": Left.SvgLeft
    }))), /*#__PURE__*/React__namespace.default.createElement("div", {
      className: css.buttonHolderNext,
      style: {
        zIndex: zIndex
      }
    }, /*#__PURE__*/React__namespace.default.createElement(Button.Button, {
      "button-theme": "white",
      "button-padding": "none",
      "button-round": "full",
      "button-width": "1-1",
      "button-height": "1-1",
      "button-blackhole": "scroll-button",
      type: "button",
      className: hideOnDisabled ? css.buttonHideDisabled : undefined,
      onClick: handleOnClickNext,
      disabled: !controlsVisible.next,
      title: t(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\u0414\u0430\u043B\u0435\u0435"])))
    }, /*#__PURE__*/React__namespace.default.createElement(Icon.Icon, {
      className: css.icon,
      "icon-events": "none",
      "icon-as": Right.SvgRight
    }))));
  }
  return null;
};
ScrollControlsButton.displayName = 'ScrollControlsButton';

var _excluded = ["children", "className", "hideOnDisabled", "isActive", "onClickNext", "onClickPrev", "showOnHover", "zIndex"];
var ScrollControls = /*#__PURE__*/React__namespace.forwardRef(function (_ref, ref) {
  var children = _ref.children,
    className = _ref.className,
    _ref$hideOnDisabled = _ref.hideOnDisabled,
    hideOnDisabled = _ref$hideOnDisabled === void 0 ? true : _ref$hideOnDisabled,
    _ref$isActive = _ref.isActive,
    isActive = _ref$isActive === void 0 ? true : _ref$isActive,
    onClickNext = _ref.onClickNext,
    onClickPrev = _ref.onClickPrev,
    _ref$showOnHover = _ref.showOnHover,
    showOnHover = _ref$showOnHover === void 0 ? false : _ref$showOnHover,
    zIndex = _ref.zIndex,
    props = _objectWithoutProperties(_ref, _excluded);
  var scrollRef = useForwardRef.useForwardRef(ref, null);
  return /*#__PURE__*/React__namespace.createElement("div", _extends({
    className: classNames__default.default(css.root, className, _defineProperty({}, css.showOnHover, showOnHover))
  }, props), children(scrollRef), /*#__PURE__*/React__namespace.createElement(ScrollControlsButton, {
    scrollRef: scrollRef,
    hideOnDisabled: hideOnDisabled,
    isActive: isActive,
    onClickNext: onClickNext,
    onClickPrev: onClickPrev,
    zIndex: zIndex
  }, children));
});
ScrollControls.displayName = 'ScrollControls';

exports.ScrollControls = ScrollControls;

require('./style.development.css');
