"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _comp = require("../../ui/src/comp");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _ui = require("../../ui");
var _log = require("../../ui/src/log");
var _utils = require("../../ui/src/utils");
var _dom = require("../../ui/src/dom");
var _button = _interopRequireDefault(require("../../button"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var _createComponentLog = (0, _log.createComponentLog)('backtop'),
  errLog = _createComponentLog.errLog;
function createInternalData() {
  return {
    targetEl: null
  };
}
function createReactData() {
  return {
    showBtn: false,
    backtopZindex: 0
  };
}
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeBacktop',
  mixins: [_ui.globalMixins.sizeMixin],
  props: {
    target: String,
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.size || (0, _ui.getConfig)().size;
      }
    },
    circle: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.circle;
      }
    },
    right: {
      type: [String, Number],
      default: function _default() {
        return (0, _ui.getConfig)().backtop.right;
      }
    },
    bottom: {
      type: [String, Number],
      default: function _default() {
        return (0, _ui.getConfig)().backtop.bottom;
      }
    },
    status: {
      type: [String, Number],
      default: function _default() {
        return (0, _ui.getConfig)().backtop.status;
      }
    },
    icon: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.icon;
      }
    },
    showIcon: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.showIcon;
      }
    },
    content: {
      type: [String, Number],
      default: function _default() {
        return (0, _ui.getConfig)().backtop.content;
      }
    },
    showContent: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.showContent;
      }
    },
    showTop: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.showTop;
      }
    },
    showBottom: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.showBottom;
      }
    },
    shadow: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.shadow;
      }
    },
    zIndex: {
      type: [String, Number],
      default: function _default() {
        return (0, _ui.getConfig)().backtop.zIndex;
      }
    },
    threshold: {
      type: [String, Number],
      default: function _default() {
        return (0, _ui.getConfig)().backtop.threshold;
      }
    },
    position: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().backtop.position;
      }
    }
  },
  data: function data() {
    var xID = _xeUtils.default.uniqueId();
    var reactData = createReactData();
    return Object.assign(Object.assign({}, {}), {
      xID: xID,
      reactData: reactData
    });
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeWrapperStyle: function computeWrapperStyle() {
      var $xeBacktop = this;
      var props = $xeBacktop;
      var reactData = $xeBacktop.reactData;
      var right = props.right,
        bottom = props.bottom;
      var backtopZindex = reactData.backtopZindex;
      var stys = {};
      if (right) {
        stys.right = (0, _dom.toCssUnit)(right);
      }
      if (bottom) {
        stys.bottom = (0, _dom.toCssUnit)(bottom);
      }
      if (backtopZindex) {
        stys.zIndex = backtopZindex;
      }
      return stys;
    }
  }),
  watch: {
    position: function position() {
      var $xeBacktop = this;
      $xeBacktop.updateZIndex();
    },
    target: function target() {
      var $xeBacktop = this;
      $xeBacktop.handleTargetElement();
    }
  },
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeBacktop = this;
      $xeBacktop.$emit(type, (0, _ui.createEvent)(evnt, {
        $backtop: $xeBacktop
      }, params));
    },
    updateZIndex: function updateZIndex() {
      var $xeBacktop = this;
      var props = $xeBacktop;
      var reactData = $xeBacktop.reactData;
      var position = props.position,
        zIndex = props.zIndex;
      var backtopZindex = reactData.backtopZindex;
      if (zIndex) {
        reactData.backtopZindex = _xeUtils.default.toNumber(zIndex);
      } else if (position === 'fixed') {
        if (backtopZindex < (0, _utils.getLastZIndex)()) {
          reactData.backtopZindex = (0, _utils.nextZIndex)();
        }
      }
    },
    showBacktop: function showBacktop() {
      var $xeBacktop = this;
      var reactData = $xeBacktop.reactData;
      $xeBacktop.updateZIndex();
      reactData.showBtn = true;
    },
    hideBacktop: function hideBacktop() {
      var $xeBacktop = this;
      var reactData = $xeBacktop.reactData;
      reactData.showBtn = false;
    },
    handleScrollEvent: function handleScrollEvent(evnt) {
      var $xeBacktop = this;
      var props = $xeBacktop;
      var reactData = $xeBacktop.reactData;
      var threshold = props.threshold;
      var currentEl = evnt.currentTarget;
      var scrollTop = currentEl.scrollTop;
      reactData.showBtn = scrollTop > Math.max(1, _xeUtils.default.toNumber(threshold));
    },
    handleToTop: function handleToTop() {
      var $xeBacktop = this;
      var internalData = $xeBacktop.internalData;
      var targetEl = internalData.targetEl;
      if (!targetEl) {
        return;
      }
      var scrollTop = targetEl.scrollTop;
      if (scrollTop > 0) {
        requestAnimationFrame($xeBacktop.handleToTop);
        var currScrollTop = scrollTop - Math.max(12, scrollTop / 6);
        targetEl.scrollTop = currScrollTop > 10 ? currScrollTop : 0;
      }
    },
    removeScrollEvent: function removeScrollEvent() {
      var $xeBacktop = this;
      var internalData = $xeBacktop.internalData;
      var targetEl = internalData.targetEl;
      if (targetEl) {
        targetEl.removeEventListener('scroll', $xeBacktop.handleScrollEvent);
      }
    },
    addScrollEvent: function addScrollEvent() {
      var $xeBacktop = this;
      var internalData = $xeBacktop.internalData;
      var targetEl = internalData.targetEl;
      if (targetEl) {
        targetEl.addEventListener('scroll', $xeBacktop.handleScrollEvent, {
          passive: true
        });
      }
    },
    handleTargetElement: function handleTargetElement() {
      var $xeBacktop = this;
      var props = $xeBacktop;
      var internalData = $xeBacktop.internalData;
      $xeBacktop.$nextTick(function () {
        var target = props.target;
        if (!target) {
          $xeBacktop.removeScrollEvent();
          errLog('vxe.error.reqProp', ['target']);
          return;
        }
        if (_xeUtils.default.isString(target)) {
          var tEl = document.querySelector(target);
          if (!tEl) {
            errLog('vxe.error.errProp', ["target=".concat(target), 'body']);
          }
          var targetEl = internalData.targetEl;
          if (targetEl !== tEl) {
            $xeBacktop.removeScrollEvent();
            internalData.targetEl = tEl;
            $xeBacktop.addScrollEvent();
          }
        }
      });
    },
    clickEvent: function clickEvent(evnt) {
      var $xeBacktop = this;
      $xeBacktop.handleToTop();
      $xeBacktop.dispatchEvent('click', {}, evnt);
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeBacktop = this;
      var props = $xeBacktop;
      var slots = $xeBacktop.$scopedSlots;
      var reactData = $xeBacktop.reactData;
      var circle = props.circle,
        position = props.position,
        status = props.status,
        icon = props.icon,
        showIcon = props.showIcon,
        content = props.content,
        showContent = props.showContent,
        showTop = props.showTop,
        showBottom = props.showBottom,
        shadow = props.shadow;
      var showBtn = reactData.showBtn;
      var wrapperStyle = $xeBacktop.computeWrapperStyle;
      var vSize = $xeBacktop.computeSize;
      var defaultSlot = slots.default;
      var topSlot = slots.top;
      var bottomSlot = slots.bottom;
      return h('div', {
        ref: 'refElem',
        class: ['vxe-backtop', position === 'fixed' ? 'is--' + position : 'is--absolute', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--visible', showBtn)],
        style: wrapperStyle
      }, [showTop && topSlot ? h('div', {
        class: 'vxe-backtop--top-wrapper'
      }, topSlot({})) : (0, _ui.renderEmptyElement)($xeBacktop), h('div', {
        class: 'vxe-backtop--content-wrapper',
        on: {
          click: $xeBacktop.clickEvent
        }
      }, [defaultSlot ? defaultSlot({}) : [h(_button.default, {
        props: {
          circle: circle,
          status: status,
          shadow: shadow,
          icon: showIcon ? icon || (0, _ui.getIcon)().BACKTOP_TOP : '',
          content: showContent ? content : ''
        }
      })]]), showBottom && bottomSlot ? h('div', {
        class: 'vxe-backtop--bottom-wrapper'
      }, bottomSlot({})) : (0, _ui.renderEmptyElement)($xeBacktop)]);
    }
  },
  created: function created() {
    var $xeBacktop = this;
    $xeBacktop.internalData = createInternalData();
  },
  mounted: function mounted() {
    var $xeBacktop = this;
    var props = $xeBacktop;
    var showTop = props.showTop;
    if (showTop) {
      $xeBacktop.updateZIndex();
    }
    $xeBacktop.handleTargetElement();
  },
  beforeDestroy: function beforeDestroy() {
    var $xeBacktop = this;
    var reactData = $xeBacktop.reactData;
    var internalData = $xeBacktop.internalData;
    $xeBacktop.removeScrollEvent();
    _xeUtils.default.assign(reactData, createReactData());
    _xeUtils.default.assign(internalData, createInternalData());
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */