'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _event = require('../_utils/dom/event');

var _touch = require('../mixins/touch');

var _mouse = require('../mixins/mouse');

var _raf = require('../_utils/dom/raf');

var _commonUtil = require('../utils/commonUtil');

var _numberUtil = require('../utils/numberUtil');

require('./index.less');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var Carousel = {
    name: 'SnCarousel',
    mixins: [_touch.TouchMixin, _mouse.MouseMixin],

    props: {
        width: Number,
        height: Number,
        autoplay: Number,
        vertical: Boolean,
        indicatorColor: String,
        loop: {
            type: Boolean,
            'default': true
        },
        duration: {
            type: Number,
            'default': 500
        },
        touchable: {
            type: Boolean,
            'default': true
        },
        mouseable: {
            type: Boolean,
            'default': true
        },
        mousewheelable: {
            type: Boolean,
            'default': true
        },
        initialSwipe: {
            type: Number,
            'default': 0
        },
        showIndicators: {
            type: Boolean,
            'default': true
        },
        threshold: {
            type: Number,
            'default': 30
        }
    },

    data: function data() {
        return {
            computedWidth: 0,
            computedHeight: 0,
            offset: 0,
            active: 0,
            deltaX: 0,
            deltaY: 0,
            swipes: [],
            swiping: false,
            draging: false
        };
    },


    watch: {
        swipes: function swipes() {
            this.initialize();
        },
        initialSwipe: function initialSwipe() {
            this.initialize();
        },
        autoplay: function autoplay(_autoplay) {
            if (!_autoplay) {
                this.clear();
            } else {
                this.autoPlay();
            }
        },
        offset: function offset(val) {
            var deltaX = this.deltaX,
                deltaY = this.deltaY,
                size = this.size,
                draging = this.draging;

            this.$emit('offset', {
                deltaX: deltaX,
                deltaY: deltaY,
                offset: Math.abs(val) % (size + 1),
                size: size,
                draging: draging
            });
        }
    },

    computed: {
        count: function count() {
            return this.swipes.length;
        },
        delta: function delta() {
            return this.vertical ? this.deltaY : this.deltaX;
        },
        size: function size() {
            return this[this.vertical ? 'computedHeight' : 'computedWidth'];
        },
        trackSize: function trackSize() {
            return this.count * this.size;
        },
        activeIndicator: function activeIndicator() {
            return (this.active + this.count) % this.count;
        },
        isCorrectDirection: function isCorrectDirection() {
            var expect = this.vertical ? 'vertical' : 'horizontal';
            return this.direction === expect;
        },
        trackStyle: function trackStyle() {
            var _ref;

            var mainAxis = this.vertical ? 'height' : 'width';
            var crossAxis = this.vertical ? 'width' : 'height';

            return _ref = {}, (0, _defineProperty3['default'])(_ref, mainAxis, this.trackSize + 'px'), (0, _defineProperty3['default'])(_ref, crossAxis, this[crossAxis] ? this[crossAxis] + 'px' : ''), (0, _defineProperty3['default'])(_ref, 'transitionDuration', (this.swiping ? 0 : this.duration) + 'ms'), (0, _defineProperty3['default'])(_ref, 'transform', 'translate' + (this.vertical ? 'Y' : 'X') + '(' + this.offset + 'px)'), _ref;
        },
        indicatorStyle: function indicatorStyle() {
            return {
                backgroundColor: this.indicatorColor
            };
        },
        minOffset: function minOffset() {
            var rect = window.getComputedStyle(this.$el);
            return (this.vertical ? parseFloat(rect.height) : parseFloat(rect.width)) - this.size * this.count;
        }
    },

    mounted: function mounted() {
        (0, _event.on)(window, 'resize', this.onResize, true);
    },
    activated: function activated() {
        (0, _event.on)(window, 'resize', this.onResize, true);
    },
    deactivated: function deactivated() {
        (0, _event.off)(window, 'resize', this.onResize);
    },
    beforeDestroy: function beforeDestroy() {
        (0, _event.off)(window, 'resize', this.onResize);
    },


    methods: {
        initialize: function initialize() {
            var active = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.initialSwipe;

            clearTimeout(this.timer);

            if (this.$el) {
                var rect = this.$el.getBoundingClientRect();
                this.computedWidth = this.width || rect.width;
                this.computedHeight = this.height || rect.height;
            }

            this.swiping = true;
            this.active = active;
            this.offset = this.count > 1 ? -this.size * this.active : 0;
            this.swipes.forEach(function (swipe) {
                swipe.offset = 0;
            });
            this.autoPlay();
        },
        onResize: function onResize() {
            this.initialize(this.activeIndicator);
        },
        onTouchStart: function onTouchStart(event) {
            if (!this.touchable) return;

            this.clear();
            this.swiping = true;
            this.draging = true;
            this.touchStart(event);
            this.correctPosition();
        },
        onMouseStart: function onMouseStart(event) {
            if (!this.mouseable) return;

            this.clear();
            this.swiping = true;
            this.draging = true;
            this.mouseStart(event);
            this.correctPosition();
        },
        onTouchMove: function onTouchMove(event) {
            if (!this.touchable || !this.swiping) return;

            this.touchMove(event);

            if (this.isCorrectDirection) {
                (0, _event.preventDefault)(event, true);
                this.move({ offset: this.delta });
            }
        },
        onMouseMove: function onMouseMove(event) {
            if (!this.mouseable || !this.swiping) return;

            this.mouseMove(event);

            if (this.isCorrectDirection) {
                (0, _event.preventDefault)(event, true);
                this.move({ offset: this.delta });
            }
        },
        onTouchEnd: function onTouchEnd() {
            if (!this.touchable || !this.swiping) return;

            if (this.delta && this.isCorrectDirection) {
                var offset = this.vertical ? this.offsetY : this.offsetX;

                if (offset > this.threshold) {
                    this.move({
                        pace: offset > 0 ? this.delta > 0 ? -1 : 1 : 0,
                        emitChange: true
                    });
                } else {
                    this.move({
                        pace: 0,
                        emitChange: false
                    });
                }
            }

            this.swiping = false;
            this.draging = false;
            this.autoPlay();
        },
        onMouseEnd: function onMouseEnd() {
            if (!this.mouseable || !this.swiping) return;

            if (this.delta && this.isCorrectDirection) {
                var offset = this.vertical ? this.offsetY : this.offsetX;

                if (offset > this.threshold) {
                    this.move({
                        pace: offset > 0 ? this.delta > 0 ? -1 : 1 : 0,
                        emitChange: true
                    });
                } else {
                    this.move({
                        pace: 0,
                        emitChange: false
                    });
                }
            }

            this.swiping = false;
            this.draging = false;
            this.autoPlay();
        },
        onMouseWheel: function onMouseWheel(event) {
            if (!this.mousewheelable) return;

            (0, _event.preventDefault)(event, true);
            var wheelDelta = event.wheelDelta;
            var nextIndicator = wheelDelta < 0 ? this.activeIndicator + 1 : this.activeIndicator - 1;

            this.swipeTo(nextIndicator);
        },
        onTransitionEnd: function onTransitionEnd(event) {
            this.$emit('transition-end', event);
        },
        getTargetActive: function getTargetActive(pace) {
            var active = this.active,
                count = this.count;


            if (pace) {
                if (this.loop) {
                    return (0, _numberUtil.range)(active + pace, -1, count);
                }

                return (0, _numberUtil.range)(active + pace, 0, count - 1);
            }

            return active;
        },
        getTargetOffset: function getTargetOffset(targetActive, offset) {
            var currentPosition = targetActive * this.size;
            if (!this.loop) {
                currentPosition = Math.min(currentPosition, -this.minOffset);
            }

            var targetOffset = Math.round(offset - currentPosition);
            if (!this.loop) {
                targetOffset = (0, _numberUtil.range)(targetOffset, this.minOffset, 0);
            }

            return targetOffset;
        },
        move: function move(_ref2) {
            var _ref2$pace = _ref2.pace,
                pace = _ref2$pace === undefined ? 0 : _ref2$pace,
                _ref2$offset = _ref2.offset,
                offset = _ref2$offset === undefined ? 0 : _ref2$offset,
                emitChange = _ref2.emitChange;
            var loop = this.loop,
                count = this.count,
                active = this.active,
                swipes = this.swipes,
                trackSize = this.trackSize,
                minOffset = this.minOffset;


            if (count <= 1) {
                return;
            }

            var targetActive = this.getTargetActive(pace);
            var targetOffset = this.getTargetOffset(targetActive, offset);
            // auto move first and last swipe in loop mode
            if (loop) {
                if (swipes[0]) {
                    var outRightBound = targetOffset < minOffset;
                    swipes[0].offset = outRightBound ? trackSize : 0;
                }

                if (swipes[count - 1]) {
                    var outLeftBound = targetOffset > 0;
                    swipes[count - 1].offset = outLeftBound ? -trackSize : 0;
                }
            }

            this.active = targetActive;
            this.offset = targetOffset;

            if (emitChange && targetActive !== active) {
                this.$emit('change', this.activeIndicator);
            }
        },
        swipeTo: function swipeTo(index) {
            var _this = this;

            var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

            this.swiping = true;
            this.resetTouchStatus();
            this.correctPosition();

            (0, _raf.doubleRaf)(function () {
                var targetIndex = void 0;
                if (_this.loop && index === _this.count) {
                    targetIndex = _this.active === 0 ? 0 : index;
                } else {
                    targetIndex = index % _this.count;
                }

                _this.move({
                    pace: targetIndex - _this.active,
                    emitChange: true
                });

                if (options.immediate) {
                    (0, _raf.doubleRaf)(function () {
                        _this.swiping = false;
                    });
                } else {
                    _this.swiping = false;
                }
            });
        },
        correctPosition: function correctPosition() {
            if (this.active <= -1) {
                this.move({ pace: this.count });
            }

            if (this.active >= this.count) {
                this.move({ pace: -this.count });
            }
        },
        clear: function clear() {
            clearTimeout(this.timer);
        },
        autoPlay: function autoPlay() {
            var _this2 = this;

            var autoplay = this.autoplay;


            if (autoplay && this.count > 1) {
                this.clear();
                this.timer = setTimeout(function () {
                    _this2.swiping = true;
                    _this2.resetTouchStatus();
                    _this2.correctPosition();

                    (0, _raf.doubleRaf)(function () {
                        _this2.swiping = false;
                        _this2.move({
                            pace: 1,
                            emitChange: true
                        });
                        _this2.autoPlay();
                    });
                }, autoplay);
            }
        },
        genIndicator: function genIndicator() {
            var _this3 = this;

            var h = this.$createElement;
            var count = this.count,
                activeIndicator = this.activeIndicator;

            var slot = this.$slots.indicator;

            if (slot) {
                return slot;
            }

            if (this.showIndicators && count > 1) {
                return h(
                    'div',
                    {
                        'class': 'sn-carousel-indicators' + (this.vertical ? ' sn-carousel-indicators-vertical' : '')
                    },
                    [Array.apply(undefined, (0, _toConsumableArray3['default'])(Array(count))).map(function (empty, index) {
                        return h('i', {
                            'class': 'sn-carousel-indicator' + (index === activeIndicator ? ' sn-carousel-indicator-active' : ''),
                            style: index === activeIndicator ? _this3.indicatorStyle : ''
                        });
                    })]
                );
            }
        }
    },

    render: function render() {
        var h = arguments[0];

        return h(
            'div',
            { 'class': 'sn-carousel' },
            [h(
                'div',
                {
                    ref: 'track',
                    style: this.trackStyle,
                    'class': 'sn-carousel-track',
                    on: {
                        'touchstart': this.onTouchStart,
                        'mousedown': this.onMouseStart,
                        'touchmove': this.onTouchMove,
                        'mousemove': this.onMouseMove,
                        'touchend': this.onTouchEnd,
                        'mouseup': this.onMouseEnd,
                        'touchcancel': this.onTouchEnd,
                        'mouseout': this.onMouseEnd,
                        'mousewheel': (0, _commonUtil.throttle)(this.onMouseWheel, 150),
                        'transitionend': this.onTransitionEnd
                    }
                },
                [this.$slots['default']]
            ), this.genIndicator()]
        );
    }
};

Carousel.install = function (Vue) {
    Vue.component(Carousel.name, Carousel);
};

exports['default'] = Carousel;
module.exports = exports.default;