

/**
 * This class handles events that should be executed instead of a passage.
 * For example, the playercharacter wants to go somewhere and meets a friend on the way.
 * @date 6/30/2023 - 8:20:40 AM
 *
 * @class Events
 * @type {Events}
 */
class Events{


	_registeredByPassage = {}
	registeredByPassage(passage){
		return this._registeredByPassage[passage] ?? [];
	}

	/**
	 *
	 * @param {string} passage
	 * @param {Array} eventPassage
	 * @param {number} priority
	 */
    registerByPassage(passage,eventPassage,priority=0,tag=undefined){
        if(!this._registeredByPassage[passage])
			this._registeredByPassage[passage] = [];
		let data = {priority: priority ?? 0,passage:eventPassage};
		if(tag != undefined)
			data['tag'] = tag;
		this._registeredByPassage[passage].push(data);
    }

	_registeredByTag = {}
	registeredByTag(tag){
		return this._registeredByTag[tag] ?? [];
	}
	registerByTag(tag,eventPassage,priority=0,datatag=undefined){
        if(!this._registeredByTag[tag])
			this._registeredByTag[tag] = [];
		let data = {priority: priority ?? 0,passage:eventPassage};
		if(datatag != undefined)
			data['tag'] = datatag;
		this._registeredByTag[tag].push(data);
    }


	_init(Events){
		Object.keys(Events).forEach(function (pn) {
			this[pn] = clone(Events[pn]);
		}, this);

		return this;
	}

	clone = function () {
		return (new setup.Events())._init(this);
	};

	toJSON = function () {
		var ownData = {};
		Object.keys(this).forEach(function (pn) {
			if(typeof this[pn] !== "function")
				ownData[pn] = clone(this[pn]);
		}, this);
		return JSON.reviveWrapper('(new setup.Events())._init($ReviveData$)', ownData);
	};
}

setup.Events = Events;
