"use strict";
import Utils from "../../utils/utils";
import utils_1 from "../common/utils"
var getClassNames = function(name) {
    return ({
        enter: "van-" + name + "-enter van-" + name + "-enter-active enter-class enter-active-class",
        'enter-to': "van-" + name + "-enter-to van-" + name +
            "-enter-active enter-to-class enter-active-class",
        leave: "van-" + name + "-leave van-" + name + "-leave-active leave-class leave-active-class",
        'leave-to': "van-" + name + "-leave-to van-" + name +
            "-leave-active leave-to-class leave-active-class"
    });
};
var nextTick = function() {
    return new Promise(function(resolve) {
        return setTimeout(resolve, 1000 / 30);
    });
};

function transition(showDefaultValue) {
    return {
        props: {
            customStyle: {
                type: String,
                default: ''
            },
            show: {
                type: Boolean,
                default: showDefaultValue
            },
            duration: {
                type: null,
                default: 300
            },
            name: {
                type: String,
                default: 'fade'
            }
        },
        data() {
            return {
                type: '',
                inited: false,
                display: false
            }
        },
        watch: {
            show: function(newData, oldData) {
                // console.log('watch', newData, oldData)
                this.observeShow(newData, oldData)
            }
        },
        mounted() {
            this.observeShow(this.show)
        },
        methods: {
            observeShow: function(value, old) {
                // console.log('observeShow')
                // console.log(value, old)
                if (value === old) {
                    return;
                }
                value ? this.enter() : this.leave();
            },
            enter: function() {
                var _this = this,
                    duration = _this.duration,
                    name = _this.name;
                var classNames = getClassNames(name);
                var currentDuration = utils_1.isObj(duration) ? duration.enter : duration;
                this.status = 'enter';
                this.$emit('before-enter');
                Promise.resolve()
                    .then(nextTick)
                    .then(function() {
                        _this.checkStatus('enter');
                        _this.$emit('enter');
                        _this.inited = true
                        _this.display = true
                        _this.classes = classNames.enter
                        // console.log('1:' + _this.classes)
                        _this.currentDuration = currentDuration
                    })
                    .then(nextTick)
                    .then(function() {
                        _this.checkStatus('enter');
                        _this.transitionEnded = false;
                        setTimeout(() => {
                            _this.classes = classNames['enter-to']
                            // console.log('2:' + _this.classes)
                        }, 300)
                    })
                    .catch(function() {});
            },
            leave: function() {
                var _this = this;
                if (!this.display) {
                    return;
                }
                var duration = _this.duration,
                    name = _this.name;
                // console.log(name)
                var classNames = getClassNames(name);
                var currentDuration = utils_1.isObj(duration) ? duration.leave : duration;
                this.status = 'leave';
                this.$emit('before-leave');
                Promise.resolve()
                    .then(nextTick)
                    .then(function() {
                        _this.checkStatus('leave');
                        _this.$emit('leave');
                        _this.classes = classNames.leave
                        _this.currentDuration = currentDuration
                    })
                    .then(nextTick)
                    .then(function() {
                        _this.checkStatus('leave');
                        _this.transitionEnded = false;
                        setTimeout(function() {
                            return _this.onTransitionEnd();
                        }, currentDuration);
                        _this.classes = classNames['leave-to']
                    })
                    .catch(function() {});
            },
            checkStatus: function(status) {
                if (status !== this.status) {
                    throw new Error("incongruent status: " + status);
                }
            },
            onTransitionEnd: function() {
                if (this.transitionEnded) {
                    return;
                }
                this.transitionEnded = true;
                this.$emit("after-" + this.status);
                var show = this.show,
                    display = this.display;
                if (!show && display) {
                    this.display = false
                }
            }
        }
    };
};

const transition_1 = {
    transition
}

export default transition_1