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

'use strict';

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

var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var vue = require('vue');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var isFunction = require('lodash/isFunction');
var isUndefined = require('lodash/isUndefined');
var utils_dom = require('../utils/dom.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_tnode = require('../hooks/tnode.js');
var affix_props = require('./props.js');
require('../_chunks/dep-f4db2590.js');
require('lodash/isString');
require('lodash/isArray');
require('../utils/easing.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('dayjs');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');

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

var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isUndefined__default = /*#__PURE__*/_interopDefaultLegacy(isUndefined);

var _Affix = vue.defineComponent({
  name: "TAffix",
  props: affix_props["default"],
  emits: ["fixedChange"],
  setup: function setup(props2, context) {
    var _document;
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("affix");
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var affixWrapRef = vue.ref(null);
    var affixRef = vue.ref(null);
    var placeholderEL = vue.ref((_document = document) === null || _document === void 0 ? void 0 : _document.createElement("div"));
    var ticking = vue.ref(false);
    var binded = vue.ref(false);
    var scrollContainer = vue.ref();
    var affixStyle = vue.ref();
    var handleScroll = function handleScroll() {
      if (!ticking.value) {
        window.requestAnimationFrame(function () {
          var _affixWrapRef$value$g;
          var _ref = (_affixWrapRef$value$g = affixWrapRef.value.getBoundingClientRect()) !== null && _affixWrapRef$value$g !== void 0 ? _affixWrapRef$value$g : {
              top: 0,
              width: 0,
              height: 0
            },
            wrapToTop = _ref.top,
            wrapWidth = _ref.width,
            wrapHeight = _ref.height;
          var containerTop = 0;
          if (scrollContainer.value instanceof HTMLElement) containerTop = scrollContainer.value.getBoundingClientRect().top;
          var fixedTop;
          var calcTop = wrapToTop - containerTop;
          var containerHeight = scrollContainer.value[scrollContainer.value instanceof Window ? "innerHeight" : "clientHeight"] - wrapHeight;
          var calcBottom = containerTop + containerHeight - props2.offsetBottom;
          if (!isUndefined__default["default"](props2.offsetTop) && calcTop <= props2.offsetTop) {
            fixedTop = containerTop + props2.offsetTop;
          } else if (!isUndefined__default["default"](props2.offsetBottom) && wrapToTop >= calcBottom) {
            fixedTop = calcBottom;
          } else {
            fixedTop = false;
          }
          if (affixRef.value) {
            var affixed = fixedTop !== false;
            var placeholderStatus = affixWrapRef.value.contains(placeholderEL.value);
            if (affixed) {
              affixRef.value.className = COMPONENT_NAME.value;
              affixStyle.value = {
                top: "".concat(fixedTop, "px"),
                width: "".concat(wrapWidth, "px"),
                height: "".concat(wrapHeight, "px"),
                zIndex: props2.zIndex
              };
              if (!placeholderStatus) {
                placeholderEL.value.style.width = "".concat(wrapWidth, "px");
                placeholderEL.value.style.height = "".concat(wrapHeight, "px");
                affixWrapRef.value.appendChild(placeholderEL.value);
              }
            } else {
              affixRef.value.removeAttribute("class");
              affixStyle.value = void 0;
              placeholderStatus && placeholderEL.value.remove();
            }
            context.emit("fixedChange", affixed, {
              top: Number(fixedTop)
            });
            if (isFunction__default["default"](props2.onFixedChange)) props2.onFixedChange(affixed, {
              top: Number(fixedTop)
            });
          }
          ticking.value = false;
        });
        ticking.value = true;
      }
    };
    var bindScroll = /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
        return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return vue.nextTick();
            case 2:
              if (!binded.value) {
                _context.next = 4;
                break;
              }
              return _context.abrupt("return");
            case 4:
              scrollContainer.value = utils_dom.getScrollContainer(props2.container);
              utils_dom.on(scrollContainer.value, "scroll", handleScroll);
              utils_dom.on(window, "resize", handleScroll);
              binded.value = true;
            case 8:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function bindScroll() {
        return _ref2.apply(this, arguments);
      };
    }();
    var unbindScroll = function unbindScroll() {
      if (!scrollContainer.value || !binded.value) return;
      utils_dom.off(scrollContainer.value, "scroll", handleScroll);
      utils_dom.off(window, "resize", handleScroll);
      binded.value = false;
    };
    vue.watch(function () {
      return props2.offsetTop;
    }, function () {
      handleScroll();
    });
    vue.watch(function () {
      return props2.offsetBottom;
    }, function () {
      handleScroll();
    });
    vue.watch(function () {
      return props2.zIndex;
    }, function () {
      handleScroll();
    });
    vue.onMounted(bindScroll);
    vue.onActivated(bindScroll);
    vue.onDeactivated(unbindScroll);
    vue.onBeforeUnmount(unbindScroll);
    return {
      affixWrapRef: affixWrapRef,
      affixRef: affixRef,
      bindScroll: bindScroll,
      unbindScroll: unbindScroll,
      handleScroll: handleScroll,
      scrollContainer: scrollContainer,
      renderTNodeJSX: renderTNodeJSX,
      affixStyle: affixStyle
    };
  },
  render: function render() {
    return vue.createVNode("div", {
      "ref": "affixWrapRef"
    }, [vue.createVNode("div", {
      "ref": "affixRef",
      "style": this.affixStyle
    }, [this.renderTNodeJSX("default")])]);
  }
});

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