/*! bxs-vue-ui v3.3.0 */
import { $, p as path } 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 '../utils/array-iteration.js';
import '../utils/sloppy-array-method.js';
import { _ as _forEachInstanceProperty } from '../utils/for-each.js';
import '../utils/index-of.js';
import { E as Env } from '../utils/env.js';
import '../utils/es.string.match.js';

// `Date.now` method
// https://tc39.github.io/ecma262/#sec-date.now
$({ target: 'Date', stat: true }, {
  now: function now() {
    return new Date().getTime();
  }
});

var now = path.Date.now;

var now$1 = now;

var now$2 = now$1;

// 嗅探transform可支持的前缀
var elementStyle = document.createElement('div').style;

var vendor = function getVendor() {
  var vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT'];
  var transform;
  var i = 0;
  var l = vendors.length;

  for (; i < l; i += 1) {
    transform = vendors[i] + "ransform";
    if (transform in elementStyle) return vendors[i].substr(0, vendors[i].length - 1);
  }

  return false;
}();

var prefixStyle = function prefixStyle(style) {
  if (vendor === false) return false;
  if (vendor === '') return style;
  return vendor + style.charAt(0).toUpperCase() + style.substr(1);
};

var transition = {
  prefixStyle: prefixStyle,
  transform: prefixStyle('transform'),
  transformProperty: prefixStyle('transitionProperty'),
  transitionDuration: prefixStyle('transitionDuration'),
  transitionTimingFunction: prefixStyle('transitionTimingFunction'),

  /* eslint no-param-reassign: ["error", { "props": false }] */
  // 设置transition属性
  setProperty: function setProperty(obj, property) {
    obj.style[prefixStyle('transitionProperty')] = property;
  },
  // 页面滑动到指定位置
  setTransform: function setTransform(obj, x, y) {
    obj.style[prefixStyle('transform')] = "translate3d(" + x + "px," + y + "px, 0px)";
  },
  // 设置transition间隔时间
  setDuration: function setDuration(obj, time) {
    obj.style[prefixStyle('transitionDuration')] = time + "ms";
  },
  // 设置scale
  setScale: function setScale(obj, scale) {
    obj.style[prefixStyle('transform')] = "scale(" + scale + ")";
  },
  // 设置transition的动画贝塞尔曲线
  setTimingFunction: function setTimingFunction(obj, easing) {
    obj.style[prefixStyle('transitionTimingFunction')] = easing;
  }
};

var dom = {
  hasClass: function hasClass($dom, className) {
    return $dom.className.match(new RegExp(className));
  },

  /* eslint no-param-reassign: ["error", { "props": false }] */
  addClass: function addClass($dom, className) {
    if (!this.hasClass($dom, className)) {
      $dom.className += " " + className;
    }
  },
  removeClass: function removeClass($dom, className) {
    if (this.hasClass($dom, className)) {
      var reg = new RegExp("(\\s|^)" + className);
      $dom.className = $dom.className.replace(reg, '');
    }
  }
};

var props = {
  // 轮播间隔时间
  speed: {
    type: Number,
    default: 5000
  },
  // 轮播动画时间
  transitionSpeed: {
    type: Number,
    default: 300
  },
  // 是否循环轮播
  loop: {
    type: Boolean,
    default: true
  },
  // 是否自动轮播
  autoplay: {
    type: Boolean,
    default: true
  },
  // 是否展示指示器/分页器
  pagination: {
    type: Boolean,
    default: true
  },
  // 页数大于当前值时,才展示分页器
  paginationLimit: {
    type: Number,
    default: 1
  },
  // 覆盖默认指示器/分页器样式
  paginationClass: {
    type: String,
    default: null
  },
  // 页数大于当前值时,才展示分页器
  angle: {
    type: Number,
    default: 45
  },
  // 滑动结束后,时间小于{duration}毫秒并且距离大于{distance},即判定为滑动到上一个/下一个
  duration: {
    type: Number,
    default: 300
  },
  distance: {
    type: Number,
    default: 30
  },
  // 是否开启回弹
  bounce: {
    type: Boolean,
    default: true
  }
};

