import { slicedToArray as _slicedToArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { useRef, useState, useEffect, useLayoutEffect } from 'react';
import { useBottomSheetDrag } from './useBottomSheetDrag.js';

var useBottomSheet = function useBottomSheet(args) {
  var opened = args.opened,
    content = args.content,
    onClose = args.onClose,
    hasHandle = args.hasHandle;
  var panelRef = useRef(null);
  var bodyRef = useRef(null);
  var handleRef = useRef(null);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    overflows = _useState2[0],
    setOverflows = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    headerDivider = _useState4[0],
    setHeaderDivider = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    footerDivider = _useState6[0],
    setFooterDivider = _useState6[1];

  // Блокировка скролла страницы
  useEffect(function () {
    if (!opened) {
      return undefined;
    }
    var _document = document,
      body = _document.body;
    var _window = window,
      scrollY = _window.scrollY;
    var prev = {
      position: body.style.position,
      top: body.style.top,
      left: body.style.left,
      right: body.style.right,
      width: body.style.width,
      overflow: body.style.overflow
    };
    body.style.position = 'fixed';
    body.style.top = "-".concat(scrollY, "px");
    body.style.left = '0';
    body.style.right = '0';
    body.style.width = '100%';
    body.style.overflow = 'hidden';
    return function () {
      body.style.position = prev.position;
      body.style.top = prev.top;
      body.style.left = prev.left;
      body.style.right = prev.right;
      body.style.width = prev.width;
      body.style.overflow = prev.overflow;
      window.scrollTo(0, scrollY);
    };
  }, [opened]);

  // Overflow и разделители body.
  useLayoutEffect(function () {
    var panel = panelRef.current;
    var body = bodyRef.current;
    if (body) {
      body.scrollTop = 0;
    }
    if (!opened) {
      setHeaderDivider(false);
      setFooterDivider(false);
      setOverflows(false);
      return;
    }
    if (!panel || !body) {
      return;
    }
    var maxPx = parseFloat(getComputedStyle(panel).maxHeight);
    var isOverflow = Number.isFinite(maxPx) && maxPx > 0 && panel.scrollHeight > maxPx;
    var update = function update() {
      setHeaderDivider(body.scrollTop > 0);
      setFooterDivider(body.scrollHeight - body.scrollTop - body.clientHeight > 1);
    };
    setOverflows(isOverflow);
    update();
    body.addEventListener('scroll', update);
    return function () {
      return body.removeEventListener('scroll', update);
    };
  }, [opened, content]);
  useBottomSheetDrag({
    panelRef: panelRef,
    handleRef: handleRef,
    enabled: hasHandle !== false,
    onClose: onClose
  });
  return {
    panelRef: panelRef,
    bodyRef: bodyRef,
    handleRef: handleRef,
    overflows: overflows,
    headerDivider: headerDivider,
    footerDivider: footerDivider
  };
};

export { useBottomSheet };
