//TODO soms blijven requests op (pending) staan. Deze moeten afgekapt worden
const CollectionAnalysisAPI = {

	runAnalysis : function(analyserId, params, callback) {
		const url = _config.COLLECTION_ANALYSIS_API_BASE + '/' + analyserId;
		const xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function() {
			if (xhr.readyState === XMLHttpRequest.DONE) {
				if(xhr.status === 200) {
					callback(JSON.parse(xhr.responseText));
				} else {
					callback(null);
				}
			}
		}
		xhr.open("POST", url);
		xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
		xhr.send(JSON.stringify(params));
	},

	//convenience function (temporary probably)
	fieldCompletenessTimeline: function(collectionId, docType, dateField, analysisField, facets, minYear, nestedPath, callback) {
		CollectionAnalysisAPI.runAnalysis('field_completeness_timeline', {
			collectionId : collectionId,
			docType : docType,
			dateField : dateField,
			analysisField : analysisField,
			facets : facets,
			minYear : minYear,
			nestedPath : nestedPath
		}, callback)
	},

	//convenience function (temporary probably)
	collectionFieldCompleteness: function(collectionId, docType, callback) {
		CollectionAnalysisAPI.runAnalysis('collection_field_completeness', {
			collectionId : collectionId,
			docType : docType
		}, callback)
	}

};

export default CollectionAnalysisAPI;
