/*
	步骤守卫
*/

export default {
	computed: {
		// 全局前置步骤守卫
		golbalBeforeNext() {
			const conf = this.realBaseConfig
			return typeof conf.beforeNext == 'function' ? conf.beforeNext : null
		},
		// 全局后置步骤守卫
		golbalAfterNext() {
			const conf = this.realBaseConfig
			return typeof conf.afterNext == 'function' ? conf.afterNext : null
		},
		// 当前步骤前置步骤守卫
		stepBeforeNext() {
			const conf = this.currentEleConfig
			return typeof conf.beforeNext == 'function' ? conf.beforeNext : null
		},
		// 当前步骤后置步骤守卫
		stepAfterNext() {
			const conf = this.currentEleConfig
			return typeof conf.afterNext == 'function' ? conf.afterNext : null
		}
	},
	methods:{
		// 进入步骤，步骤已改变，但还未渲染完成
		enterStep() {
			
		},
		// 离开步骤，步骤改变前，
		leaveStep() {
			
		},
		// 全局步骤守卫（前置）
		beforeNext(customParams = {}, callback = null) {
			const params = {
				to: this.stepList[this.currentStep + 1] || null,
				from: this.currentEleConfig,
				next: this.handleStepBeforeNext,
				skipStep: this.skipStep,
				index:this.currentStep,
				...customParams
			}
			if (this.golbalBeforeNext) {
				this.golbalBeforeNext(params)
				return
			}
			if (typeof callback == 'function') return callback();
			this.handleStepBeforeNext()
		},
		// 执行步骤守卫（前置）
		handleStepBeforeNext(customParams = {}, callback = null) {
			const params = {
				to: this.stepList[this.currentStep + 1] || null,
				from: this.currentEleConfig,
				next: this.next,
				skipStep: this.skipStep,
				index: this.currentStep,
				...customParams
			}
			if (this.stepBeforeNext) {
				this.stepBeforeNext(params)
				return
			}
			if (typeof callback == 'function') return callback();
			this.next()
		},
		// 全局步骤守卫（后置）
		afterNext(customParams = {}, callback = null) {
			const params = {
				to: this.currentEleConfig,
				from: this.stepList[this.currentStep - 1] || null,
				index: this.currentStep,
				skipStep: this.skipStep,
				...customParams 
			}
			if (this.golbalAfterNext) {
				this.golbalAfterNext(params)
			}
			if (typeof callback == 'function') return callback();
			this.handleStepAfterNext()
		},
		// 执行步骤守卫（后置）
		handleStepAfterNext(customParams = {}) {
			const params = {
				to: this.stepList[this.currentStep + 1] || null,
				from: this.currentEleConfig,
				index:this.currentStep,
				skipStep: this.skipStep,
				...customParams
			}
			if (this.stepAfterNext) {
				this.stepAfterNext(params)
			}
		},
		// 完成引导守卫
		beforeFinished(customParams = {}, callback = null) {
			const realBaseConfig = this.realBaseConfig
			if(typeof realBaseConfig.beforeFinished == "function") {
				realBaseConfig.beforeFinished({
					finish: this.handleFinished,
					index:this.currentStep, 
					from: this.currentEleConfig,
					skipStep: this.skipStep,
					...customParams
				})
				return
			}
			if (typeof callback == 'function') return callback();
			this.handleFinished()
		},
		// 跳过守卫
		beforeSkip(customParams = {}, callback = null) {
			const realBaseConfig = this.realBaseConfig
			if(typeof realBaseConfig.beforeSkip == "function") {
				realBaseConfig.beforeSkip({
					skip: this.handleSkip, 
					index:this.currentStep, 
					from: this.currentEleConfig,
					skipStep: this.skipStep,
					...customParams
				})
				return
			}
			if (typeof callback == 'function') return callback();
			this.handleSkip()
		}
	}
}