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

import Vue from 'vue';
import isFunction from 'lodash/isFunction';
import { getScrollContainer, on, off } from '../../utils/dom.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import affixProps from './props.js';
import 'raf';
import 'lodash/isString';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/easing.js';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';

const classMixins = getClassPrefixMixins("affix");
var _Affix = mixins(Vue, classMixins).extend({
  name: "LAffix",
  props: {
    ...affixProps
  },
  watch: {
    offsetTop() {
      this.handleScroll();
    },
    offsetBottom() {
      this.handleScroll();
    },
    zIndex() {
      this.handleScroll();
    }
  },
  methods: {
    handleScroll() {
      const {
        scrollContainer,
        offsetTop,
        offsetBottom
      } = this;
      const {
        affixWrapRef,
        affixRef
      } = this.$refs;
      if (!this.ticking) {
        window.requestAnimationFrame(() => {
          const {
            top: wrapToTop,
            width: wrapWidth,
            height: wrapHeight
          } = (affixWrapRef === null || affixWrapRef === void 0 ? void 0 : affixWrapRef.getBoundingClientRect()) ?? {
            top: 0,
            width: 0,
            height: 0
          };
          let containerTop = 0;
          if (scrollContainer instanceof HTMLElement) containerTop = scrollContainer.getBoundingClientRect().top;
          let fixedTop;
          const calcTop = wrapToTop - containerTop;
          const containerHeight = (scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer[scrollContainer instanceof Window ? "innerHeight" : "clientHeight"]) - wrapHeight;
          const calcBottom = containerTop + containerHeight - offsetBottom;
          if (offsetTop !== void 0 && calcTop <= offsetTop) {
            fixedTop = containerTop + offsetTop;
          } else if (offsetBottom !== void 0 && wrapToTop >= calcBottom) {
            fixedTop = calcBottom;
          } else {
            fixedTop = false;
          }
          if (affixRef) {
            const affixed = fixedTop !== false;
            const placeholderStatus = affixWrapRef.contains(this.placeholderEL);
            if (affixed) {
              affixRef.className = this.componentName;
              affixRef.style.top = `${fixedTop}px`;
              affixRef.style.width = `${wrapWidth}px`;
              affixRef.style.height = `${wrapHeight}px`;
              if (this.zIndex) affixRef.style.zIndex = `${this.zIndex}`;
              if (!placeholderStatus) {
                this.placeholderEL.style.width = `${wrapWidth}px`;
                this.placeholderEL.style.height = `${wrapHeight}px`;
                affixWrapRef.appendChild(this.placeholderEL);
              }
            } else {
              affixRef.removeAttribute("class");
              affixRef.removeAttribute("style");
              placeholderStatus && this.placeholderEL.remove();
            }
            this.$emit("fixedChange", affixed, {
              top: fixedTop
            });
            if (isFunction(this.onFixedChange)) this.onFixedChange(affixed, {
              top: Number(fixedTop)
            });
          }
          this.ticking = false;
        });
        this.ticking = true;
      }
    }
  },
  mounted() {
    this.placeholderEL = document.createElement("div");
    this.$nextTick(() => {
      this.scrollContainer = getScrollContainer(this.container);
      this.handleScroll();
      on(this.scrollContainer, "scroll", this.handleScroll);
      on(window, "resize", this.handleScroll);
    });
  },
  destroyed() {
    if (!this.scrollContainer) return;
    off(this.scrollContainer, "scroll", this.handleScroll);
    off(window, "resize", this.handleScroll);
  },
  render() {
    const h = arguments[0];
    return h("div", {
      "ref": "affixWrapRef"
    }, [h("div", {
      "ref": "affixRef"
    }, [this.$slots.default])]);
  }
});

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