{"size": 2373, "ext": "js", "max_stars_repo_path": "src/vigenere-cipher.js", "max_stars_repo_name": "vladdominator/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": "vladdominator/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": "vladdominator/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.5161290323, "max_line_length": 143, "alphanum_fraction": 0.5343447113}
const CustomError = require("../extensions/custom-error");

class VigenereCipheringMachine {
  letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

  mode = true;

  constructor(flag = true) {
    this.mode = flag;
  }


  encrypt(message, key) {
    if (arguments.length < 2) {
      throw new Error();
    }

    let strArray = message.toUpperCase().split('');
    let keyAbout = key.toUpperCase().split('');

    let ArrayPrototype = [];
    let j = 0;
    for (let i = 0; i < strArray.length; i++) {
      if (this.letters.includes(strArray[i])) {
        ArrayPrototype[i] = [strArray[i],keyAbout[j]];
        j++;
        if (j === keyAbout.length) {
          j = 0;
        }
      } else {
        ArrayPrototype[i] = strArray[i];
      }
    }

    let strArrayDefend = [];
    for (let i = 0; i < ArrayPrototype.length; i++) {
      if (ArrayPrototype[i] instanceof Array) {
        let pos = (this.letters.indexOf(ArrayPrototype[i][0]) + this.letters.indexOf(ArrayPrototype[i][1])) % 26;
        strArrayDefend[i] = this.letters[pos];
      } else {
        strArrayDefend[i] = ArrayPrototype[i];
      }
    }

    if (!this.mode) {
      strArrayDefend.reverse();
    }

    return strArrayDefend.join('');
  } 

  decrypt(message, key) {
    if (arguments.length < 2) {
      throw new Error();
    }

    let strArray = message.toUpperCase().split('');
    let keyAbout = key.toUpperCase().split('');

    let ArrayPrototype = [];
    let j = 0;
    for (let i = 0; i < strArray.length; i++) {
      if (this.letters.includes(strArray[i])) {
        ArrayPrototype[i] = [strArray[i], keyAbout[j]];
        j++;
        if (j === keyAbout.length) {
          j = 0;
        }
      } else {
        ArrayPrototype[i] = strArray[i];
      }
    }

    let strArrayDefend = [];
    for (let i = 0; i < ArrayPrototype.length; i++) {
      if (ArrayPrototype[i] instanceof Array) {
        let pos = (this.letters.indexOf(ArrayPrototype[i][0]) - this.letters.indexOf(ArrayPrototype[i][1]) + 26) % 26;
        strArrayDefend[i] = this.letters[pos];
      } else {
        strArrayDefend[i] = ArrayPrototype[i];
      }
    }

    if (!this.mode) {
      strArrayDefend.reverse();
    }

    return strArrayDefend.join('');
  }
}

module.exports = VigenereCipheringMachine;
