<template>
    <view :class="'custom-class ' + utils.bem('tabs', [type])">
        <van-sticky :disabled="!sticky" :z-index="zIndex" :offset-top="offsetTop" :container="container" @scroll="onTouchScroll">
            <view :class="utils.bem('tabs__wrap', { scrollable }) + ' ' + (type === 'line' && border ? 'van-hairline--top-bottom' : '')">
                <slot name="nav-left" />

                <scroll-view
                    :scroll-x="scrollable"
                    scroll-with-animation
                    :scroll-left="scrollLeft"
                    :class="utils.bem('tabs__scroll', [type])"
                    :style="color ? 'border-color: ' + color : ''"
                >
                    <view :class="utils.bem('tabs__nav', [type]) + ' nav-class'" :style="getters.tabCardTypeBorderStyle(color, type)">
                        <view v-if="type === 'line'" class="van-tabs__line" :style="lineStyle" />
                        <view
                            :data-index="index"
                            :class="
                                getters.tabClass(index === currentIndex, ellipsis) +
                                ' ' +
                                utils.bem('tab', { active: index === currentIndex, disabled: item.disabled, complete: !ellipsis })
                            "
                            :style="
                                getters.tabStyle(index === currentIndex, ellipsis, color, type, item.disabled, titleActiveColor, titleInactiveColor, swipeThreshold, scrollable)
                            "
                            @tap="onTap"
                            v-for="(item, index) in tabs"
                            :key="index"
                        >
                            <view :class="ellipsis ? 'van-ellipsis' : ''" :style="item.titleStyle">
                                {{ item.title }}
                                <van-info v-if="item.info !== null || item.dot" :info="item.info" :dot="item.dot" custom-class="van-tab__title__info" />
                            </view>
                        </view>
                    </view>
                </scroll-view>

                <slot name="nav-right" />
            </view>
        </van-sticky>

        <view class="van-tabs__content" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
            <view :class="utils.bem('tabs__track', [{ animated }]) + ' van-tabs__track'" :style="getters.trackStyle({ duration, currentIndex, animated })">
                <slot />
            </view>
        </view>
    </view>
