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

'use strict';

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

var Vue = require('vue');
var isFunction = require('lodash/isFunction');
var utils_dom = require('../../utils/dom.js');
var components_configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../../utils/mixins.js');
var components_affix_props = require('./props.js');
require('raf');
require('lodash/isString');
require('../../chunks/log.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../utils/easing.js');
require('lodash/mergeWith');
require('lodash');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

const classMixins = components_configProvider_configReceiver.getClassPrefixMixins("affix");
var _Affix = utils_mixins["default"](Vue__default["default"], classMixins).extend({
  name: "LAffix",
  props: {
    ...components_affix_props["default"]
  },
  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__default["default"](this.onFixedChange)) this.onFixedChange(affixed, {
              top: Number(fixedTop)
            });
          }
          this.ticking = false;
        });
        this.ticking = true;
      }
    }
  },
  mounted() {
    this.placeholderEL = document.createElement("div");
    this.$nextTick(() => {
      this.scrollContainer = utils_dom.getScrollContainer(this.container);
      this.handleScroll();
      utils_dom.on(this.scrollContainer, "scroll", this.handleScroll);
      utils_dom.on(window, "resize", this.handleScroll);
    });
  },
  destroyed() {
    if (!this.scrollContainer) return;
    utils_dom.off(this.scrollContainer, "scroll", this.handleScroll);
    utils_dom.off(window, "resize", this.handleScroll);
  },
  render() {
    const h = arguments[0];
    return h("div", {
      "ref": "affixWrapRef"
    }, [h("div", {
      "ref": "affixRef"
    }, [this.$slots.default])]);
  }
});

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