/* This Python script will generate the scrambled text and output a list of alterations to use as a key

#!/usr/bin/env python3
import sys
import random

INPUT_STRING = sys.argv[1]

incp = list(INPUT_STRING)
transformations = []

for i in range(len(incp) * 3):
    a = random.randint(0, len(incp)-1)
    b = None
    while b is None or b == a:
        b = random.randint(0, len(incp)-1)

    incp[a], incp[b] = incp[b], incp[a]

    transformations.append((a, b))

print("const scrambled = " + repr("".join(incp)) + ";")
print("const key = " + str(list(reversed(map(list, transformations)))) + ";")

*/

String.prototype.replaceAt = function (index, replacement) {
  return this.substring(0, index) + replacement + this.substring(index + replacement.length);
};

const scrambledEmailAddr = "ieahola.pntkne@l";
const keyForEmailAddr = [[7, 5], [14, 6], [0, 6], [0, 8], [10, 4], [14, 4], [8, 12], [0, 3], [15, 2], [5, 14], [3, 12], [14, 1], [7, 0], [13, 9], [13, 2], [14, 8], [3, 1], [4, 8], [10, 4], [2, 1], [1, 13], [4, 2], [4, 7], [10, 1], [10, 1], [15, 11], [1, 2], [14, 11], [6, 2], [0, 4], [4, 9], [12, 14], [5, 3], [3, 6], [15, 4], [12, 14], [9, 8], [4, 5], [3, 8], [1, 10], [10, 2], [15, 6], [12, 4], [14, 6], [4, 8], [5, 7], [2, 4], [9, 6]];

const unscramble = (ciphertext, key) => {
  for (let i = 0; i < key.length; i += 1) {
    const op = key[i];
    const t = ciphertext.charAt(op[0]);
    ciphertext = ciphertext.replaceAt(op[0], ciphertext.charAt(op[1]));
    ciphertext = ciphertext.replaceAt(op[1], t);
  }
  return ciphertext;
};
