/*
 *
 * CS3283/4 - query.js
 * This class deals with query statements for the facade storage
 * This class will communicate with sqlCommands to get a certain command for
 *
 */

import SqlCommands from 'src/database/sql-related/sqlCommands';
import constants from 'src/database/constants';
import StringManipulator from 'src/database/stringManipulation';

const stringManipulator = new StringManipulator();

const sqlCommands = new SqlCommands();

// dummy function to init sequence running for code aesthetic below
function initPromiseSequence() {
  return new Promise((resolve, reject) =>
    resolve()
  );
}

export default class query {
  static constructor() {}

  static getAllFilesAndFolders() {
    return new Promise((resolve, reject) => {
      const promiseArr = [];
      initPromiseSequence()
      .then(() =>
        sqlCommands.getFullTableData(constants.ENTITIES_FILE_MANAGER)
        .then(fileArr => promiseArr.push(fileArr))
        .catch(sqlError => reject(sqlError)))
      .then(() =>
        sqlCommands.getFullTableData(constants.ENTITIES_FOLDER)
        .then(folderArr => promiseArr.push(folderArr))
        .catch(sqlError => reject(sqlError)))
      .then(() => resolve(promiseArr))
      .catch(sqlErr => reject(sqlErr));
    });
  }

  static isTableExistsInDatabase(tableName) {
    return new Promise((resolve, reject) =>
      sqlCommands.getFullTableData(tableName)
      .then(() => resolve(true))
      .catch(sqlErr => resolve(false))
    );
  }

  static getFullTableData(tableName) {
    return new Promise((resolve, reject) =>
      sqlCommands.getFullTableData(tableName)
      .then(data => resolve(data))
      .catch(sqlError => reject(sqlError))
    );
  }

  static searchString(searchString) {
    return new Promise((resolve, reject) => {
      const promiseArr = [];

      initPromiseSequence()
      .then(() => sqlCommands.partialSearchFileName(searchString)
        .then(fileArr => promiseArr.push(fileArr))
        .catch(sqlError => reject(sqlError)))
      .then(() => sqlCommands.partialSearchFolderName(searchString)
        .then(folderArr => promiseArr.push(folderArr))
        .catch(sqlError => reject(sqlError)))
      .then(() => resolve(promiseArr))
      .catch(sqlErr => reject(sqlErr));
    });
  }

  static loadFolder(folderId) {
    return new Promise((resolve, reject) => {
      const promiseArr = [];

      initPromiseSequence()
      .then(() => sqlCommands.loadFilesFromFolder(folderId)
        .then(fileArr => promiseArr.push(fileArr))
        .catch(sqlError => reject(sqlError)))
      .then(() => sqlCommands.loadFoldersFromFolder(folderId)
        .then(folderArr => promiseArr.push(folderArr))
        .catch(sqlError => reject(sqlError)))
      .then(() => resolve(promiseArr))
      .catch(sqlError => reject(sqlError));
    });
  }

  static loadFile(fileId) {
    return new Promise((resolve, reject) =>
      sqlCommands.loadFile(fileId)
      .then((fileContent) => {
        fileContent = stringManipulator.replaceAll(fileContent, '\\"', '"');
        resolve(fileContent);
      })
      .catch(sqlError => reject(sqlError))
    );
  }
}

@@ -2352,34 +2352,42 @@
       const 
-promiseArr
+fileAndFolderArray
  = %5B%5D;%0A%0A    
@@ -2491,34 +2491,42 @@
 (fileArr =%3E 
-promiseArr
+fileAndFolderArray
 .push(fileAr
@@ -2660,34 +2660,42 @@
 olderArr =%3E 
-promiseArr
+fileAndFolderArray
 .push(folder
@@ -2768,34 +2768,42 @@
  =%3E resolve(
-promiseArr
+fileAndFolderArray
 ))%0A      .ca

Update name of promiseArr in query.js to fileAndFolderArray