/*! bxs-vue-ui v3.3.0 */
import '../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/string-trim.js';
import { _ as _parseInt } from '../utils/parse-int.js';
import '../utils/es.number.to-fixed.js';

var script = create({
  name: 'tab',
  props: {
    index: {
      type: Number,
      default: 0
    }
  },
  data: function data() {
    return {
      currentIndex: this.index,
      isAdaptive: false,
      itemWidth: 0,
      lineWidth: 0,
      style: {}
    };
  },
  watch: {
    index: function index(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.currentIndex = this.index;
        this.calculateLineStyle();
        this.$emit('change', this.index);
      }
    }
  },
  methods: {
    init: function init() {
      var _context,
          _context2,
          _this = this;

      if (this.currentIndex >= this.$children.length) {
        this.currentIndex = this.$children.length - 1;
      }

      if (this.currentIndex < 0) {
        this.currentIndex = 0;
      } // 如果宽度小于屏幕宽度，则自适应


      var totalWidth = 0;

      _forEachInstanceProperty(_context = this.$children).call(_context, function (item) {
        totalWidth += item.$el.clientWidth;
      });

      if (totalWidth < this.$el.clientWidth) {
        this.isAdaptive = true;
      } // 动态为每个项设置index


      _forEachInstanceProperty(_context2 = this.$children).call(_context2, function (item, index) {
        /* eslint no-param-reassign: ["error", { "props": false }] */
        item.$data.index = index;
      }); // 线条宽度


      this.lineWidth = this.$el.childNodes[0].lastChild.clientWidth; // 渲染结束后，再动态设置line的样式

      this.$nextTick(function () {
        _this.calculateLineStyle();
      });
    },
    // 计算style
    calculateLineStyle: function calculateLineStyle() {
      // tab项宽度
      this.itemWidth = this.$children[this.currentIndex].$el.clientWidth;
      var left = this.$children[this.currentIndex].$el.offsetLeft + (this.itemWidth - this.lineWidth) / 2;
      left = this.toFixed(left, 1);
      this.style = {
        left: left + "px",
        transitionProperty: 'left'
      };
    },
    // 重写toFixed函数
    toFixed: function toFixed(value, decimal) {
      var times = Math.pow(10, decimal);
      var des = value * times + 0.5;
      des = _parseInt(des, 10) / times;
      des = des.toFixed(decimal);
      return "" + des;
    },
    // tab项改变监听
    itemListener: function itemListener() {
      var _this2 = this;

      this.$on('change-item', function (index) {
        _this2.currentIndex = index;

        _this2.calculateLineStyle();

        _this2.$emit('change', index);
      });
    },
    // resize监听
    resizeListener: function resizeListener() {
      var self = this;
      window.addEventListener('resize', function () {
        self.calculateLineStyle();
      }, false);
    }
  },
  mounted: function mounted() {
    this.init();
    this.itemListener();
    this.resizeListener();
  }
});

/* 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-tab"
  }, [_c('div', {
    staticClass: "bx-tab-scroller"
  }, [_c('div', {
    staticClass: "bx-tab-wrap",
    class: _vm.isAdaptive && 'bx-tab-adaptive'
  }, [_vm._t("default")], 2), _c('div', {
    staticClass: "bx-tab-line",
    style: _vm.style
  })])]);
};

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;
