/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import { defineComponent, ref, nextTick, watch, onMounted, onActivated, onDeactivated, onBeforeUnmount, createVNode } from 'vue';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import isFunction from 'lodash/isFunction';
import isUndefined from 'lodash/isUndefined';
import { getScrollContainer, on, off } from '../utils/dom.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useTNodeJSX } from '../hooks/tnode.js';
import props from './props.js';
import '../_chunks/dep-512eb181.js';
import 'lodash/isString';
import 'lodash/isArray';
import '../utils/easing.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import 'dayjs';
import '../_chunks/dep-ac28b67c.js';
import '../config-provider/type.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isObject';

var _Affix = defineComponent({
  name: "TAffix",
  props: props,
  emits: ["fixedChange"],
  setup: function setup(props2, context) {
    var _document;
    var COMPONENT_NAME = usePrefixClass("affix");
    var renderTNodeJSX = useTNodeJSX();
    var affixWrapRef = ref(null);
    var affixRef = ref(null);
    var placeholderEL = ref((_document = document) === null || _document === void 0 ? void 0 : _document.createElement("div"));
    var ticking = ref(false);
    var binded = ref(false);
    var scrollContainer = ref();
    var affixStyle = ref();
    var handleScroll = function handleScroll() {
      if (!ticking.value) {
        window.requestAnimationFrame(function () {
          var _affixWrapRef$value$g;
          var _ref = (_affixWrapRef$value$g = affixWrapRef.value.getBoundingClientRect()) !== null && _affixWrapRef$value$g !== void 0 ? _affixWrapRef$value$g : {
              top: 0,
              width: 0,
              height: 0
            },
            wrapToTop = _ref.top,
            wrapWidth = _ref.width,
            wrapHeight = _ref.height;
          var containerTop = 0;
          if (scrollContainer.value instanceof HTMLElement) containerTop = scrollContainer.value.getBoundingClientRect().top;
          var fixedTop;
          var calcTop = wrapToTop - containerTop;
          var containerHeight = scrollContainer.value[scrollContainer.value instanceof Window ? "innerHeight" : "clientHeight"] - wrapHeight;
          var calcBottom = containerTop + containerHeight - props2.offsetBottom;
          if (!isUndefined(props2.offsetTop) && calcTop <= props2.offsetTop) {
            fixedTop = containerTop + props2.offsetTop;
          } else if (!isUndefined(props2.offsetBottom) && wrapToTop >= calcBottom) {
            fixedTop = calcBottom;
          } else {
            fixedTop = false;
          }
          if (affixRef.value) {
            var affixed = fixedTop !== false;
            var placeholderStatus = affixWrapRef.value.contains(placeholderEL.value);
            if (affixed) {
              affixRef.value.className = COMPONENT_NAME.value;
              affixStyle.value = {
                top: "".concat(fixedTop, "px"),
                width: "".concat(wrapWidth, "px"),
                height: "".concat(wrapHeight, "px"),
                zIndex: props2.zIndex
              };
              if (!placeholderStatus) {
                placeholderEL.value.style.width = "".concat(wrapWidth, "px");
                placeholderEL.value.style.height = "".concat(wrapHeight, "px");
                affixWrapRef.value.appendChild(placeholderEL.value);
              }
            } else {
              affixRef.value.removeAttribute("class");
              affixStyle.value = void 0;
              placeholderStatus && placeholderEL.value.remove();
            }
            context.emit("fixedChange", affixed, {
              top: Number(fixedTop)
            });
            if (isFunction(props2.onFixedChange)) props2.onFixedChange(affixed, {
              top: Number(fixedTop)
            });
          }
          ticking.value = false;
        });
        ticking.value = true;
      }
    };
    var bindScroll = /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return nextTick();
            case 2:
              if (!binded.value) {
                _context.next = 4;
                break;
              }
              return _context.abrupt("return");
            case 4:
              scrollContainer.value = getScrollContainer(props2.container);
              on(scrollContainer.value, "scroll", handleScroll);
              on(window, "resize", handleScroll);
              binded.value = true;
            case 8:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function bindScroll() {
        return _ref2.apply(this, arguments);
      };
    }();
    var unbindScroll = function unbindScroll() {
      if (!scrollContainer.value || !binded.value) return;
      off(scrollContainer.value, "scroll", handleScroll);
      off(window, "resize", handleScroll);
      binded.value = false;
    };
    watch(function () {
      return props2.offsetTop;
    }, function () {
      handleScroll();
    });
    watch(function () {
      return props2.offsetBottom;
    }, function () {
      handleScroll();
    });
    watch(function () {
      return props2.zIndex;
    }, function () {
      handleScroll();
    });
    onMounted(bindScroll);
    onActivated(bindScroll);
    onDeactivated(unbindScroll);
    onBeforeUnmount(unbindScroll);
    return {
      affixWrapRef: affixWrapRef,
      affixRef: affixRef,
      bindScroll: bindScroll,
      unbindScroll: unbindScroll,
      handleScroll: handleScroll,
      scrollContainer: scrollContainer,
      renderTNodeJSX: renderTNodeJSX,
      affixStyle: affixStyle
    };
  },
  render: function render() {
    return createVNode("div", {
      "ref": "affixWrapRef"
    }, [createVNode("div", {
      "ref": "affixRef",
      "style": this.affixStyle
    }, [this.renderTNodeJSX("default")])]);
  }
});

export { _Affix as default };
//# sourceMappingURL=affix.js.map
