'use strict';

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

var vue = require('vue');
var core = require('@vueuse/core');
var index = require('../../icon/index.js');
var animation = require('../../../utils/animation.js');
var error = require('../../../utils/error.js');
var iconsVue = require('@element-plus/icons-vue');
var backtop = require('./backtop2.js');

const COMPONENT_NAME = "ElBacktop";
var script = vue.defineComponent({
  name: COMPONENT_NAME,
  components: {
    ElIcon: index.ElIcon,
    CaretTop: iconsVue.CaretTop
  },
  props: backtop.backtopProps,
  emits: backtop.backtopEmits,
  setup(props, { emit }) {
    const el = vue.shallowRef(document.documentElement);
    const container = vue.shallowRef(document);
    const visible = vue.ref(false);
    const styleBottom = vue.computed(() => `${props.bottom}px`);
    const styleRight = vue.computed(() => `${props.right}px`);
    const scrollToTop = () => {
      if (!el.value)
        return;
      const beginTime = Date.now();
      const beginValue = el.value.scrollTop;
      const frameFunc = () => {
        if (!el.value)
          return;
        const progress = (Date.now() - beginTime) / 500;
        if (progress < 1) {
          el.value.scrollTop = beginValue * (1 - animation.easeInOutCubic(progress));
          requestAnimationFrame(frameFunc);
        } else {
          el.value.scrollTop = 0;
        }
      };
      requestAnimationFrame(frameFunc);
    };
    const handleScroll = () => {
      if (el.value)
        visible.value = el.value.scrollTop >= props.visibilityHeight;
    };
    const handleClick = (event) => {
      scrollToTop();
      emit("click", event);
    };
    const handleScrollThrottled = core.useThrottleFn(handleScroll, 300);
    vue.onMounted(() => {
      var _a;
      if (props.target) {
        el.value = (_a = document.querySelector(props.target)) != null ? _a : void 0;
        if (!el.value) {
          error.throwError(COMPONENT_NAME, `target is not existed: ${props.target}`);
        }
        container.value = el.value;
      }
      core.useEventListener(container, "scroll", handleScrollThrottled);
    });
    return {
      visible,
      styleBottom,
      styleRight,
      handleClick
    };
  }
});

exports["default"] = script;
//# sourceMappingURL=backtop.vue_vue_type_script_lang.js.map
