{"size": 2833, "ext": "js", "max_stars_repo_path": "src/vigenere-cipher.js", "max_stars_repo_name": "Feintty/basic-js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/vigenere-cipher.js", "max_issues_repo_name": "Feintty/basic-js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/vigenere-cipher.js", "max_forks_repo_name": "Feintty/basic-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.2946428571, "max_line_length": 89, "alphanum_fraction": 0.5806565478}
const CustomError = require("../extensions/custom-error");

class VigenereCipheringMachine {
  constructor(reverse){
    this.reverse=reverse;
  }
  encrypt(message,key) {
    let vig = new Viginere();
    if(this.reverse == true||this.reverse ==undefined){
    return vig.encrypt(message,key);
    }
    else {
      return vig.encrypt(message,key).split("").reverse().join("");
    }
  } 
  decrypt(message,key) {
    let vig = new Viginere();
    if(this.reverse == true||this.reverse ==undefined){
    return vig.decrypt(message,key);
    }
    else {
      return vig.decrypt(message,key).split("").reverse().join("");
    }
  }

}
class Viginere {
  constructor() {
    this.arr = function () {
      return "abcdefghijklmnopqrstuvwxyz".split("");
    };
    this.result = "";
  }

  shift(index) {
    return this.arr().splice(index).concat(this.arr());
  }

  letterEnc(firstLetter, secondLetter) {
    if (this.arr().includes(firstLetter)) {
      return this.shift(this.arr().indexOf(firstLetter))[
        this.arr().indexOf(secondLetter)
      ];
    } else return firstLetter;
  }

  encrypt(message, key) {
    message = message.toLowerCase();
    key = key.toLowerCase();
    let index = 0;
    if (message.length > key.length)
      while (key.length !== message.length) {
        key += key[index++];
      }
    else {
      key = key.split("");
      key.splice(message.length, key.length);
      key = key.join("");
    }

    message.split("").forEach((element,i) => {
      if(!this.arr().includes(element)){
        key = key.split("");
        key.splice(i,0,element);
        key = key.join("");
      }
    });

    for (let i = 0; i < message.length; i++) {
        this.result += this.letterEnc(message[i], key[i]);
    }
    let enctypted = this.result.toUpperCase();
    this.result = "";
    return enctypted;
  }

  letterDec(firstLetter,secondLetter){
    if(this.arr().includes(secondLetter))
    return this.arr()[this.shift(this.arr().indexOf(firstLetter)).indexOf(secondLetter)];
    else return secondLetter;
  }

  decrypt(message,key){
    message = message.toLowerCase();
    key = key.toLowerCase();
    let index = 0;
    if (message.length > key.length)
      while (key.length !== message.length) {
        key += key[index++];
      }
    else {
      key = key.split("");
      key.splice(message.length, key.length);
      key = key.join("");
    }
    message.split("").forEach((element,i) => {
      if(!this.arr().includes(element)){
        key = key.split("");
        key.splice(i,0,element);
        key = key.join("");
      }
    });
    for (let i = 0; i < message.length; i++) {
      this.result += this.letterDec(key[i], message[i]);
  }
  let decrypted = this.result.toUpperCase();
  this.result = "";
  return decrypted;
  }
}
module.exports = VigenereCipheringMachine;
