/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var utils_dom = require('../../../utils/dom.js');
require('vue');
require('raf');
require('lodash/isString');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../utils/easing.js');

function useAffix(props) {
  const tableContentRef = VueCompositionAPI.ref();
  const affixHeaderRef = VueCompositionAPI.ref();
  const affixFooterRef = VueCompositionAPI.ref();
  const horizontalScrollbarRef = VueCompositionAPI.ref();
  const paginationRef = VueCompositionAPI.ref();
  const showAffixHeader = VueCompositionAPI.ref(true);
  const showAffixFooter = VueCompositionAPI.ref(true);
  const showAffixPagination = VueCompositionAPI.ref(true);
  const isVirtualScroll = VueCompositionAPI.computed(() => props.scroll && props.scroll.type === "virtual" && (props.scroll.threshold || 100) < props.data.length);
  const isAffixed = VueCompositionAPI.computed(() => !!(props.headerAffixedTop || props.footerAffixedBottom || props.horizontalScrollAffixedBottom));
  let lastScrollLeft = 0;
  const onHorizontalScroll = scrollElement => {
    if (!isAffixed.value && !isVirtualScroll.value) return;
    let target = scrollElement;
    if (!target && tableContentRef.value) {
      lastScrollLeft = 0;
      target = tableContentRef.value;
    }
    if (!target) return;
    const left = target.scrollLeft;
    if (lastScrollLeft === left) return;
    lastScrollLeft = left;
    const toUpdateScrollElement = [tableContentRef.value, affixHeaderRef.value, affixFooterRef.value, horizontalScrollbarRef.value];
    for (let i = 0, len = toUpdateScrollElement.length; i < len; i++) {
      if (toUpdateScrollElement[i] && scrollElement !== toUpdateScrollElement[i]) toUpdateScrollElement[i].scrollLeft = left;
    }
  };
  const isAffixedBottomElementShow = (elementRect, tableRect, headerHeight) => tableRect.top + headerHeight < elementRect.top && elementRect.top > elementRect.height;
  const isAffixedBottomScrollShow = (elementRect, tableRect, headerHeight) => tableContentRef.value.scrollWidth > tableContentRef.value.clientWidth && isAffixedBottomElementShow(elementRect, tableRect, headerHeight);
  const getOffsetTop = props2 => {
    if (typeof props2 === "boolean") return 0;
    return props2.offsetTop || 0;
  };
  const updateAffixHeaderOrFooter = () => {
    var _tableContentRef$valu, _tableContentRef$valu2, _tableContentRef$valu3, _affixFooterRef$value;
    if (!isAffixed.value && !isVirtualScroll.value) return;
    const pos = (_tableContentRef$valu = tableContentRef.value) === null || _tableContentRef$valu === void 0 ? void 0 : _tableContentRef$valu.getBoundingClientRect();
    const headerRect = (_tableContentRef$valu2 = tableContentRef.value) === null || _tableContentRef$valu2 === void 0 ? void 0 : (_tableContentRef$valu3 = _tableContentRef$valu2.querySelector("thead")) === null || _tableContentRef$valu3 === void 0 ? void 0 : _tableContentRef$valu3.getBoundingClientRect();
    const headerHeight = (headerRect === null || headerRect === void 0 ? void 0 : headerRect.height) || 0;
    const footerRect = (_affixFooterRef$value = affixFooterRef.value) === null || _affixFooterRef$value === void 0 ? void 0 : _affixFooterRef$value.getBoundingClientRect();
    if ((props.headerAffixedTop || isVirtualScroll.value) && affixHeaderRef.value) {
      const offsetTop = getOffsetTop(props.headerAffixProps || props.headerAffixedTop);
      const footerHeight = (footerRect === null || footerRect === void 0 ? void 0 : footerRect.height) || 0;
      let r = Math.abs(pos.top) < pos.height - headerHeight - offsetTop - footerHeight;
      if (isVirtualScroll.value) r = pos.top > -1 * headerRect.height;
      showAffixHeader.value = r;
    }
    if (props.footerAffixedBottom && affixFooterRef !== null && affixFooterRef !== void 0 && affixFooterRef.value) {
      showAffixFooter.value = isAffixedBottomElementShow(footerRect, pos, headerHeight);
    } else if (props.horizontalScrollAffixedBottom && horizontalScrollbarRef !== null && horizontalScrollbarRef !== void 0 && horizontalScrollbarRef.value) {
      const horizontalScrollbarRect = horizontalScrollbarRef.value.getBoundingClientRect();
      showAffixFooter.value = isAffixedBottomScrollShow(horizontalScrollbarRect, pos, headerHeight);
    }
    if (props.paginationAffixedBottom && paginationRef.value) {
      const pageRect = paginationRef.value.getBoundingClientRect();
      showAffixPagination.value = isAffixedBottomElementShow(pageRect, pos, headerHeight);
    }
  };
  const onDocumentScroll = () => {
    updateAffixHeaderOrFooter();
  };
  const onFootScroll = () => {
    onHorizontalScroll(affixFooterRef.value);
  };
  const onHeaderScroll = () => {
    onHorizontalScroll(affixHeaderRef.value);
  };
  const horizontalScrollbarScroll = () => {
    onHorizontalScroll(horizontalScrollbarRef.value);
  };
  const onTableContentScroll = () => {
    onHorizontalScroll(tableContentRef.value);
  };
  const onFootMouseEnter = () => {
    utils_dom.on(affixFooterRef.value, "scroll", onFootScroll);
  };
  const onFootMouseLeave = () => {
    utils_dom.off(affixFooterRef.value, "scroll", onFootScroll);
  };
  const onHeaderMouseEnter = () => {
    utils_dom.on(affixHeaderRef.value, "scroll", onHeaderScroll);
  };
  const onHeaderMouseLeave = () => {
    utils_dom.off(affixHeaderRef.value, "scroll", onHeaderScroll);
  };
  const onScrollbarMouseEnter = () => {
    utils_dom.on(horizontalScrollbarRef.value, "scroll", horizontalScrollbarScroll);
  };
  const onScrollbarMouseLeave = () => {
    utils_dom.off(horizontalScrollbarRef.value, "scroll", horizontalScrollbarScroll);
  };
  const onTableContentMouseEnter = () => {
    utils_dom.on(tableContentRef.value, "scroll", onTableContentScroll);
  };
  const onTableContentMouseLeave = () => {
    utils_dom.off(tableContentRef.value, "scroll", onTableContentScroll);
  };
  const addHorizontalScrollListeners = () => {
    if (affixHeaderRef.value) {
      utils_dom.on(affixHeaderRef.value, "mouseenter", onHeaderMouseEnter);
      utils_dom.on(affixHeaderRef.value, "mouseleave", onHeaderMouseLeave);
    }
    if (props.footerAffixedBottom && affixFooterRef.value) {
      utils_dom.on(affixFooterRef.value, "mouseenter", onFootMouseEnter);
      utils_dom.on(affixFooterRef.value, "mouseleave", onFootMouseLeave);
    }
    if (props.horizontalScrollAffixedBottom && horizontalScrollbarRef.value) {
      utils_dom.on(horizontalScrollbarRef.value, "mouseenter", onScrollbarMouseEnter);
      utils_dom.on(horizontalScrollbarRef.value, "mouseleave", onScrollbarMouseLeave);
    }
    if ((isAffixed.value || isVirtualScroll.value) && tableContentRef.value) {
      utils_dom.on(tableContentRef.value, "mouseenter", onTableContentMouseEnter);
      utils_dom.on(tableContentRef.value, "mouseleave", onTableContentMouseLeave);
    }
  };
  const removeHorizontalScrollListeners = () => {
    if (affixHeaderRef.value) {
      utils_dom.off(affixHeaderRef.value, "mouseenter", onHeaderMouseEnter);
      utils_dom.off(affixHeaderRef.value, "mouseleave", onHeaderMouseLeave);
    }
    if (affixFooterRef.value) {
      utils_dom.off(affixFooterRef.value, "mouseenter", onFootMouseEnter);
      utils_dom.off(affixFooterRef.value, "mouseleave", onFootMouseLeave);
    }
    if (tableContentRef.value) {
      utils_dom.off(tableContentRef.value, "mouseenter", onTableContentMouseEnter);
      utils_dom.off(tableContentRef.value, "mouseleave", onTableContentMouseLeave);
    }
    if (horizontalScrollbarRef.value) {
      utils_dom.off(horizontalScrollbarRef.value, "mouseenter", onScrollbarMouseEnter);
      utils_dom.off(horizontalScrollbarRef.value, "mouseleave", onScrollbarMouseLeave);
    }
  };
  const addVerticalScrollListener = () => {
    if (!isAffixed.value && !props.paginationAffixedBottom) return;
    const timer = setTimeout(() => {
      if (isAffixed.value || props.paginationAffixedBottom) utils_dom.on(document, "scroll", onDocumentScroll);else utils_dom.off(document, "scroll", onDocumentScroll);
      clearTimeout(timer);
    });
  };
  VueCompositionAPI.watch([affixHeaderRef, affixFooterRef, horizontalScrollbarRef, tableContentRef], () => {
    addHorizontalScrollListeners();
    onHorizontalScroll();
    updateAffixHeaderOrFooter();
  });
  VueCompositionAPI.watch(isAffixed, addVerticalScrollListener);
  VueCompositionAPI.watch(() => [props.data, props.columns, props.headerAffixedTop, props.footerAffixedBottom, props.horizontalScrollAffixedBottom], () => {
    onHorizontalScroll();
  });
  VueCompositionAPI.onBeforeMount(() => {
    utils_dom.off(document, "scroll", onDocumentScroll);
    removeHorizontalScrollListeners();
  });
  const setTableContentRef = tableContent => {
    tableContentRef.value = tableContent;
    addVerticalScrollListener();
  };
  return {
    showAffixHeader,
    showAffixFooter,
    showAffixPagination,
    affixHeaderRef,
    affixFooterRef,
    horizontalScrollbarRef,
    paginationRef,
    onHorizontalScroll,
    setTableContentRef,
    updateAffixHeaderOrFooter
  };
}

exports["default"] = useAffix;
//# sourceMappingURL=useAffix.js.map
