text: /* eslint-disable no-unused-vars */
  
  var conversation = null;
  var tags = null;
  
  class Service {
    constructor(options) {
      this.options = options || {};
    }
  
    /**
     * Set the conversation service for fetching tree nodes
     * @param {} conv
     */
    setConversation(conv) {
      conversation = conv;
      // console.info("TS", conversation);
    }
  
    setTags(tgs) {
      tags = tgs;
    }
  
    async find() {
      return [];
    }
  
    /**
     *@method populateKids
     *@description Get the children of the passed parent tree node (questions, answers, pros,
     *              cons, and tags and more)
     * @param {*} id -- for debugging
     * @param {*} questionArray
     * @param {*} answerArray
     * @param {*} proArray
     * @param {*} conArray
     * @param {*} tagArray
     * @param {*} relationArray
     * @param {*} subclassArray
     * @param {*} instanceArray
     * @returns
     * @memberof Service
     */
    async populateKids(questionArray, answerArray, proArray, conArray,
                        /*tagArray,*/ relationArray, subclassArray, instanceArray) {
      let result = [];
      let cursor;
      let nodeId;
      let node;
      var i;
  
      if (questionArray) {
        for (i in questionArray) {
          cursor = {};
  
          nodeId = questionArray[i];
  
          //Get the node of the child
          const questionResult = await conversation.find({
            query: { nodeId, skippop: true }
          });
  
          node = questionResult.data[0];
          cursor.label = node.label;
          cursor.nodeId = node.nodeId;
          result.push(cursor);
        }
      }
      if (answerArray) {
        for (i in answerArray) {
          cursor = {};
  
          nodeId = answerArray[i];
  
          //Get the node of the child
          const answerResult = await conversation.find({
            query: { nodeId, skippop: true }
          });
  
          node = answerResult.data[0];
          cursor.label = node.label;
          cursor.nodeId = node.nodeId;
          result.push(cursor);
        }
      }
      if (proArray) {
        for (i in proArray) {
          cursor = {};
  
          nodeId = proArray[i];
  
          //Get the node of the child
          const proResult = await conversation.find({
            query: { nodeId, skippop: true }
          });
  
          node = proResult.data[0];
          cursor.label = node.label;
          cursor.nodeId = node.nodeId;
          result.push(cursor);
        }
      }
      if (conArray) {
        for (i in conArray) {
          cursor = {};
  
          nodeId = conArray[i];
  
          //Get the node of the child
          const conResult = await conversation.find({
            query: { nodeId, skippop: true }
          });
  
          node = conResult.data[0];
          cursor.label = node.label;
          cursor.nodeId = node.nodeId;
          result.push(cursor);
        }
      }
      /*if (tagArray) {
        for (i in tagArray) {
          cursor = {};
          nodeId = tagArray[i];
  
          //Get the node of the child
          const tagResult = await tags.find({
            query: { nodeId, skippop: true }
          });
          if (tagResult) {
            console.log("TagResult", nodeId, tagResult);
            node = tagResult.data[0];
            cursor.label = node.label;
            cursor.nodeId = node.nodeId;
            result.push(cursor);
          } else {
            console.error('BadTag', nodeId);
          }
        }
      }*/
      if (relationArray) {
        for (i in relationArray) {
          cursor = {};
  
          nodeId = relationArray[i];
  
          //Get the node of the child
          const conResult = await conversation.find({
            query: { nodeId, skippop: true }
          });
          if (conResult) {
            node = conResult.data[0];
            //console.info('ConNode', nodeId, node);
            cursor.label = node.label;
            cursor.nodeId = node.nodeId;
            //cursor.isTag = false;
            result.push(cursor);
          } else {
            console.error('BadConnection', nodeId);
          }
        }
      }
      //console.info('AAA', subclassArray, instanceArray);
      if (subclassArray) {
        for (i in subclassArray) {
          cursor = {};
  
          nodeId = subclassArray[i];
  //console.info('SCA', nodeId);
          //Get the node of the child
          const conResult = await conversation.find({
            query: { nodeId, skippop: true }
          });
  
          node = conResult.data[0];
          console.info('SubNode', nodeId, node);
          cursor.label = node.label;
          cursor.nodeId = node.nodeId;
          result.push(cursor);
        }
      }
      if (instanceArray) {
        for (i in instanceArray) {
          cursor = {};
  
          nodeId = instanceArray[i];
  
          //Get the node of the child
          const conResult = await conversation.find({
            query: { nodeId, skippop: true }
          });
  
          node = conResult.data[0];
          cursor.label = node.label;
          cursor.nodeId = node.nodeId;
          result.push(cursor);
        }
      }
      //console.log("TREEEEEE", id, result);
      return result;
    }
  
    /**
     * A recursive tree builder
     * @param {*} rootNodeId
     * @param {*} isTag
     * @param {*} callback signature: (jsonTree)
     */
    async toJsTree(rootNodeId, isTag, level = 0) {
      var thisNode;
      var childArray;
      var respConv;
      level++;
  
      console.info('ToJsTree', isTag, { level, rootNodeId });
      // Use find to avoid populating the children
      if (isTag) {
        respConv = await tags.find({
          query: { nodeId: rootNodeId },
          skippop: true
        });
  
      } else {
        respConv = await conversation.find({
          query: { nodeId: rootNodeId },
          skippop: true
        });
      }
      console.dir(respConv);
  
      if (respConv.data.length < 1) {
        console.info('End', { level });
        return {};
      }
  
      const node = respConv.data[0];
      //console.info("TV-1", rootNodeId, JSON.stringify(node));
      thisNode = {};
      thisNode.nodeId = node.nodeId;
      thisNode.label = node.label;
      thisNode.img = node.imgsm;
      thisNode.expanded = true;
      childArray = await this.populateKids(
        node.questions,
        node.answers,
        node.pros,
        node.cons,
        //null, //node.tags,
        node.relations,
        node.subclasses,
        node.instances
      );
      thisNode.children = [];
      const arrPromises = childArray.map(child =>
        this.toJsTree(child.nodeId, child.isTag, level)
      );
      const children = await Promise.all(arrPromises);
  
      thisNode.children = children;
      
      // console.info('Going Back', thisNode)
      return thisNode;
    }
  
    /**
     * @method get
     * @description
     *
     * @param {*} nodeId
     * @returns
     * @memberof Service
     */
    async get(nodeId) {
      //console.info("TREEVIEW-GET", nodeId);
      try {
        // A recursive walk down a tree from a root node identified by id
        return await this.toJsTree(nodeId, false, 0);
      } catch (e) {
        console.error('Error fetching tree', e);
      }
    }
  
    async create(data, params) {
      if (Array.isArray(data)) {
        return Promise.all(data.map(current => this.create(current, params)));
      }
  
      return data;
    }
  
    async update(data) {
      return data;
    }
  
    async patch(data) {
      return data;
    }
  
    async remove(id) {
      return { id };
    }
  }
  
  module.exports = function(options) {
    return new Service(options);
  };
  
  module.exports.Service = Service;
  