'use strict';

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

var React = require('react');
var tsDebounce = require('ts-debounce');
var clsx = require('clsx');
var useBodyTTag = require('@prom-ui/core/useBodyTTag');
var Portal = require('@prom-ui/core/Portal');
var Button = require('@prom-ui/core/Button');
var Icon = require('@prom-ui/core/Icon');
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 React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var clsx__default = /*#__PURE__*/_interopDefaultCompat(clsx);

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 ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    enumerableOnly && (symbols = symbols.filter(function (sym) {
      return Object.getOwnPropertyDescriptor(object, sym).enumerable;
    })), keys.push.apply(keys, symbols);
  }
  return keys;
}
function _objectSpread2(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = null != arguments[i] ? arguments[i] : {};
    i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
      _defineProperty(target, key, source[key]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
      Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
    });
  }
  return target;
}
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":"ButtonPageUp__root___L21yK"};

var _templateObject;
var _excluded = ["children", "scrollRef", "onClick", "scrollDelay", "scrollOffset", "margin", "zIndex", "style", "className"];
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,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useBodyTTag = useBodyTTag.useBodyTTag(),
    t = _useBodyTTag.t;
  var _React$useState = React__namespace.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    visible = _React$useState2[0],
    setVisible = _React$useState2[1];
  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 () {
    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: _objectSpread2(_objectSpread2({}, positionStyle), {}, {
      zIndex: zIndex
    })
  }, /*#__PURE__*/React__namespace.createElement(Button.Button, _extends({
    "button-padding": "xs",
    "button-theme": "white",
    "button-round": "full",
    "button-blackhole": "scroll-button",
    type: "button",
    "aria-label": t(_templateObject || (_templateObject = _taggedTemplateLiteral(["\u041D\u0430\u0432\u0435\u0440\u0445"]))),
    "data-qaid": "ButtonPageUp",
    "data-testid": "ButtonPageUp"
  }, props, {
    className: clsx__default.default(css.root, className),
    style: _objectSpread2(_objectSpread2({}, 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');
