/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import Vue from 'vue';
import isFunction from 'lodash/isFunction';
import { prefix } from '../config.js';
import { getScrollContainer, on, off } from '../utils/dom.js';
import affixProps from './props.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';

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 name = "".concat(prefix, "-affix");
var _Affix = Vue.extend({
  name: "TAffix",
  props: _objectSpread({}, affixProps),
  data: function data() {
    return {
      fixedTop: false,
      oldWidthHeight: {
        width: "0px",
        height: "0px"
      }
    };
  },
  watch: {
    offsetTop: function offsetTop() {
      this.calcInitValue();
    },
    offsetBottom: function offsetBottom() {
      this.calcInitValue();
    },
    fixedTop: function fixedTop(val) {
      this.$emit("fixedChange", val !== false, {
        top: val
      });
      if (isFunction(this.onFixedChange)) this.onFixedChange(val !== false, {
        top: Number(val)
      });
    }
  },
  methods: {
    handleScroll: function handleScroll() {
      var _this = this;

      if (!this.ticking) {
        window.requestAnimationFrame(function () {
          var _this$$el$getBounding = _this.$el.getBoundingClientRect(),
              top = _this$$el$getBounding.top;

          var containerTop = 0;

          if (_this.scrollContainer instanceof HTMLElement) {
            containerTop = _this.scrollContainer.getBoundingClientRect().top;
          }

          var calcTop = top - containerTop;
          var calcBottom = containerTop + _this.containerHeight - _this.offsetBottom;

          if (_this.offsetTop !== void 0 && calcTop <= _this.offsetTop) {
            _this.fixedTop = containerTop + _this.offsetTop;
          } else if (_this.offsetBottom !== void 0 && top >= calcBottom) {
            _this.fixedTop = calcBottom;
          } else {
            _this.fixedTop = false;
          }

          _this.ticking = false;
        });
        this.ticking = true;
      }
    },
    calcInitValue: function calcInitValue() {
      var scrollContainer = this.scrollContainer;
      var containerHeight = scrollContainer[scrollContainer instanceof Window ? "innerHeight" : "clientHeight"];
      this.containerHeight = containerHeight - this.$el.clientHeight;

      var _ref = this.$el.querySelector(".".concat(name)) || this.$el,
          clientWidth = _ref.clientWidth,
          clientHeight = _ref.clientHeight;

      this.oldWidthHeight = {
        width: "".concat(clientWidth, "px"),
        height: "".concat(clientHeight, "px")
      };
      this.handleScroll();
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _this2.$nextTick();

            case 2:
              _this2.scrollContainer = getScrollContainer(_this2.container);

              _this2.calcInitValue();

              on(_this2.scrollContainer, "scroll", _this2.handleScroll);
              on(window, "resize", _this2.calcInitValue);
              if (!(_this2.scrollContainer instanceof Window)) on(window, "scroll", _this2.handleScroll);

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  },
  destroyed: function destroyed() {
    if (!this.scrollContainer) return;
    off(this.scrollContainer, "scroll", this.handleScroll);
    off(window, "resize", this.calcInitValue);
    if (!(this.scrollContainer instanceof Window)) off(window, "scroll", this.handleScroll);
  },
  render: function render() {
    var h = arguments[0];
    var children = this.$slots["default"],
        oldWidthHeight = this.oldWidthHeight,
        fixedTop = this.fixedTop,
        zIndex = this.zIndex;

    if (fixedTop !== false) {
      return h("div", [h("div", {
        "style": oldWidthHeight
      }), h("div", {
        "class": name,
        "style": {
          zIndex: zIndex,
          top: "".concat(fixedTop, "px"),
          width: oldWidthHeight.width
        }
      }, [children])]);
    }

    return h("div", [children]);
  }
});

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