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

import { _ as _asyncToGenerator, r as regenerator } from '../_chunks/dep-beaa677e.mjs';
import { defineComponent, ref, nextTick, watch, onMounted, onActivated, onDeactivated, onBeforeUnmount, createVNode } from 'vue';
import { i as isFunction_1 } from '../_chunks/dep-6b22dd27.mjs';
import { i as isUndefined_1 } from '../_chunks/dep-30451c48.mjs';
import { getScrollContainer, on, off } from '../utils/dom.mjs';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import { useTNodeJSX } from '../hooks/tnode.mjs';
import props from './props.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-0f0c89ff.mjs';
import '../_chunks/dep-1cf2b129.mjs';
import '../_chunks/dep-e3b14d70.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../utils/easing.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-44417261.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-c852a61a.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-99920870.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../config-provider/type.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-c4e1a112.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-bd03960e.mjs';

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_1(props2.offsetTop) && calcTop <= props2.offsetTop) {
            fixedTop = containerTop + props2.offsetTop;
          } else if (!isUndefined_1(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_1(props2.onFixedChange)) props2.onFixedChange(affixed, {
              top: Number(fixedTop)
            });
          }
          ticking.value = false;
        });
        ticking.value = true;
      }
    };
    var bindScroll = /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee() {
        return regenerator.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.mjs.map
