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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
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 '../utils/easing.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
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 '../_chunks/dep-2cb61c79.js';
import 'dayjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var classMixins = getClassPrefixMixins("affix");
var _Affix = mixins(Vue, classMixins).extend({
  name: "TAffix",
  props: _objectSpread({}, affixProps),
  watch: {
    offsetTop: function offsetTop() {
      this.handleScroll();
    },
    offsetBottom: function offsetBottom() {
      this.handleScroll();
    },
    zIndex: function zIndex() {
      this.handleScroll();
    }
  },
  methods: {
    handleScroll: function handleScroll() {
      var _this = this;
      var scrollContainer = this.scrollContainer,
        offsetTop = this.offsetTop,
        offsetBottom = this.offsetBottom;
      var _this$$refs = this.$refs,
        affixWrapRef = _this$$refs.affixWrapRef,
        affixRef = _this$$refs.affixRef;
      if (!this.ticking) {
        window.requestAnimationFrame(function () {
          var _affixWrapRef$getBoun, _scrollContainer$getB;
          var _ref = (_affixWrapRef$getBoun = affixWrapRef === null || affixWrapRef === void 0 ? void 0 : affixWrapRef.getBoundingClientRect()) !== null && _affixWrapRef$getBoun !== void 0 ? _affixWrapRef$getBoun : {
              top: 0,
              width: 0,
              height: 0
            },
            wrapToTop = _ref.top,
            wrapWidth = _ref.width,
            wrapHeight = _ref.height;
          var containerTop = 0;
          if (scrollContainer instanceof HTMLElement) containerTop = scrollContainer === null || scrollContainer === void 0 ? void 0 : (_scrollContainer$getB = scrollContainer.getBoundingClientRect) === null || _scrollContainer$getB === void 0 ? void 0 : _scrollContainer$getB.call(scrollContainer).top;
          var fixedTop;
          var calcTop = wrapToTop - containerTop;
          var containerHeight = (scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer[scrollContainer instanceof Window ? "innerHeight" : "clientHeight"]) - wrapHeight;
          var 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) {
            var affixed = fixedTop !== false;
            var placeholderStatus = affixWrapRef.contains(_this.placeholderEL);
            if (affixed) {
              affixRef.className = _this.componentName;
              affixRef.style.top = "".concat(fixedTop, "px");
              affixRef.style.width = "".concat(wrapWidth, "px");
              affixRef.style.height = "".concat(wrapHeight, "px");
              if (_this.zIndex) affixRef.style.zIndex = "".concat(_this.zIndex);
              if (!placeholderStatus) {
                _this.placeholderEL.style.width = "".concat(wrapWidth, "px");
                _this.placeholderEL.style.height = "".concat(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: function mounted() {
    var _this2 = this;
    this.placeholderEL = document.createElement("div");
    var timer = setTimeout(function () {
      _this2.scrollContainer = getScrollContainer(_this2.container);
      _this2.handleScroll();
      on(_this2.scrollContainer, "scroll", _this2.handleScroll);
      on(window, "resize", _this2.handleScroll);
      clearTimeout(timer);
    }, 0);
  },
  destroyed: function destroyed() {
    if (!this.scrollContainer) return;
    off(this.scrollContainer, "scroll", this.handleScroll);
    off(window, "resize", this.handleScroll);
  },
  render: function render() {
    var h = arguments[0];
    return h("div", {
      "ref": "affixWrapRef"
    }, [h("div", {
      "ref": "affixRef"
    }, [this.$slots["default"]])]);
  }
});

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