'use strict';
Object.defineProperty(exports, '__esModule', {
    value: true
});
exports.transition = void 0;
var utils_1 = require('../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, 33.333333333333336);
    });
};
exports.transition = function (showDefaultValue) {
    return {
        data() {
            return {
                type: '',
                inited: false,
                display: false
            };
        },
        props: {
            customStyle: String,
            // @ts-ignore
            show: {
                type: Boolean,
                value: showDefaultValue,
                observer: 'observeShow'
            },
            // @ts-ignore
            duration: {
                type: null,
                value: 300,
                observer: 'observeDuration'
            },
            name: {
                type: String,
                value: 'fade'
            }
        },
        methods: {
            observeShow: function (value, old) {
                if (value === old) {
                    return;
                }
                if (value) {
                    this.enter();
                } else {
                    this.leave();
                }
            },
            enter: function () {
                var that = this;
                var _a = this;
                var duration = _a.duration;
                var name = _a.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 () {
                        that.checkStatus('enter');
                        that.$emit('enter');
                        that.setData({
                            inited: true,
                            display: true,
                            classes: classNames.enter,
                            currentDuration: currentDuration
                        });
                    })
                    .then(nextTick)
                    .then(function () {
                        that.checkStatus('enter');
                        that.transitionEnded = false;
                        that.setData({
                            classes: classNames['enter-to']
                        });
                    })
                    .catch(function () {});
            },
            leave: function () {
                var that = this;
                if (!this.display) {
                    return;
                }
                var _a = this;
                var duration = _a.duration;
                var name = _a.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 () {
                        that.checkStatus('leave');
                        that.$emit('leave');
                        that.setData({
                            classes: classNames.leave,
                            currentDuration: currentDuration
                        });
                    })
                    .then(nextTick)
                    .then(function () {
                        that.checkStatus('leave');
                        that.transitionEnded = false;
                        setTimeout(function () {
                            return that.onTransitionEnd();
                        }, currentDuration);
                        that.setData({
                            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 _a = this;
                var show = _a.show;
                var display = _a.display;
                if (!show && display) {
                    this.setData({
                        display: false
                    });
                }
            }
        }
    };
};
