<template>
  <div class="el-tabs__active-bar" :class="`is-${ rootTabs.tabPosition }`" :style="barStyle"></div>
</template>
<script>
  export default {
    name: 'TabBar',

    props: {
      tabs: Array,
      type: String
    },

    inject: ['rootTabs'],

    computed: {
      barStyle: {
        cache: false,
        get() {
          let {
            type
          } = this;
          if (!this.$parent.$refs.tabs) return {};
          let style = {};
          let offset = 0;
          let tabSize = 0;
          const sizeName = ['top', 'bottom'].indexOf(this.rootTabs.tabPosition) !== -1 ? 'width' : 'height';
          const sizeDir = sizeName === 'width' ? 'x' : 'y';
          const firstUpperCase = str => {
            return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
          };
          this.tabs.every((tab, index) => {
            let $el = this.$parent.$refs.tabs[index];
            if (!$el) { return false; }
            let _offset = $el[`offset${firstUpperCase(sizeName)}`];
            if (!tab.active) {
              /* offset += type === 'border-card' ? (_offset - 2) : _offset; */
              type === 'border-card' && (_offset -= 2);
              sizeName === 'height' && (_offset += 8);
              offset += _offset;
              return true;
            } else {
              tabSize = type === 'border-card' ? (_offset - 1) : _offset;
              /* if (sizeName === 'width' && this.tabs.length > 1) {
                 tabSize -= (index === 0 || index === this.tabs.length - 1) ? 20 : 40;
                 tabSize += (index === this.tabs.length - 1) && 20;
              } */
              return false;
            }
          });

          /* if (sizeName === 'width' && offset !== 0) {
            offset += 20;
          } */
          const transform = `translate${firstUpperCase(sizeDir)}(${offset}px)`;
          style[sizeName] = tabSize + 'px';
          style.transform = transform;
          style.msTransform = transform;
          style.webkitTransform = transform;
          type && (style.top = 0, style.bottom = 'auto');

          return style;
        }
      }
    }
  };
</script>
