/*! bxs-vue-ui v3.3.0 */
import { u as aFunction, i as isObject, $, y as entryVirtual } from '../utils/entry-virtual.js';
import { C as create, _ as __vue_normalize__ } from '../utils/normalize-component.js';
import '../utils/object-create.js';
import '../utils/html.js';
import '../utils/es.number.constructor.js';
import Vue from 'vue';
import '../utils/is-nan.js';
import Loading from '../loading/index.js';

var MIN_DISTANCE = 10;

function getDirection(x, y) {
  if (x > y && x > MIN_DISTANCE) {
    return 'horizontal';
  }

  if (y > x && y > MIN_DISTANCE) {
    return 'vertical';
  }

  return '';
}

var Touch = {
  data: function data() {
    return {
      direction: ''
    };
  },
  methods: {
    touchStart: function touchStart(event) {
      this.resetTouchStatus();
      this.startX = event.touches[0].clientX;
      this.startY = event.touches[0].clientY;
    },
    touchMove: function touchMove(event) {
      var touch = event.touches[0];
      this.deltaX = touch.clientX - this.startX;
      this.deltaY = touch.clientY - this.startY;
      this.offsetX = Math.abs(this.deltaX);
      this.offsetY = Math.abs(this.deltaY);
      this.direction = this.direction || getDirection(this.offsetX, this.offsetY);
    },
    resetTouchStatus: function resetTouchStatus() {
      this.direction = '';
      this.deltaX = 0;
      this.deltaY = 0;
      this.offsetX = 0;
      this.offsetY = 0;
    }
  }
};

var slice = [].slice;
var factories = {};

var construct = function (C, argsLength, args) {
  if (!(argsLength in factories)) {
    for (var list = [], i = 0; i < argsLength; i++) list[i] = 'a[' + i + ']';
    // eslint-disable-next-line no-new-func
    factories[argsLength] = Function('C,a', 'return new C(' + list.join(',') + ')');
  } return factories[argsLength](C, args);
};

// `Function.prototype.bind` method implementation
// https://tc39.github.io/ecma262/#sec-function.prototype.bind
var functionBind = Function.bind || function bind(that /* , ...args */) {
  var fn = aFunction(this);
  var partArgs = slice.call(arguments, 1);
  var boundFunction = function bound(/* args... */) {
    var args = partArgs.concat(slice.call(arguments));
    return this instanceof boundFunction ? construct(fn, args.length, args) : fn.apply(that, args);
  };
  if (isObject(fn.prototype)) boundFunction.prototype = fn.prototype;
  return boundFunction;
};

// `Function.prototype.bind` method
// https://tc39.github.io/ecma262/#sec-function.prototype.bind
$({ target: 'Function', proto: true }, {
  bind: functionBind
});

var bind = entryVirtual('Function').bind;

var FunctionPrototype = Function.prototype;

var bind_1 = function (it) {
  var own = it.bind;
  return it === FunctionPrototype || (it instanceof Function && own === FunctionPrototype.bind) ? bind : own;
};

var bind$1 = bind_1;

var bind$2 = bind$1;

var _context;
var isServer = Vue.prototype.$isServer;
var scrollUtils = {
  getScrollEventTarget: function getScrollEventTarget(element, rootParent) {
    if (rootParent === void 0) {
      rootParent = window;
    }

    var currentNode = element;

    while (currentNode && currentNode.tagName !== 'HTML' && currentNode.tagName !== 'BODY' && currentNode.nodeType === 1 && currentNode !== rootParent) {
      var _this$getComputedStyl = this.getComputedStyle(currentNode),
          overflowY = _this$getComputedStyl.overflowY;

      if (overflowY === 'scroll' || overflowY === 'auto') {
        return currentNode;
      }

      currentNode = currentNode.parentNode;
    }

    return rootParent;
  },
  getScrollTop: function getScrollTop(element) {
    return 'scrollTop' in element ? element.scrollTop : element.pageYOffset;
  },
  getComputedStyle: !isServer && bind$2(_context = document.defaultView.getComputedStyle).call(_context, document.defaultView)
};

