export default function symmetryEncrypt(text) {
  if (text === '') {
    return '';
  }

  try {
    const base64Code = [
      '0',
      '1',
      '2',
      '3',
      '4',
      '5',
      '6',
      '7',
      '8',
      '9',
      '+',
      '/',
      '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',
      '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',
      '=',
    ];
    const empty = 0;
    const byteMessage = Array.from(text, (c) => c.charCodeAt(0));
    let outmessage;
    const messageLen = byteMessage.length;
    let page = Math.floor(messageLen / 3);
    let use = 0;

    if ((use = messageLen % 3) > 0) {
      for (let i = 0; i < 3 - use; i++) byteMessage.push(empty);
      page++;
    }

    outmessage = [];
    for (let i = 0; i < page; i++) {
      const instr = [
        byteMessage[i * 3],
        byteMessage[i * 3 + 1],
        byteMessage[i * 3 + 2],
      ];
      const outstr = new Array(4).fill(0);

      outstr[0] = instr[0] >> 2;
      outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);

      if (instr[1] !== empty) {
        outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);
      } else {
        outstr[2] = 64;
      }

      if (instr[2] !== empty) {
        outstr[3] = instr[2] & 0x3f;
      } else {
        outstr[3] = 64;
      }

      outmessage.push(
        base64Code[outstr[0]],
        base64Code[outstr[1]],
        base64Code[outstr[2]],
        base64Code[outstr[3]],
      );
    }

    const finalStr = 'Symmetry_' + outmessage.join('');
    return finalStr;
  } catch (e) {
    throw e;
  }
}
