class TimedVarsContainer{
	static handler = {
		get(target:TimedVarsContainer, prop:string) {
			return target.get(prop);
		},
		set(target:TimedVarsContainer, prop:string, value:any, receiver){
			target.set(prop,value);	
			return true;
		}
	};

	#timeFunction:()=>number;
	#variablesEntry:string;

	static create(variablesEntry:string,timeFunction:()=>number){
		return new Proxy(new TimedVarsContainer(variablesEntry,timeFunction),TimedVarsContainer.handler);
	}

	get(prop:string){
		const mainObject = State.getVar(this.#variablesEntry);
		if(!mainObject)
			return undefined;
		const dataEntry = mainObject[prop] ?? [-Infinity,undefined];
		const [validityTime,v] = dataEntry;
		if(validityTime < this.#timeFunction())
			return undefined;
		return v;
	}

	set(prop:string, val: any){
		let mainObject = State.getVar(this.#variablesEntry);
		if(!mainObject){
			State.setVar(this.#variablesEntry,{});
			mainObject = {};
		}
		let overwrite = {};
		overwrite[prop] = [this.#timeFunction(),val];
		const updatedObject = Object.assign(mainObject,overwrite);
		State.setVar(this.#variablesEntry,updatedObject);
	}

	
	/**
	 * @private
	 * @constructor
	 * @param {string} variablesEntry
	 * @param {()=>number} timeFunction
	 */
	constructor(variablesEntry:string,timeFunction:()=>number){
		this.#timeFunction = timeFunction;
		this.#variablesEntry = variablesEntry;
	}
}