var _ = require('lodash');

module.exports = function myJSMergeProcessor(log) {
	return {
		$runAfter: ['computePathsProcessor'],
		$runBefore: ['renderDocsProcessor'],
		$process: function (docs) {

			ngDocs = _(docs).reject({docType: 'ngModule'})
							.reject({docType: 'componentGroup'})
							.value();



			jsDocsWithMemberOf = _(docs).filter({docType: 'js'})
										.filter(function(doc) {
											var tags = doc.tags.tags;
											return _.find(tags, function(tag) {
												if(tag.tagName === 'memberof') {
													return true;
												}
												return false;
											});
										})
										.value();

			jsDocsWithMemberOf.forEach( function(doc, idx) {
				var tags = doc.tags.tags;
				var memberofTag = _.find(tags, function(tag) {
					if(tag.tagName === 'memberof'){
						return true;
					}
				});

				if (typeof memberofTag !== 'undefined'){
					var parentName = memberofTag.description;

					log.debug("memberof: " + parentName);
					// find the associated doc e.g. service or directive
					var parentIdx = _.findIndex(ngDocs, function(a) {
						return a.name === parentName;
					});

					log.debug("doc id:"+ doc.id + ", parentIdx:" + parentIdx + ", memberof:" + parentName);

					if(parentIdx > -1){

						var parentDoc = ngDocs[parentIdx];

						if (! parentDoc.hasOwnProperty('methods')) {
							// add methods
							parentDoc.methods = [];
						}

						parentDoc.methods.push({
							"name": doc.id,
							"description": doc.description
						});

						// if(_.isString(parentDoc.path)){
							doc.path = parentDoc.path;
							doc.outputPath = doc.path + doc.outputPath;
						// }
					}
				}

				// update doc type
				doc.docType = "gcMethod";

			});

			return docs;
		}
	};
};
@@ -1231,24 +1231,31 @@
 rentName);%0A%0A
+%09%09%09%09%09%0A%0A
 %09%09%09%09%09if(pare
@@ -1433,104 +1433,152 @@
 %09%09%09%09
-parentDoc.methods.push(%7B%0A%09%09%09%09%09%09%09%22name%22: doc.id,%0A%09%09%09%09%09%09%09%22description%22: doc.description
+// update path and outputPath%0A%09%09%09%09%09%09doc.path = parentDoc.path;%0A%09%09%09%09%09%09doc.outputPath = doc.path + doc.outputPath;%0A%09%09%09%09%09%09doc.name = doc.id;
 %0A%09%09%09%09%09%09
-%7D);%0A
 %0A%09%09%09
@@ -1587,97 +1587,147 @@
 %09// 
-if(_.isString(parentDoc.path))%7B%0A%09%09%09%09%09%09%09doc.path = parentDoc.path;%0A%09%09%09%09%09%09%09doc.outputPath =
+add method to parent node%0A%09%09%09%09%09%09parentDoc.methods.push(%7B%0A%09%09%09%09%09%09%09%22name%22: doc.id,%0A%09%09%09%09%09%09%09%22description%22: doc.description,%0A%09%09%09%09%09%09%09%22url%22: '#/' +
  doc
@@ -1742,30 +1742,23 @@
 doc.
-outputPath;
+name
 %0A%09%09%09%09%09%09
-// %7D
+%7D);%0A
 %0A%09%09%09

update jsMergerProcessor, provide more info in created doc object.