/* global InvertedIndex */
// import the angular package and other components

const angular = require('angular');

angular.module('indexApp', [])
  .controller('InvertedIndexController', ['$scope', ($scope) => {
    const index = new InvertedIndex();

    $scope.files = {};
    $scope.fileNames = [];
    $scope.searchText = '';
    $scope.showIntro = true;
    $scope.hideTable = true;
    $scope.showResult = false;

    function findWrongFormat(element) {
      if (!{}.hasOwnProperty.call(element, 'title') || !{}.hasOwnProperty.call(element, 'text')) {
        return true;
      }
      return false;
    }

    $scope.uploadFile = () => {
      $scope.error = '';
      $scope.success = '';
      const file = document.forms['upload-form']['json-file'].files[0];
      const fileName = file.name.replace(/\s+/, '');
      if (file) {
        if (!fileName.match(/\.json$/i)) {
          $scope.error = 'Invalid file format';
          return;
        }
        const reader = new FileReader();
        reader.onload = (evt) => {
          try {
            const jsonData = JSON.parse(evt.target.result);
            if (jsonData.find(findWrongFormat)) {
              $scope.$apply(() => {
                $scope.error = `The .json file did not follow
                the required format`;
              });
              return;
            } else if ($scope.fileNames.includes(fileName)) {
              $scope.$apply(() => {
                $scope.error = 'The file has been uploaded before';
              });
              return;
            } else if (jsonData.length < 1 || Array.isArray(jsonData === false)) {
              $scope.$apply(() => {
                $scope.error = 'This file is empty or not an Array of object';
              });
              return;
            }
            $scope.$apply(() => {
              $scope.fileNames.push(fileName);
              $scope.files[fileName] = jsonData;
              $scope.success = 'The file has been successfully uploaded';
            });
          } catch (error) {
            $scope.$apply(() => {
              $scope.error = 'Invalid .json file';
            });
          }
        };
        reader.readAsBinaryString(file);
      }
    };

    $scope.createIndex = (obj) => {
      const fileData = $scope.files[obj];
      index.createIndex(obj, fileData);
    };

    $scope.getIndex = (title) => {
      $scope.indexed = index.getIndex(title);
      $scope.document = $scope.files[title];
      $scope.title = title;
      $scope.showIntro = false;
      $scope.hideTable = false;
      $scope.showResult = false;
    };

    $scope.searchIndex = () => {
      const searchItem = $scope.searchText;
      const file = $scope.selected;

      if (file === undefined) {
        $scope.success = '';
        $scope.error = 'You are searching an unindexed file';
      } else if (file === 'all') {
        $scope.searchResult = index.searchIndex(searchItem, file);
        $scope.searchTerms = searchItem;
        $scope.showResult = true;
        $scope.hideTable = true;
      } else {
        $scope.searchResult = index.searchIndex(searchItem, file);
        $scope.searchTerms = searchItem;
        $scope.showResult = true;
        $scope.hideTable = true;
      }
    };
  }]);

@@ -609,16 +609,149 @@
 %0A    %7D%0A%0A
+    function isJson(file) %7B%0A      if (file && file.name.replace(/%5Cs+/, '').match(/%5C.json$/i)) %7B%0A        return true;%0A      %7D %0A    %7D%0A%0A
     $sco
@@ -965,134 +965,23 @@
 if (
-file) %7B%0A        if (!fileName.match(/%5C.json$/i)) %7B%0A          $scope.error = 'Invalid file format';%0A          return;%0A        %7D
+isJson(file)) %7B
 %0A   
@@ -2868,218 +2868,8 @@
 e';%0A
-      %7D else if (file === 'all') %7B%0A        $scope.searchResult = index.searchIndex(searchItem, file);%0A        $scope.searchTerms = searchItem;%0A        $scope.showResult = true;%0A        $scope.hideTable = true;%0A
     

refactor code on index.js