const $ = require('jquery');
require('jstree');
const directoryTree = require('directory-tree');

const imageUtil = require('../utilities/image-utility');
const pathIdPairs = require('../utilities/path-id-pairs/path-id-pairs');
const pathIdPairsHandlerForTreeView = require('../utilities/path-id-pairs/path-id-pairs-handler-for-tree-view');

const treeView = $('#tree-view');

initialize();

function initialize() {
  treeView
    .on('changed.jstree', function (e, data) {
      cascadeDownSelectedState(data);
      exports.onChanged(data);
    })
    .jstree(
    {
      'core' : 
      {
        'data' : 
        [
          { "text" : "Root node", 
            "children" : 
            [
              { "text" : "Child node 1" },
              { "text" : "Child node 2",
                "children" : 
                [
                  { "text" : "Child node 2-1" }
                ] 
              },
              { "text" : "Child node 3",
                "state": { "disabled" : true },
                "children" : 
                [
                  { "text" : "Child node 3-1",
                    "state": { "disabled" : true }
                  }
                ] 
              }
            ]
          }
        ]
      },
    });
}

exports.update = function (selectedPath) {
  const directoryTreeRoot = directoryTree(selectedPath);
  pathIdPairs.reset();
  const dataForJsTree = getDataForJsTree(directoryTreeRoot);
  treeView.jstree(true).settings.core.data = dataForJsTree;
  treeView.jstree(true).deselect_all(true);
  treeView.jstree(true).close_all();
  treeView.jstree(true).refresh();
}

function getDataForJsTree(directoryTreeRoot) {
  if(!Array.isArray(directoryTreeRoot)){
    directoryTreeRoot = [directoryTreeRoot];
  }
  return processDirectoryTreeElementArray(directoryTreeRoot);
}

function processDirectoryTreeElementArray(directoryTreeElementArray) {
  var jstreeElementArray = [];
  directoryTreeElementArray.forEach(
    (directoryTreeElement) => {
      var jstreeElement = {
        id: pathIdPairsHandlerForTreeView.issueIdForTreeView(directoryTreeElement.path),
        text: directoryTreeElement.name,
        state: { disabled: isDisabled(directoryTreeElement) }
      };
      if(directoryTreeElement.hasOwnProperty("children")) {
        jstreeElement.children = processDirectoryTreeElementArray(directoryTreeElement.children);
        const isAllChildrenDisabled = jstreeElement.children.every(child => child.state.disabled);
        jstreeElement.state.disabled = isAllChildrenDisabled;
      }
      jstreeElementArray.push(jstreeElement);
    }
  );
  return jstreeElementArray;
}

function isDisabled(directoryTreeElement) {
  const isFile = () => directoryTreeElement.type === "file";
  const isFilenameExtensionSupported = () => 
    imageUtil.isSupportedFilenameExtension(directoryTreeElement.extension);
  return isFile() && !isFilenameExtensionSupported();
}

/**
 * Event to be fired when tree view is changed.
 * @param {*} data data property of jstree
 */
exports.onChanged = function(data){}; //Default event handler is assigned to avoid being undefined.

exports.getSelectedNodes = function(data) {
  var i, length, selectedNodes = [];
  for (i = 0, length = data.selected.length; i < length; i++) {
    const selectedNode = data.instance.get_node(data.selected[i]);
    selectedNodes.push(selectedNode);
  }
  return selectedNodes;
}

function cascadeDownSelectedState(data) {
  const selectedNodes = exports.getSelectedNodes(data);
  selectedNodes.forEach(node => selectChildrenRecursively(node));
}

function selectChildrenRecursively(parentNode) {
  parentNode.children
    .map(id => treeView.jstree(true).get_node(id))
    .filter(node => !node.state.disabled)
    .forEach(node => {
      treeView.jstree(true).select_node(node.id, "true", "true");
      selectChildrenRecursively(node);
    });
}
@@ -1453,17 +1453,22 @@
 ree = ge
-t
+nerate
 DataForJ
@@ -1690,17 +1690,22 @@
 ction ge
-t
+nerate
 DataForJ
@@ -1738,141 +1738,49 @@
 %0D%0A  
-if(!Array.isArray(directoryTreeRoot))%7B%0D%0A    directoryTreeRoot = %5BdirectoryTreeRoot%5D;%0D%0A  %7D%0D%0A  return processDirectoryTreeElementArra
+return generateDataForJsTreeRecursivel
 y(
+%5B
 dire
@@ -1788,24 +1788,25 @@
 toryTreeRoot
+%5D
 );%0D%0A%7D%0D%0A%0D%0Afun
@@ -1815,39 +1815,39 @@
 ion 
-processDirectoryTreeElementArra
+generateDataForJsTreeRecursivel
 y(di
@@ -2309,39 +2309,39 @@
 n = 
-processDirectoryTreeElementArra
+generateDataForJsTreeRecursivel
 y(di

Modify data generation for jsTree