</template>
<script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/lib/wxs/utils.wxs"></script>
<script module="getters" lang="wxs" src="@/node_modules/@vant/weapp/lib/tabs/index.wxs"></script>
<script>
'use strict';
Object.defineProperty(exports, '__esModule', {
    value: true
});
var component_1 = require('../common/component');
var touch_1 = require('../mixins/touch');
var utils_1 = require('../common/utils');
component_1.VantComponent({
    mixins: [touch_1.touch],
    classes: ['nav-class', 'tab-class', 'tab-active-class', 'line-class'],
    relation: {
        name: 'tab',
        type: 'descendant',
        current: 'tabs',
        linked: function (target) {
            target.index = this.children.length - 1;
            this.updateTabs();
        },
        unlinked: function () {
            this.children = this.children.map(function (child, index) {
                child.index = index;
                return child;
            });
            this.updateTabs();
        }
    },
    props: {
        color: {
            type: String,
            observer: 'setLine'
        },
        sticky: Boolean,
        animated: {
            type: Boolean,
            observer: function () {
                var that = this;
                this.children.forEach(function (child, index) {
                    return child.updateRender(index === that.currentIndex, that);
                });
            }
        },
        swipeable: Boolean,
        lineWidth: {
            type: [String, Number],
            value: -1,
            observer: 'setLine'
        },
        lineHeight: {
            type: [String, Number],
            value: -1,
            observer: 'setLine'
        },
        titleActiveColor: String,
        titleInactiveColor: String,
        active: {
            type: [String, Number],
            value: 0,
            observer: function (name) {
                if (name !== this.getCurrentName()) {
                    this.setCurrentIndexByName(name);
                }
            }
        },
        type: {
            type: String,
            value: 'line'
        },
        border: {
            type: Boolean,
            value: true
        },
        ellipsis: {
            type: Boolean,
            value: true
        },
        duration: {
            type: Number,
            value: 0.3
        },
        zIndex: {
            type: Number,
            value: 1
        },
        swipeThreshold: {
            type: Number,
            value: 4,
            observer: function (value) {
                this.setData({
                    scrollable: this.children.length > value || !this.ellipsis
                });
            }
        },
        offsetTop: {
            type: Number,
            value: 0
        },
        lazyRender: {
            type: Boolean,
            value: true
        }
    },
    data: {
        tabs: [],
        lineStyle: '',
        scrollLeft: 0,
        scrollable: false,
        trackStyle: '',
        currentIndex: null,
        container: null
    },
    mounted: function () {
        var that = this;
        this.$nextTick(function () {
            that.setLine(true);
            that.scrollIntoView();
        });
    },
    methods: {
        updateContainer: function () {
            var that = this;
            this.setData({
                container: function () {
                    return uni.createSelectorQuery().in(that).select('.van-tabs');
                }
            });
        },
        updateTabs: function () {
            var that = this;
            var _b = that.children;
            var children = _b === void 0 ? [] : _b;
            var data = that;
            this.setData({
                tabs: children.map(function (child) {
                    return child.data;
                }),
                scrollable: this.children.length > data.swipeThreshold || !data.ellipsis
            });
            this.setCurrentIndexByName(this.getCurrentName() || data.active);
        },
        trigger: function (eventName, child) {
            var currentIndex = this.currentIndex;
            var currentChild = child || this.children[currentIndex];
            if (!utils_1.isDef(currentChild)) {
                return;
            }
            this.$emit(eventName, {
                index: currentChild.index,
                name: currentChild.getComputedName(),
                title: currentChild.data.title
            });
        },
        onTap: function (event) {
            var that = this;
            var index = event.currentTarget.dataset.index;
            var child = this.children[index];
            if (child.data.disabled) {
                this.trigger('disabled', child);
            } else {
                this.setCurrentIndex(index);
                this.$nextTick(function () {
                    that.trigger('click');
                });
            }
        },
        // correct the index of active tab
        setCurrentIndexByName: function (name) {
            var _a = this.children;
            var children = _a === void 0 ? [] : _a;
            var matched = children.filter(function (child) {
                return child.getComputedName() === name;
            });
            if (matched.length) {
                this.setCurrentIndex(matched[0].index);
            }
        },
        setCurrentIndex: function (currentIndex) {
            var that = this;
            var _a = this;
            var data = _a;
            var _b = _a.children;
            var children = _b === void 0 ? [] : _b;
            if (!utils_1.isDef(currentIndex) || currentIndex >= children.length || currentIndex < 0) {
                return;
            }
            children.forEach(function (item, index) {
                var active = index === currentIndex;
                if (active !== item.data.active || !item.inited) {
                    item.updateRender(active, that);
                }
            });
            if (currentIndex === data.currentIndex) {
                return;
            }
            var shouldEmitChange = data.currentIndex !== null;
            this.setData({
                currentIndex: currentIndex
            });
            this.$nextTick(function () {
                that.setLine();
                that.scrollIntoView();
                that.updateContainer();
                that.trigger('input');
                if (shouldEmitChange) {
                    that.trigger('change');
                }
            });
        },
        getCurrentName: function () {
            var activeTab = this.children[this.currentIndex];
            if (activeTab) {
                return activeTab.getComputedName();
            }
        },
        setLine: function (skipTransition) {
            var that = this;
            if (this.type !== 'line') {
                return;
            }
            var _a = this;
            var color = _a.color;
            var duration = _a.duration;
            var currentIndex = _a.currentIndex;
            var lineWidth = _a.lineWidth;
            var lineHeight = _a.lineHeight;
            this.getRect('.van-tab', true).then(function (rects) {
                if (rects === void 0) {
                    rects = [];
                }
                var rect = rects[currentIndex];
                if (rect == null) {
                    return;
                }
                var width = lineWidth !== -1 ? lineWidth : rect.width / 2;
                var height = lineHeight !== -1 ? 'height: ' + utils_1.addUnit(lineHeight) + '; border-radius: ' + utils_1.addUnit(lineHeight) + ';' : '';
                var left = rects.slice(0, currentIndex).reduce(function (prev, curr) {
                    return prev + curr.width;
                }, 0);
                left += (rect.width - width) / 2;
                var transition = skipTransition ? '' : 'transition-duration: ' + duration + 's; -webkit-transition-duration: ' + duration + 's;';
                that.setData({
                    lineStyle:
                        '\n            ' +
                        height +
                        '\n            width: ' +
                        utils_1.addUnit(width) +
                        ';\n            background-color: ' +
                        color +
                        ';\n            -webkit-transform: translateX(' +
                        left +
                        'px);\n            transform: translateX(' +
                        left +
                        'px);\n            ' +
                        transition +
                        '\n          '
                });
            });
        },
        // scroll active tab into view
        scrollIntoView: function () {
            var that = this;
            var _a = this;
            var currentIndex = _a.currentIndex;
            var scrollable = _a.scrollable;
            if (!scrollable) {
                return;
            }
            Promise.all([this.getRect('.van-tab', true), this.getRect('.van-tabs__nav')]).then(function (_a) {
                var tabRects = _a[0];
                var navRect = _a[1];
                var tabRect = tabRects[currentIndex];
                var offsetLeft = tabRects.slice(0, currentIndex).reduce(function (prev, curr) {
                    return prev + curr.width;
                }, 0);
                that.setData({
                    scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2
                });
            });
        },
        onTouchScroll: function (event) {
            this.$emit('scroll', event.detail);
        },
        onTouchStart: function (event) {
            if (!this.swipeable) {
                return;
            }
            this.touchStart(event);
        },
        onTouchMove: function (event) {
            if (!this.swipeable) {
                return;
            }
            this.touchMove(event);
        },
        // watch swipe touch end
        onTouchEnd: function () {
            if (!this.swipeable) {
                return;
            }
            var _a = this;
            var tabs = _a.tabs;
            var currentIndex = _a.currentIndex;
            var that = this;
            var direction = that.direction;
            var deltaX = that.deltaX;
            var offsetX = that.offsetX;
            var minSwipeDistance = 50;
            if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
                if (deltaX > 0 && currentIndex !== 0) {
                    this.setCurrentIndex(currentIndex - 1);
                } else if (deltaX < 0 && currentIndex !== tabs.length - 1) {
                    this.setCurrentIndex(currentIndex + 1);
                }
            }
        }
    }
});
</script>
<style>
@import './index.css';
</style>
