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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _typeof = require('@babel/runtime/helpers/typeof');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var utils_dom = require('../utils/dom.js');
var hooks_useGlobalIcon = require('../hooks/useGlobalIcon.js');
var backTop_props = require('./props.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_slot = require('../hooks/slot.js');
var hooks_tnode = require('../hooks/tnode.js');
require('../_chunks/dep-7f0559c3.js');
require('lodash/isString');
require('lodash/isFunction');
require('lodash/isArray');
require('../utils/easing.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('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 _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);

var _BackTop = vue.defineComponent({
  name: "TBackTop",
  props: backTop_props["default"],
  setup: function setup(props2) {
    var visible = vue.ref(false);
    var containerRef = vue.ref(null);
    var componentName = hooks_useConfig.usePrefixClass("back-top");
    var _useConfig = configProvider_useConfig.useConfig("alert"),
      classPrefix = _useConfig.classPrefix;
    var _useGlobalIcon = hooks_useGlobalIcon.useGlobalIcon({
        BacktopIcon: iconsVueNext.BacktopIcon
      }),
      BacktopIcon = _useGlobalIcon.BacktopIcon;
    var renderContent = hooks_tnode.useContent();
    var getContainer = function getContainer(container) {
      if (typeof container === "string" && (typeof document === "undefined" ? "undefined" : _typeof__default["default"](document)) !== void 0) {
        if (container === "body") return document;
        return document.querySelector(container);
      }
      if (typeof container === "function") return container();
      return null;
    };
    vue.onMounted(function () {
      containerRef.value = getContainer(props2.container);
      var visibleHeight;
      if (typeof props2.visibleHeight === "string") visibleHeight = Number(props2.visibleHeight.replace("px", ""));else visibleHeight = props2.visibleHeight;
      if (visibleHeight === 0) {
        visible.value = true;
        return;
      }
      var scrollDOM;
      if (containerRef.value.scrollTop === void 0) scrollDOM = document.documentElement;else scrollDOM = containerRef.value;
      containerRef.value.onscroll = function () {
        var scrollTop = scrollDOM.scrollTop;
        if (scrollTop >= visibleHeight) {
          visible.value = true;
          containerRef.value.onscroll = null;
        }
      };
    });
    return function () {
      var theme = props2.theme,
        shape = props2.shape,
        size = props2.size,
        target = props2.target,
        duration = props2.duration,
        offset = props2.offset,
        container = props2.container;
      var getChild = hooks_slot.useChildSlots();
      var children = getChild();
      if (children.length < 1) children = null;
      var cls = vue.computed(function () {
        var _ref;
        return _ref = {}, _defineProperty__default["default"](_ref, componentName.value, true), _defineProperty__default["default"](_ref, "".concat(componentName.value, "--theme-").concat(theme), true), _defineProperty__default["default"](_ref, "".concat(componentName.value, "--").concat(shape), true), _defineProperty__default["default"](_ref, "".concat(componentName.value, "--show"), visible.value), _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-size-s"), size === "small"), _defineProperty__default["default"](_ref, "".concat(classPrefix.value, "-size-m"), size === "medium"), _ref;
      });
      var defaultContent = vue.createVNode(vue.Fragment, null, [vue.createVNode(BacktopIcon, {
        "class": "".concat(componentName.value, "__icon"),
        "size": "24"
      }, null), vue.createVNode("span", {
        "class": "".concat(componentName.value, "__text")
      }, [vue.createTextVNode("TOP")])]);
      var getBackTo = function getBackTo() {
        if (target === container) return 0;
        if (target === "body") return 0;
        if (!target) return 0;
        var targetNode = getContainer(target);
        if (!targetNode) return 0;
        var y = targetNode.getBoundingClientRect().y;
        return y;
      };
      var handleClick = function handleClick(e) {
        var _props2$onClick;
        var y = getBackTo();
        utils_dom.scrollTo(y, {
          container: containerRef.value,
          duration: duration
        });
        (_props2$onClick = props2.onClick) === null || _props2$onClick === void 0 ? void 0 : _props2$onClick.call(props2, {
          e: e
        });
      };
      var positionStyle = vue.computed(function () {
        return {
          insetInlineEnd: offset[0],
          insetBlockEnd: offset[1]
        };
      });
      return vue.createVNode("button", {
        "type": "button",
        "class": cls.value,
        "style": positionStyle.value,
        "onClick": handleClick
      }, [renderContent("content", "default", defaultContent)]);
    };
  }
});

exports["default"] = _BackTop;
//# sourceMappingURL=back-top.js.map
