"use strict";

exports.__esModule = true;
exports.default = void 0;

require("./index-sfc.css");

// 调试开关
var DEBUG = process.env.NODE_ENV === 'development'; // 数据填充

var getPageData = function getPageData(currentIndex, totalCount, list) {
  var result = [];

  var DataFiller = function DataFiller(start, count, dataList) {
    result = result.concat(dataList.slice(start, start + count)); // 差距

    var disparityNum = totalCount - result.length;

    if (disparityNum > 0) {
      DataFiller(0, disparityNum, dataList);
    }
  };

  DataFiller(currentIndex, totalCount, list);
  return result;
};

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

  var _h = _vm.$createElement;

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

  return _c('div', {
    ref: "ScrollList",
    staticClass: "ScrollList"
  }, [_c('div', {
    staticClass: "_wrap"
  }, [_c('ul', {
    staticClass: "_page _page_before"
  }, _vm._l(_vm.page1Data, function (item, index) {
    return _c('li', {
      key: '_page_item' + index,
      staticClass: "_item"
    }, [_vm._t("default", function () {
      return [_vm._v(_vm._s(item))];
    }, {
      "row": item,
      "isCur": index === (_vm.canMoveOn ? 0 : _vm.currentIndex)
    })], 2);
  }), 0), _vm._v(" "), _c('ul', {
    staticClass: "_page _page_after"
  }, _vm._l(_vm.page2Data, function (item, index) {
    return _c('li', {
      key: '_page2_item' + index,
      staticClass: "_item",
      class: [index % 2 === 0 ? 'odd' : 'even']
    }, [_vm._t("default", function () {
      return [_vm._v(_vm._s(item))];
    }, {
      "row": item
    })], 2);
  }), 0)])]);
};

var __vue_staticRenderFns__ = [];
var _default2 = {
  _scopeId: 'data-v-14c6aa0e',
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: 'scroll-list',
  props: {
    list: {
      type: Array,
      required: false,
      default: function _default() {
        return [];
      }
    },
    pageSize: {
      // 展示条数
      type: Number,
      default: 10
    },
    defaultIndex: {
      // 默认展示第几条
      type: Number,
      default: 0
    },
    interval: {
      type: Number,
      default: 4000
    },
    duration: {
      type: Number,
      default: 500
    },
    loop: {
      // 列表是否循环
      type: Boolean,
      default: true
    }
  },
  data: function data() {
    return {
      itemHeight: 0,
      currentIndex: 0,
      timehandler: null,
      page1Data: [],
      page2Data: [],
      canMoveOn: true
    };
  },
  watch: {
    defaultIndex: {
      handler: function handler() {
        this.currentIndex = this.defaultIndex;
      },
      immediate: true
    },
    list: {
      handler: function handler() {
        if (this.list.length && !this.itemHeight) {
          this.init();
        }
      },
      immediate: true
    },
    currentIndex: function currentIndex() {
      if (this.canMoveOn) {
        this.moveOn();
      }
    }
  },
  methods: {
    setPageData: function setPageData() {
      // 第一页
      this.page1Data = getPageData(this.currentIndex, this.pageSize, this.list); // 下一页

      var nextPageIndex = (this.currentIndex + this.pageSize) % this.list.length;
      this.page2Data = getPageData(nextPageIndex, this.pageSize, this.list); // 强制刷新

      this.$forceUpdate();
    },
    moveOn: function moveOn() {
      var _this = this;

      var wrapDom = this.$refs.ScrollList.querySelector('._wrap');

      if (wrapDom) {
        // transition: all ease 0.5s
        wrapDom.style.transition = "all ease " + this.duration + "ms";
        wrapDom.style.transform = "translateY(-" + this.itemHeight + "px)";
      } else {
        DEBUG && console.warn('找不到 wrapDom');
      }

      setTimeout(function () {
        _this.setPageData();

        wrapDom.style.transition = "none";
        wrapDom.style.transform = ""; // 回调事件

        _this.$emit('animateEnd', _this.page1Data);
      }, this.duration);
    },
    init: function init() {
      var _this2 = this;

      // 初始数据
      if (this.pageSize > this.list.length && !this.loop) {
        this.canMoveOn = false;
        DEBUG && console.log('scrooList: 数据不足，无法滚动');
        this.page1Data = this.list;
      } else {
        this.setPageData();
      } // 获取元素高度


      this.$nextTick(function () {
        var itemDom = _this2.$refs.ScrollList.querySelector("._page_before ._item");

        if (itemDom) {
          DEBUG && console.log("itemDom\u9AD8\u5EA6", itemDom.offsetHeight);
          _this2.itemHeight = itemDom.offsetHeight;
        } else {
          DEBUG && console.warn("\u627E\u4E0D\u5230 ._item \u5143\u7D20");
        }
      }); // 设置定时器

      if (this.canMoveOn && this.interval) {
        this.timehandler = setInterval(function () {
          if (_this2.currentIndex < _this2.list.length - 1) {
            _this2.currentIndex++;
          } else {
            _this2.currentIndex = 0;
          }
        }, this.interval);
      }
    }
  }
};
exports.default = _default2;