var runQuery={};

//external dependencies:
//LABKEY.Query
//print -> configObject.print

runQuery.makeQuery=
function(targetObject,containerName,queryName,fieldName,filterArray){
   //call with makeQuery(config.formConfig,getContainer(name),...
	let e=new Object();
	e.containerName=containerName;
	e.queryName=queryName;
	e.fieldName=fieldName;
	e.filterArray=filterArray;
   e.targetObject=targetObject;
	return e;
}

runQuery.getDataFromQueries=
function(parentClass,queryArray,cb){
	//queryArray should contain elements with
	//- fieldName to set the data variable
	//- containerName to select container (data,config,CRF)
	//- queryName to select query
	//- filterArray to perform filtering, empty array works
	//- callback cb to be called with no arguments
	//
	this.afterQuery(new Object(),-1,parentClass,queryArray,cb);
}

runQuery.afterQuery=
function(data,id,parentClass,queryArray,cb){
	//queryArray should contain elements with
	//- fieldName to set the data variable
	//- containerName to select container (data,config,CRF)
	//- queryName to select query
	//- filterArray to perform filtering, empty array works
	//- callback cb to be called with no arguments
	//
	//it should be called with id -1.
	//
	//targetObject.print('afterQuery1['+id+'/'+queryArray.length+']: ');
   let fName='[afterQuery]';

	if (id>-1){
	   let e1=queryArray[id];
		let fieldName=e1.fieldName;
		parentClass.print(fName+' ['+fieldName+']: '+data.rows.length);
		e1.targetObject[fieldName]=data;
	}
	id+=1;
	if (id==queryArray.length) {
		cb();
		return;
	}
   

	let e=queryArray[id];
   for (v in e){
      parentClass.print(fName+' value ['+v+'] '+e[v]);
   }
	let qconfig=new Object();
	qconfig.containerPath=parentClass.getContainer(e.containerName);
   if ("containerPath" in e){
      parentClass.print(fName+' containerPath '+e.containerPath);
      qconfig.containerPath=e.containerPath;
   }

	qconfig.schemaName="lists";
	if ("schemaName" in e){
		parentClass.print(fName+' schemaName='+e.schemaName);
		qconfig.schemaName=e.schemaName;
	}

	if ("columns" in e){
		parentClass.print(fName+' columns='+e.columns);
		qconfig.columns=e.columns;
	}
	qconfig.queryName=e.queryName;
	//this should point to configuration container
	//don't filter -> so we can pick up other forms (say registration) later on
	//qconfig.filterArray=[LABKEY.Filter.create('Key',config.formId)];
	if ("filterArray" in e)
		qconfig.filterArray=e.filterArray;
	
	//qconfig.filterArray=[LABKEY.Filter.create('formStatus',1)]
   let that=this;
	qconfig.success=function(data){that.afterQuery(data,id,parentClass,queryArray,cb);};
	qconfig.failure=function(errorInfo,responseObj){that.onTAFailure(parentClass,errorInfo,responseObj);};
	LABKEY.Query.selectRows(qconfig);

}

runQuery.onTAFailure=
function(parentClass, errorInfo, responseObj){
   //don't have configObject to rely to
   parentClass.print('[afterQuery]: Failure: '+errorInfo.exception);

}