"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const util = require("../lazyload/vue-lazyload/util.js");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const index = require("../icon/index.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const basic = require("../utils/basic.js");
const format = require("../utils/format.js");
const dom = require("../utils/dom.js");
const [name, bem] = create.createNamespace("back-top");
const backTopProps = {
  right: props.numericProp,
  bottom: props.numericProp,
  zIndex: props.numericProp,
  target: [String, Object],
  offset: props.makeNumericProp(200),
  immediate: Boolean,
  teleport: {
    type: [String, Object],
    default: "body"
  }
};
var stdin_default = Vue.defineComponent({
  name,
  inheritAttrs: false,
  props: backTopProps,
  emits: ["click"],
  setup(props2, {
    emit,
    slots,
    attrs
  }) {
    let shouldReshow = false;
    const show = Vue.ref(false);
    const root = Vue.ref();
    const scrollParent = Vue.ref();
    const style = Vue.computed(() => basic.extend(format.getZIndexStyle(props2.zIndex), {
      right: format.addUnit(props2.right),
      bottom: format.addUnit(props2.bottom)
    }));
    const onClick = (event) => {
      var _a;
      emit("click", event);
      (_a = scrollParent.value) == null ? void 0 : _a.scrollTo({
        top: 0,
        behavior: props2.immediate ? "auto" : "smooth"
      });
    };
    const scroll = () => {
      show.value = scrollParent.value ? dom.getScrollTop(scrollParent.value) >= +props2.offset : false;
    };
    const getTarget = () => {
      const {
        target
      } = props2;
      if (typeof target === "string") {
        const el = document.querySelector(target);
        if (el) {
          return el;
        }
      } else {
        return target;
      }
    };
    const updateTarget = () => {
      if (basic.inBrowser) {
        Vue.nextTick(() => {
          scrollParent.value = props2.target ? getTarget() : index_esm.getScrollParent(root.value);
          scroll();
        });
      }
    };
    index_esm.useEventListener("scroll", util.throttle(scroll, 100), {
      target: scrollParent
    });
    Vue.onMounted(updateTarget);
    Vue.onActivated(() => {
      if (shouldReshow) {
        show.value = true;
        shouldReshow = false;
      }
    });
    Vue.onDeactivated(() => {
      if (show.value && props2.teleport) {
        show.value = false;
        shouldReshow = true;
      }
    });
    Vue.watch(() => props2.target, updateTarget);
    return () => {
      const Content = Vue.createVNode("div", Vue.mergeProps({
        "ref": !props2.teleport ? root : void 0,
        "class": bem({
          active: show.value
        }),
        "style": style.value,
        "onClick": onClick
      }, attrs), [slots.default ? slots.default() : Vue.createVNode(index.Icon, {
        "name": "back-top",
        "class": bem("icon")
      }, null)]);
      if (props2.teleport) {
        return [Vue.createVNode("div", {
          "ref": root,
          "class": bem("placeholder")
        }, null), Vue.createVNode(Vue.Teleport, {
          "to": props2.teleport
        }, {
          default: () => [Content]
        })];
      }
      return Content;
    };
  }
});
exports.backTopProps = backTopProps;
exports.default = stdin_default;