var _components;
var script = create({
  name: 'pull-refresh',
  components: (_components = {}, _components[Loading.name] = Loading, _components),
  mixins: [Touch],
  props: {
    value: {
      type: Boolean,
      required: true
    },
    pullingText: {
      type: String,
      default: '下拉即可刷新…'
    },
    loosingText: {
      type: String,
      default: '释放即可刷新…'
    },
    loadingText: {
      type: String,
      default: '正在刷新…'
    },
    animationDuration: {
      type: Number,
      default: 300
    },
    headHeight: {
      type: Number,
      default: 49
    }
  },
  data: function data() {
    return {
      status: 'normal',
      height: 0,
      duration: 0
    };
  },
  computed: {
    style: function style() {
      return {
        transition: this.duration + "ms",
        transform: "translate3d(0," + this.height + "px, 0)"
      };
    },
    untouchable: function untouchable() {
      return this.status === 'loading';
    }
  },
  watch: {
    value: function value(val) {
      this.getStatus(val ? this.headHeight : 0, val);
    }
  },
  mounted: function mounted() {
    this.scrollEl = scrollUtils.getScrollEventTarget(this.$el);
  },
  methods: {
    onTouchStart: function onTouchStart(event) {
      if (this.untouchable) return;

      if (this.getCeiling()) {
        this.duration = 0;
        this.touchStart(event);
      }
    },
    onTouchMove: function onTouchMove(event) {
      if (this.untouchable) return;
      this.touchMove(event);

      if (!this.ceiling && this.getCeiling()) {
        this.duration = 0;
        this.startY = event.touches[0].clientY;
        this.deltaY = 0;
      }

      if (this.ceiling && this.deltaY >= 0) {
        if (this.direction === 'vertical') {
          this.getStatus(this.ease(this.deltaY));
          if (event.cancelable) event.preventDefault();
        }
      }
    },
    onTouchEnd: function onTouchEnd() {
      if (this.untouchable) return;

      if (this.ceiling && this.deltaY) {
        this.duration = this.animationDuration;

        if (this.status === 'loosing') {
          this.getStatus(this.headHeight, true);
          this.$emit('input', true);
          this.$emit('refresh');
        } else {
          this.getStatus(0);
        }
      }
    },
    getCeiling: function getCeiling() {
      this.ceiling = scrollUtils.getScrollTop(this.scrollEl) === 0;
      return this.ceiling;
    },
    ease: function ease(height) {
      var headHeight = this.headHeight;
      var realHeight;

      if (height < headHeight) {
        realHeight = height;
      } else if (height < headHeight * 2) {
        realHeight = Math.round(headHeight + (height - headHeight) / 2);
      } else {
        realHeight = Math.round(headHeight * 1.5 + (height - headHeight * 2) / 4);
      }

      return realHeight;
    },
    getStatus: function getStatus(height, isLoading) {
      this.height = height;
      var status;

      if (isLoading) {
        status = 'loading';
      } else if (height === 0) {
        status = 'normal';
      } else if (height < this.headHeight) {
        status = 'pulling';
      } else {
        status = 'loosing';
      }

      if (status !== this.status) {
        this.status = status;
      }
    }
  }
});

/* script */
var __vue_script__ = script;
/* template */

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

  var _h = _vm.$createElement;

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

  return _c('div', {
    staticClass: "bx-pull-refresh"
  }, [_c('div', {
    staticClass: "bx-pull-refresh-main",
    style: _vm.style,
    on: {
      "touchstart": _vm.onTouchStart,
      "touchmove": _vm.onTouchMove,
      "touchend": _vm.onTouchEnd,
      "touchcancel": _vm.onTouchEnd
    }
  }, [_c('div', {
    staticClass: "bx-pull-refresh-header"
  }, [_vm.status === 'pulling' ? [_c('span', {
    staticClass: "bx-pull-refresh-text"
  }, [_vm._v(_vm._s(_vm.pullingText))])] : _vm._e(), _vm.status === 'loosing' ? [_c('span', {
    staticClass: "bx-pull-refresh-text"
  }, [_vm._v(_vm._s(_vm.loosingText))])] : _vm._e(), _vm.status === 'loading' ? [_c('bxs-loading'), _c('span', {
    staticClass: "bx-pull-refresh-text"
  }, [_vm._v(_vm._s(_vm.loadingText))])] : _vm._e()], 2), _vm._t("default")], 2)]);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var index = __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);

export default index;
