'use strict';

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

var _extends = require('@babel/runtime/helpers/extends');
var _taggedTemplateLiteral = require('@babel/runtime/helpers/taggedTemplateLiteral');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var tsDebounce = require('ts-debounce');
var clsx = require('clsx');
var Button = require('@prom-ui/core/Button');
var Icon = require('@prom-ui/core/Icon');
var Portal = require('@prom-ui/core/Portal');
var BodyProvider = require('@prom-ui/core/BodyProvider');
var useIsMounted = require('@prom-ui/core/useIsMounted');
var ArrowUp = require('@prom-ui/icons/ArrowUp');

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 _extends__default = /*#__PURE__*/_interopDefaultCompat(_extends);
var _taggedTemplateLiteral__default = /*#__PURE__*/_interopDefaultCompat(_taggedTemplateLiteral);
var _defineProperty__default = /*#__PURE__*/_interopDefaultCompat(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultCompat(_slicedToArray);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefaultCompat(_objectWithoutProperties);
var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var clsx__default = /*#__PURE__*/_interopDefaultCompat(clsx);

var css = {"root":"ButtonPageUp__root___L21yK"};

var _templateObject;
var _excluded = ["children", "scrollRef", "onClick", "scrollDelay", "scrollOffset", "margin", "zIndex", "style", "className", "onVisibleChange"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var POSITION_FIXED = {
  position: 'fixed',
  right: 0,
  bottom: 0,
  width: '100%'
};
var POSITION_STICKY = {
  position: 'sticky',
  bottom: 0,
  height: 0,
  width: '100%'
};
var ButtonPageUp = function ButtonPageUp(_ref) {
  var children = _ref.children,
    scrollRef = _ref.scrollRef,
    onClick = _ref.onClick,
    _ref$scrollDelay = _ref.scrollDelay,
    scrollDelay = _ref$scrollDelay === void 0 ? 100 : _ref$scrollDelay,
    _ref$scrollOffset = _ref.scrollOffset,
    scrollOffset = _ref$scrollOffset === void 0 ? 100 : _ref$scrollOffset,
    _ref$margin = _ref.margin,
    margin = _ref$margin === void 0 ? '0 18px 18px 0' : _ref$margin,
    _ref$zIndex = _ref.zIndex,
    zIndex = _ref$zIndex === void 0 ? 10 : _ref$zIndex,
    style = _ref.style,
    className = _ref.className,
    onVisibleChange = _ref.onVisibleChange,
    props = _objectWithoutProperties__default.default(_ref, _excluded);
  var _useBodyStoreTTag = BodyProvider.useBodyStoreTTag(),
    t = _useBodyStoreTTag.t;
  var _React$useState = React__namespace.useState(false),
    _React$useState2 = _slicedToArray__default.default(_React$useState, 2),
    visible = _React$useState2[0],
    setVisible = _React$useState2[1];
  var isMounted = useIsMounted.useIsMounted();
  var getRootElement = function getRootElement() {
    return (scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) || document.documentElement;
  };
  var handleScrollToTop = function handleScrollToTop(e) {
    if (typeof onClick === 'function') {
      onClick(e);
    }
    getRootElement().scroll({
      top: 0,
      behavior: 'smooth'
    });
  };
  var handleSetVisible = tsDebounce.debounce(function () {
    var rootElement = getRootElement();
    var isScrollAllow = rootElement.scrollHeight - rootElement.clientHeight > 0;
    var isScrollСorrect = rootElement.scrollTop >= scrollOffset;
    setVisible(isScrollAllow && isScrollСorrect);
  }, scrollDelay);
  React__namespace.useEffect(function () {
    if (isMounted && typeof onVisibleChange === 'function') {
      onVisibleChange(visible);
    }
  }, [visible]);
  React__namespace.useEffect(function () {
    var rootElement = (scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) || document;
    rootElement.addEventListener('resize', handleSetVisible);
    rootElement.addEventListener('scroll', handleSetVisible);
    handleSetVisible();
    return function () {
      rootElement.removeEventListener('resize', handleSetVisible);
      rootElement.removeEventListener('scroll', handleSetVisible);
    };
  }, []);
  if (!visible) return null;
  var Parent = scrollRef !== null && scrollRef !== void 0 && scrollRef.current ? React__namespace.Fragment : Portal.Portal;
  var positionStyle = scrollRef !== null && scrollRef !== void 0 && scrollRef.current ? POSITION_STICKY : POSITION_FIXED;
  return /*#__PURE__*/React__namespace.createElement(Parent, null, /*#__PURE__*/React__namespace.createElement("div", {
    style: _objectSpread(_objectSpread({}, positionStyle), {}, {
      zIndex: zIndex
    })
  }, /*#__PURE__*/React__namespace.createElement(Button.Button, _extends__default.default({
    "button-padding": "xs",
    "button-theme": "white",
    "button-round": "full",
    "button-blackhole": "scroll-button",
    type: "button",
    "aria-label": t(_templateObject || (_templateObject = _taggedTemplateLiteral__default.default(["\u041D\u0430\u0432\u0435\u0440\u0445"]))),
    "data-qaid": "ButtonPageUp",
    "data-testid": "ButtonPageUp"
  }, props, {
    className: clsx__default.default(css.root, className),
    style: _objectSpread(_objectSpread({}, style), {}, {
      margin: margin
    }),
    onClick: handleScrollToTop
  }), children || /*#__PURE__*/React__namespace.createElement(Icon.Icon, {
    "icon-as": ArrowUp.SvgArrowUp,
    style: {
      width: 24,
      height: 24
    }
  }))));
};
ButtonPageUp.displayName = 'ButtonPageUp';

exports.ButtonPageUp = ButtonPageUp;

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