import "./index.css";
/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
import { createComponentName, componentMixin } from '../../utils/base.js';
import throttle from 'lodash/throttle';
import { getElementScrollView, on, off } from '../../utils/dom';
import Icon from '../icon';
const __vue_script__ = {
  name: createComponentName('loadmore'),
  components: {
    Icon
  },
  mixins: [componentMixin],
  props: {
    /**
     * 距离底部多少开始加载
     */
    offset: {
      type: Number,
      default: 20
    },
    // 等待多久开始显示
    waitTime: {
      type: Number,
      default: 100
    },
    // 返回Promise<boolean> 当返回 false 的时候表示没有更多数据了, 不在展示
    loadMore: {
      type: Function,
      required: true
    }
  },

  data() {
    return {
      loading: false,
      error: false,
      hasMore: true
    };
  },

  computed: {
    scrollDom() {
      return getElementScrollView(this.$el);
    }

  },

  mounted() {
    this.addScrollListener();
    this.scrollListener();
  },

  beforeDestroy() {
    this.unbindEventListener();
  },

  methods: {
    addScrollListener() {
      const fn = throttle(this.scrollListener, this.waitTime);

      this.unbindEventListener = () => off(this.scrollDom, 'scroll', fn);

      on(this.scrollDom, 'scroll', fn);
    },

    async scrollListener() {
      if (this.loading) return;
      const scrollDom = getElementScrollView(this.$el);
      let needLoadMore = false;
      this.error = false;

      if (scrollDom === window) {
        let scrollTop = 0;
        if (document.body) scrollTop = document.body.scrollTop;
        scrollTop = Math.max(window.pageYOffset || 0, scrollTop);

        if (document.documentElement && document.documentElement.clientHeight + scrollTop >= document.documentElement.scrollHeight - this.offset) {
          needLoadMore = true;
        }
      } else if (scrollDom.clientHeight + scrollDom.scrollTop >= scrollDom.scrollHeight - this.offset) {
        needLoadMore = true;
      }

      if (needLoadMore) {
        try {
          this.error = false;
          this.loading = true;
          this.hasMore = await this.loadMore();
        } catch (error) {
          this.error = true;
        } finally {
          this.loading = false;
        }

        if (!this.hasMore) {
          this.unbindEventListener();
        } else if (!this.error) {
          await this.scrollListener();
        }
      }
    }

  }
};
/* template */

var __vue_render__ = function () {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("div", {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.hasMore,
      expression: "hasMore"
    }],
    class: _vm.bem()
  }, [!_vm.error ? _vm._t("loading", [_c("Icon", {
    attrs: {
      name: "loading",
      spin: ""
    }
  }), _vm._v(" "), _c("span", [_vm._v("玩命加载中...")])], {
    loading: _vm.loading
  }) : _c("div", {
    class: _vm.bem("error")
  }, [_vm._t("error", [_c("span", {
    on: {
      click: _vm.scrollListener
    }
  }, [_vm._v("加载失败点击重试")])], {
    refresh: _vm.scrollListener
  })], 2)], 2);
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

const __vue_inject_styles__ = undefined;
/* scoped */

const __vue_scope_id__ = undefined;
/* module identifier */

const __vue_module_identifier__ = undefined;
/* functional template */

const __vue_is_functional_template__ = false;
/* component normalizer */

import __vue_normalize__ from '../../utils/vue-normalize-component.js';
/* style inject */

/* style inject SSR */

export default __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);