/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var isFunction = require('lodash/isFunction');
var utils_dom = require('../utils/dom.js');
var affix_props = require('./props.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../utils/mixins.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');

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

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

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__default["default"](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 = configProvider_configReceiver.getClassPrefixMixins("affix");
var _Affix = utils_mixins["default"](Vue__default["default"], classMixins).extend({
  name: "TAffix",
  props: _objectSpread({}, affix_props["default"]),
  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;

          var _ref = (_affixWrapRef$getBoun = 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.getBoundingClientRect().top;
          }

          var fixedTop;
          var calcTop = wrapToTop - containerTop;
          var containerHeight = 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__default["default"](_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");
    this.$nextTick(function () {
      _this2.scrollContainer = utils_dom.getScrollContainer(_this2.container);

      _this2.handleScroll();

      utils_dom.on(_this2.scrollContainer, "scroll", _this2.handleScroll);
      utils_dom.on(window, "resize", _this2.handleScroll);
    });
  },
  destroyed: function destroyed() {
    if (!this.scrollContainer) return;
    utils_dom.off(this.scrollContainer, "scroll", this.handleScroll);
    utils_dom.off(window, "resize", this.handleScroll);
  },
  render: function render() {
    var h = arguments[0];
    return h("div", {
      "ref": "affixWrapRef"
    }, [h("div", {
      "ref": "affixRef"
    }, [this.$slots["default"]])]);
  }
});

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