const imposter = `
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKK00OOOOOOkkkOO0KKKKKKKKKKKKK
KKKKKKKKKK0Oxl.... . .-.. coxO0KKKKKKKKK
KKKKKKKKKOo.-.ldxxxxxxddol. --lx0KKKKKKK
KKKKKKKK0o- oO0KKKKKKKKKK00ko..-lk0KKKKK
KKKKKKK0d-.d0KKK0Oxddollccclc. ..ck0KKKK
KKKKKK0k .lOKKKOl.. - ccloooolc....oO0KK
KKKKK00d .d0KK0o. okOO000KKKK00Okd...x0K
KOkdolc .cOKKKOl- kKKKKKKKKKKKKKKKOl-ckK
kc- --- -o0KKKOo. x0KKKKKKKKKKKKKK0d--xK
l. d00k..o0KKK0x- cxO0KKKKKKKKKK0ko..ckK
c.cOK0x .o0KKKK0d. -- odkkkkxdol.--.ck0K
c lOK0d--d0KKKKK0ko- ... - . .-cl. .d0KK
.-oOK0d-.d0KKKKKKK00OOkxxxxkkOO0Kx. d0KK
-.d0K0d-.o0KKKKKKKKKKKKKKKKKKKKKKx--oOKK
 .d0K0d. d0KKKKKKKKKKKKKKKKKKKKKKx--lOKK
 .d0K0d--d0KKKKKKKKKKKKKKKKKKKKKKx. oOKK
--oOK0x- o0KKKKKKKKKKKKKKKKKKKKKKd..d0KK
c.cOK0k -o0KKKKKKKKKKKKKKKKKKKKKOl .kKKK
d .lxkd-.o0KKKKKKK0OOO00KKKKKKKKk .cOKKK
0dc.-. .-cOKKKKK0kl. --cccloxO00d- o0KKK
K00kxxdc--k0KKKK0d .- oc--lkO0KOc -kKKKK
KKKKKK0o--x0KKKK0o -.o0d -xKKKKkc-cOKKKK
KKKKKK0d. d0KKKK0o..-d0x .lkkkko..d0KKKK
KKKKKK0x .lO0KK0.-x-lx00o..      lOKKKKK
`
const morseCode = {
    "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 convertToMorse = (str) => str.toUpperCase().split("").map(el => morseCode[el] ? morseCode[el] : el).join(" ");

const main = (str, key) => {

   const morse = convertToMorse(str).split("")
   let i = 0;
   let j = 0;

   const newImposter = imposter.split("").map(imp => {
       if(imp === "." || imp === "-" || imp === " ") {
           if(i < morse.length) {
               return morse[i++]
           } else {
               return ' '
           }
       } else {
           if(j < key.length && imp !== '\n') {
               return key[j++]
           } else {
               return imp
           }
       }
   }).join("")

   console.log(newImposter)

   return newImposter
}

const map = {
    a: 'v',
    b: 'u',
    c: 'w',
    d: 'e',
    e: 'n',
    f: 'd',
    g: 'l',
    h: 'g',
    i: 'z',
    j: 'k',
    k: 'b',
    l: 't',
    m: 's',
    n: 'h',
    o: 'p',
    p: 'o',
    q: 'c',
    r: 'm',
    s: 'y',
    t: 'x',
    u: 'j',
    v: 'f',
    w: 'i',
    x: 'a',
    y: 'r',
    z: 'q',
    "?": '?',
    "!": '!'
}

const flipMap = Object.fromEntries(Object.entries(map).map(([k, v]) => [v, k]))
const encode = (str) => {
    console.log(str)
    const val = str.split("").map(c => flipMap[c]).join('')
    console.log(val)
    return val
}


const flipMorse = Object.fromEntries(Object.entries(morseCode).map(([k, v]) => [v, k]))
const convertFromMorse = (str) => str.split(" ").map(el => flipMorse[el] ? flipMorse[el] : el).join("")
const decode = (imposter) => {
    const val = convertFromMorse(imposter.split('').filter(imp => imp === '.' || imp === '-' || imp === ' ').join('')).toLowerCase().split('').map(c => map[c]).join('')
    console.log(val)
    return val
}

decode(main(encode('helloisthisamongusimposterimposterfromamongus'), Object.entries(map).flat().join('')))