define([
	"dojo/_base/kernel",
	"dojo/_base/lang",
	"dojo/Stateful"
], function(kernel, lang, Stateful){


	//target prop may refer to an object path
	var watch = function(/*dojo/Stateful*/ target, /*String*/ targetProp, callback){
		
		var path = targetProp.split(".");
		if (path.length)
		{
			var tmp=target;
			for (var i=0; i<path.length; i++)
			{
				if (i==path.length - 1)
				{
					//last path segment
					var s = tmp.get(path[i]);
					if ( s && tmp.watch)
					{
						return tmp.watch(path[i], callback);
					}
					else
					{
						tmp.set(path[i], null);
						return tmp.watch(path[i], callback);
					}
				}
				else
				{
					var s = tmp.get(path[i]);
					if (s && s.get)
					{
						tmp=s;
					}
					else
					{
						//if it does not exist create a new stateful
						var n = new Stateful();
						tmp.set(path[i], n);
						tmp = n;
					}
				}
			}
		}
		return null;
		
	};

	return lang.setObject("obno.model.resource.watch", watch);
});