var touchEvents = {
  start: Env.supportTouch ? 'touchstart' : 'mousedown',
  move: Env.supportTouch ? 'touchmove' : 'mousemove',
  end: Env.supportTouch ? 'touchend' : 'mouseup'
};
var timer = null;
var script = create({
  name: 'swiper',
  props: props,
  data: function data() {
    return {
      width: 0,
      currentIndex: 0,
      // 当前活动项
      count: 0,
      // 子元素个数
      touches: {
        startX: 0,
        // touch前一个点
        startY: 0,
        // touch前一个点
        currentX: 0,
        // touch后一个点
        currentY: 0,
        // touch后一个点
        distanceX: 0,
        // 两个touch之间的距离
        distanceY: 0 // 两个touch之间的距离

      },
      offset: {
        x: 0,
        // 横向滑动距离
        y: 0,
        // 纵向滑动距离
        distance: 0 // 滑动的距离

      },
      direction: null,
      // 滑动的方向
      startTime: null,
      // 控制滑动的时间不能太长
      endTime: null,
      // 控制滑动的时间不能太长
      isPagination: this.pagination,
      // 是否展示分页指示器
      isLoop: this.loop,
      // 是否循环滚动
      isBounce: this.bounce,
      // 是否回弹
      isTouch: false,
      // 是否点击
      isSliding: false,
      isTouchStart: false,
      isTouchEnd: false
    };
  },
  methods: {
    init: function init() {
      this.element = this.$el;
      this.scroller = this.$el.firstChild;
      this.width = this.element.clientWidth; // 防止swiper元素上设置了小数点宽度，而clientWidth取出来是整数

      this.scroller.style.width = this.width + "px";
      this.count = this.$children.length; // 当设置了分页指示器，并且数量大于指定数量，才展示指示器

      this.isPagination = this.pagination && this.count > this.paginationLimit; // 当数量为1时，自动取消循环

      this.isLoop = this.loop && this.count > 1;
      this.setIndex();

      if (this.isLoop) {
        // 循环滚动的数量
        this.loopCount = this.count + 2;
        this.createLoop();
      }

      if (this.autoplay && this.count > 1) {
        this.startAutoPlay();
      }
    },
    // 是否可以回弹
    canBounce: function canBounce() {
      var result = false;

      if (this.bounce && !this.isLoop) {
        if (this.currentIndex === 0 && this.direction === 'right') {
          result = true;
        }

        if (this.currentIndex === this.count - 1 && this.direction === 'left') {
          result = true;
        }
      }

      return result;
    },
    // 动态为每个项设置index
    setIndex: function setIndex() {
      var _context;

      _forEachInstanceProperty(_context = this.$children).call(_context, function (item, index) {
        /* eslint no-param-reassign: ["error", { "props": false }] */
        item.$data.index = index;
      });
    },
    // 更新index位置
    updateIndex: function updateIndex() {
      if (this.isLoop) {
        this.slideTo(-1 * (this.currentIndex + 1) * this.width, 0, 0);
      } else {
        this.slideTo(-1 * this.currentIndex * this.width, 0, 0);
      }
    },
    // 创建无限循环
    createLoop: function createLoop() {
      var firstNode = this.$children[0].$el.cloneNode(true);
      var lastNode = this.$children[this.count - 1].$el.cloneNode(true);
      dom.removeClass(firstNode, 'bx-swiper-item--active');
      dom.removeClass(lastNode, 'bx-swiper-item--active');
      this.scroller.insertBefore(lastNode, this.$children[0].$el);
      this.scroller.appendChild(firstNode); // 定位到第一个slideitem

      this.translate(-1 * this.width, 0);
    },
    // 处理循环滚动位置
    handleLoop: function handleLoop() {
      if (this.offset.x === 0) {
        this.slideTo(-1 * this.count * this.width, 0, 0);
      }

      if (this.offset.x === -1 * (this.loopCount - 1) * this.width) {
        this.slideTo(-1 * this.width, 0, 0);
      }
    },

    /* eslint-disable consistent-return */
    // 开启自动播放
    startAutoPlay: function startAutoPlay() {
      var _this = this;

      timer = window.setInterval(function () {
        if (_this.isTouchStart) {
          if (_this.isTouchEnd) {
            _this.isTouchStart = false;
            _this.isTouchEnd = false;
          }

          return false;
        }

        _this.next();
      }, this.speed);
    },
    addEvent: function addEvent() {
      var $element = this.element;
      var $scroller = this.scroller; // touchStart、touchMove、touchEnd事件

      $element.addEventListener(touchEvents.start, this.handleEvent, false);
      $element.addEventListener(touchEvents.move, this.handleEvent, false);
      $element.addEventListener(touchEvents.end, this.handleEvent, false); // 兼容PC
      // if (Env.supportTouch && !Env.isIos && !Env.isAndroid) {
      //   this.wrapper['addEventListener']('mousedown', this.handleEvent, false);
      //   this.wrapper['addEventListener']('mousemove', this.handleEvent, false);
      //   this.wrapper['addEventListener']('mouseup', this.handleEvent, false);
      // }
      // 动画结束事件

      $scroller.addEventListener('transitionend', this.handleEvent, false);
      $scroller.addEventListener('webkitTransitionEnd', this.handleEvent, false); // 窗口resize事件

      window.addEventListener('resize', this.handleEvent, false);
    },
    handleEvent: function handleEvent(e) {
      // if (e.originalEvent) e = e.originalEvent;
      switch (e.type) {
        case 'touchstart':
          this.onTouchStart(e);
          break;

        case 'touchmove':
          this.onTouchMove(e);
          break;

        case 'touchend':
          this.onTouchEnd(e);
          break;

        case 'mousedown':
          this.onTouchStart(e);
          break;

        case 'mousemove':
          this.onTouchMove(e);
          break;

        case 'mouseup':
          this.onTouchEnd(e);
          break;

        case 'transitionend':
          this.transitionEnd(e);
          break;

        case 'webkitTransitionEnd':
          this.transitionEnd(e);
          break;

        case 'resize':
          this.onResize(e);
          break;

        default:
          break;
      }
    },
    onTouchStart: function onTouchStart(e) {
      // 大于一个touch点,不做处理
      if (e.touches && e.touches.length > 1) {
        e.preventDefault();
        return false;
      }

      this.touches.startX = e.type === 'touchstart' ? e.touches[0].pageX : e.pageX;
      this.touches.startY = e.type === 'touchstart' ? e.touches[0].pageY : e.pageY;
      this.isTouch = true;
      this.isSliding = false;
      this.offset.distance = 0;
      this.startTime = now$2();
      this.isTouchStart = true;
      transition.setDuration(this.scroller, 0);
      this.$emit('touch-start', e);
    },
    onTouchMove: function onTouchMove(e) {
      // 大于一个touch点,不做处理
      if (!this.isTouch && e.type === 'mousemove' || e.touches && e.touches.length > 1) {
        e.preventDefault();
        return false;
      } // this.isLoop && this.handleLoop();


      this.touches.currentX = e.type === 'touchmove' ? e.touches[0].pageX : e.pageX;
      this.touches.currentY = e.type === 'touchmove' ? e.touches[0].pageY : e.pageY;
      var touchAngle = Math.atan2(Math.abs(this.touches.currentY - this.touches.startY), Math.abs(this.touches.currentX - this.touches.startX)) * 180 / Math.PI; // 滑动的角度小于{angle}才能正常滑动

      if (!this.isSliding) {
        this.isSliding = touchAngle <= this.angle;
      }

      if (!this.isSliding) {
        return false;
      }

      this.touches.distanceX = this.touches.currentX - this.touches.startX;
      this.touches.distanceY = this.touches.currentY - this.touches.startY;
      this.direction = this.touches.distanceX >= 0 ? 'right' : 'left';
      this.isBounce = this.canBounce();
      this.touches.distanceX = this.isBounce ? this.touches.distanceX / 2 : this.touches.distanceX;
      this.offset.x += this.touches.distanceX;
      this.offset.distance += this.touches.distanceX;
      this.translate(this.offset.x, 0);
      this.touches.startX = this.touches.currentX;
      this.touches.startY = this.touches.currentY;
      e.preventDefault();
      this.$emit('touch-move', e);
    },

    /* eslint-enable consistent-return */
    onTouchEnd: function onTouchEnd(e) {
      this.endTime = now$2();
      var duration = this.endTime - this.startTime;

      if (!this.isBounce && (duration <= this.duration && Math.abs(this.offset.distance) > this.distance || Math.abs(this.offset.distance) > this.width / 2)) {
        if (this.offset.distance > 0) {
          this.prev();
        } else {
          this.next();
        }
      } else {
        this.slideTo(this.offset.x - this.offset.distance, 0);
      }

      this.isTouch = false;
      this.isTouchEnd = true;
      this.$emit('touch-end', e);
    },
    transitionEnd: function transitionEnd(e) {
      if (this.isLoop) {
        this.handleLoop();
      }

      this.$emit('transition-end', e);
    },
    onResize: function onResize() {
      this.width = this.element.clientWidth;
      this.updateIndex();
    },
    // 滚动到指定的x轴/y轴位置, time为动画时间
    slideTo: function slideTo(x, y, time) {
      /* eslint-disable no-param-reassign */
      if (time !== 0) {
        time = this.transitionSpeed;
      }

      transition.setProperty(this.scroller, transition.prefixStyle('transform'));
      transition.setDuration(this.scroller, time);
      this.translate(x, y);
    },
    // 滚动到指定的index
    slideToIndex: function slideToIndex(index, type) {
      if (this.loop) {
        if (type === 'prev') {
          if (index === this.count - 1) {
            this.slideTo(0, 0);
          } else {
            this.slideTo(-1 * (index + 1) * this.width, 0);
          }
        } else if (index === 0) {
          this.slideTo(-1 * (this.loopCount - 1) * this.width, 0);
        } else {
          this.slideTo(-1 * (index + 1) * this.width, 0);
        }
      } else {
        this.slideTo(-1 * index * this.width, 0);
      }

      this.$emit('slide', index);
    },
    // 滑动到上一个
    prev: function prev() {
      this.currentIndex -= 1;

      if (this.currentIndex < 0) {
        this.currentIndex = this.count - 1;
      }

      this.slideToIndex(this.currentIndex, 'prev');
      this.$emit('slide-prev', this.currentIndex);
    },
    // 滑动到下一个
    next: function next() {
      this.currentIndex += 1;

      if (this.currentIndex >= this.count) {
        this.currentIndex = 0;
      }

      this.slideToIndex(this.currentIndex, 'next');
      this.$emit('slide-next', this.currentIndex);
    },
    // 页面滑动到指定位置

    /* eslint-disable no-param-reassign */
    translate: function translate(x, y) {
      if (!this.bounce) {
        if (x > 0) {
          x = 0;
        }

        var total = this.loop ? this.loopCount : this.count;
        var totalDistance = (total - 1) * this.width;

        if (Math.abs(x) > totalDistance) {
          x = -1 * totalDistance;
        }
      }

      this.offset.x = x;
      transition.setTransform(this.scroller, x, y);
    }
  },
  mounted: function mounted() {
    this.init();
    this.addEvent();
  },
  destroyed: function destroyed() {
    if (timer) {
      window.clearInterval(timer);
    }
  }
});

/* 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-swiper"
  }, [_c('div', {
    staticClass: "bx-swiper-scroller"
  }, [_vm._t("default")], 2), _vm.isPagination ? _c('div', {
    staticClass: "bx-swiper-paginations",
    class: _vm.paginationClass
  }, _vm._l(_vm.count, function (key) {
    return _c('div', {
      key: key,
      staticClass: "bx-swiper-pagination bx-swiper-pagination--translation",
      class: key - 1 == _vm.currentIndex && 'bx-swiper-pagination--active'
    });
  })) : _vm._e()]);
};

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;
