let states = {"keylayouts":[[""],[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
["0",")"],
["1","!"],
["2","@"],
["3","#"],
["4","$"],
["5","%"],
["6","^"],
["7","&"],
["8","*"],
["9","("],
["*"],
["#"],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
["ㅁ"],
["ㅠ"],
["ㅊ"],
["ㅇ"],
["ㄷ","ㄸ"],
["ㄹ"],
["ㅎ"],
["ㅗ"],
["ㅑ"],
["ㅓ"],
["ㅏ","ㆍ"],// A & ARAE-A
["ㅣ"],
["ㅡ"],
["ㅜ"],
["ㅐ","ㅒ"],
["ㅔ","ㅖ"],
["ㅂ","ㅃ"],
["ㄱ","ㄲ"],
["ㄴ"],
["ㅅ","ㅆ"],
["ㅕ"],
["ㅍ"],
["ㅈ","ㅉ"],
["ㅌ","¤"],
["ㅛ"],
["ㅋ"],
[",","<"],
[".",">"],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[" "],
[undefined],
[undefined],
[undefined],
["\n"],
["\x08"],
["`","~"],
["-","_"],
["=","+"],
["[","{"],
["]","}"],
["\\","|"],
[";",":"],
["'",'"'],
["/","?"],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
["0"],
["1"],
["2"],
["3"],
["4"],
["5"],
["6"],
["7"],
["8"],
["9"],
["/"],
["*"],
["-"],
["+"],
["."],
["."],
["\n"],
["="],
["("],
[")"],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined],
[undefined]
],
"code":0,
"buf":[]}
let reset = () => {
    states.code = 0
    states.buf = []
}
let inRange = (s,a,b) => (a <= s && s <= b)
let isHangul = (s) => s !== undefined && inRange(s.charCodeAt(0), 0x3131, 0x318E)
let isConsonant = (s) =>  s !== undefined && inRange(s.charCodeAt(0), 0x3131, 0x314E)
let isVowel = (s) => s !== undefined && (inRange(s.charCodeAt(0), 0x314F, 0x3163) || inRange(s.charCodeAt(0), 0x318D, 0x318E))
let isVowelSuper = (s) => s !== undefined && inRange(s.charCodeAt(0), 0x318D, 0x318E)
let isJongseongConsonant = (s) =>  s !== undefined && (inRange(s.charCodeAt(0), 0x3131, 0x314E) && !([0x3138, 0x3143, 0x3149].includes(s.charCodeAt(0))))
let isJungseongDigraph1 = (s) => s !== undefined && ([0x3157, 0x315C].includes(s.charCodeAt(0)))
let isJungseongDigraphO = (s) => s !== undefined && ([0x314F, 0x3150, 0x3163].includes(s.charCodeAt(0)))
let isJungseongDigraphU = (s) => s !== undefined && ([0x3153, 0x3154, 0x3163].includes(s.charCodeAt(0)))
let isJungseongDigraphEU = (s) => s !== undefined && ([0x3163].includes(s.charCodeAt(0)))
let isJungseongDigraphAA = (s) => s !== undefined && ([0x3163].includes(s.charCodeAt(0)))
let isJongseongDigraphG = (s) => s !== undefined && ([0x3145].includes(s.charCodeAt(0)))
let isJongseongDigraphN = (s) => s !== undefined && ([0x3148, 0x314E].includes(s.charCodeAt(0)))
let isJongseongDigraphR = (s) => s !== undefined && ([0x3131, 0x3141, 0x3142, 0x3145, 0x314C, 0x314D, 0x314E].includes(s.charCodeAt(0)))
let isJongseongDigraphB = (s) => s !== undefined && ([0x3145].includes(s.charCodeAt(0)))
let jungseongDigraphsO = {"\u314F":"\u3158", "\u3150":"\u3159", "\u3163":"\u315A"}
let jungseongDigraphsU = {"\u3153":"\u315D", "\u3154":"\u315E", "\u3163":"\u315F"}
let jungseongDigraphsEU = {"\u3163":"\u3162"}
let jungseongDigraphsAA = {"\u3163":"\u318E"}
let jongseongDigraphsG = {"\u3145":"\u3133"}
let jongseongDigraphsN = {"\u3148":"\u3135", "\u314E":"\u3136"}
let jongseongDigraphsR = {"\u3131":"\u313A", "\u3141":"\u313B", "\u3142":"\u313C", "\u3145":"\u313D", "\u314C":"\u313E", "\u314D":"\u313F", "\u314E":"\u3140"}
let jongseongDigraphsB = {"\u3145":"\u3144"}
let isThisCharJongseongDigraph = (s) => s !== undefined && ([0x3133, 0x3135, 0x3136, 0x313A, 0x313B, 0x313C, 0x313D, 0x313E, 0x313F, 0x3140, 0x3144].includes(s.charCodeAt(0)))
let isThisCharJungseongDigraph = (s) => s !== undefined && ([0x3158, 0x3159, 0x315A, 0x315D, 0x315E, 0x315F, 0x3162, 0x318E].includes(s.charCodeAt(0)))
let choseongTable = {"\u3131":0,"\u3132":1,"\u3134":2,"\u3137":3,"\u3138":4,"\u3139":5,"\u3141":6,"\u3142":7,"\u3143":8,"\u3145":9,"\u3146":10,"\u3147":11,"\u3148":12,"\u3149":13,"\u314A":14,"\u314B":15,"\u314C":16,"\u314D":17,"\u314E":18}
let jongseongTable = {"\u3131":0,"\u3132":1,"\u3133":2,"\u3134":3,"\u3135":4,"\u3136":5,"\u3137":6,"\u3139":7,"\u313A":8,"\u313B":9,"\u313C":10,"\u313D":11,"\u313E":12,"\u313F":13,"\u3140":14,"\u3141":15,"\u3142":16,"\u3144":17,"\u3145":18,"\u3146":19,"\u3147":20,"\u3148":21,"\u314A":22,"\u314B":23,"\u314C":24,"\u314D":25,"\u314E":26}
let detachJongseongDigraph = {
"\u3133":["\u3131","\u3145"],
"\u3135":["\u3134","\u3148"],
"\u3136":["\u3134","\u314E"],
"\u313A":["\u3139","\u3131"],
"\u313B":["\u3139","\u3141"],
"\u313C":["\u3139","\u3142"],
"\u313D":["\u3139","\u3145"],
"\u313E":["\u3139","\u314C"],
"\u313F":["\u3139","\u314D"],
"\u3140":["\u3139","\u314E"],
"\u3144":["\u3142","\u3145"]
}
let detachJungseongDigraph = {
"\u3158":["\u3157","\u314F"],
"\u3159":["\u3157","\u3150"],
"\u315A":["\u3157","\u314E"],
"\u315D":["\u315C","\u3153"],
"\u315E":["\u315C","\u3154"],
"\u315F":["\u315C","\u3163"],
"\u3162":["\u3161","\u3163"],
"\u318E":["\u318D","\u3163"]
}
let bufAssemble = (isPreview) => {
    // nothing on the buffer
    if (states.buf[0] === undefined && states.buf[1] === undefined && states.buf[2] === undefined)
        return ''
    // Hangul: I P F
    else if (!isVowelSuper(states.buf[1]) && isConsonant(states.buf[0]) && isVowel(states.buf[1]) && isConsonant(states.buf[2])) {
        let i = choseongTable[states.buf[0]]
        let p = states.buf[1].charCodeAt(0) - 0x314F
        let f = jongseongTable[states.buf[2]] + 1
        return String.fromCodePoint(0xAC00 + (i * 588) + (p * 28) + f)
    }
    // Hangul: I P x
    else if (!isVowelSuper(states.buf[1]) && isConsonant(states.buf[0]) && isVowel(states.buf[1]) && undefined == states.buf[2]) {
        let i = choseongTable[states.buf[0]]
        let p = states.buf[1].charCodeAt(0) - 0x314F
        return String.fromCodePoint(0xAC00 + (i * 588) + (p * 28))
    }
    else if (isVowelSuper(states.buf[1]) && isConsonant(states.buf[0]) && isConsonant(states.buf[2])) {
        let i = choseongTable[states.buf[0]]
        let p = (states.buf[1].charCodeAt(0) - 0x318D) * 3
        let f = jongseongTable[states.buf[2]] + 1
        return String.fromCodePoint(0x1100 + i, 0x119E + p, 0x11A7 + f)
    }
    else if (isVowelSuper(states.buf[1]) && isConsonant(states.buf[0])) {
        let i = choseongTable[states.buf[0]]
        let p = (states.buf[1].charCodeAt(0) - 0x318D) * 3
        return String.fromCodePoint(0x1100 + i, 0x119E + p)
    }
    else
        return states.buf.join('')
}
//let bufDebugStringify = (buf) => [0,1,2].map(i => (buf[i] == undefined) ? "·" : `\\u${buf[i].codePointAt(0).toString(16).toUpperCase()}`).join(' ')
let bufDebugStringify = (buf) => [0,1,2].map(i => (buf[i] == undefined) ? "·" : `${buf[i]}`).join(' ')
return Object.freeze({"n":"두벌식 표준","v":"none","c":"CuriousTo\uA75Bvald","m":"rewrite",
"t":states.keylayouts.map(it => [it[0],it[1]]),
"l":"koKR",
// return: [delete count, composed output]
"accept":(headkey,shiftin,altgrin)=>{
    let layer = 1*shiftin// + 2*altgrin
    states.code = 1

    let s = states.keylayouts[headkey][layer] || states.keylayouts[headkey][0]

    if (isHangul(s)) {
        let bufIndex = (isJongseongConsonant(s) && isConsonant(states.buf[0]) && undefined !== states.buf[1]) ? 2 :
                (isVowel(s) && isConsonant(states.buf[0])) ? 1 : 0
        let vowelCollision = isVowel(states.buf[1]) && isVowel(s)
        let hasJongseongAlready = (states.buf[2] !== undefined)

//        console.log(`accepting hangul '${s}' at buf[${bufIndex}] (vowelCollision = ${vowelCollision})`)

        // ㅘ ㅙ ㅚ
        if (!hasJongseongAlready && 1 == bufIndex && "\u3157" == states.buf[1] && isJungseongDigraphO(s)) {
            states.buf[1] = jungseongDigraphsO[s]
        }
        // ㅝ ㅞ ㅟ
        else if (!hasJongseongAlready && 1 == bufIndex && "\u315C" == states.buf[1] && isJungseongDigraphU(s)) {
            states.buf[1] = jungseongDigraphsU[s]
        }
        // ㅢ
        else if (!hasJongseongAlready && 1 == bufIndex && "\u3161" == states.buf[1] && isJungseongDigraphEU(s)) {
            states.buf[1] = jungseongDigraphsEU[s]
        }
        // ㆎ
        else if (!hasJongseongAlready && 1 == bufIndex && "\u318D" == states.buf[1] && isJungseongDigraphAA(s)) {
            states.buf[1] = jungseongDigraphsAA[s]
        }
        // ㄳ
        else if (2 == bufIndex && "\u3131" == states.buf[2] && isJongseongDigraphG(s)) {
            states.buf[2] = jongseongDigraphsG[s]
        }
        // ㄵ ㄶ
        else if (2 == bufIndex && "\u3134" == states.buf[2] && isJongseongDigraphN(s)) {
            states.buf[2] = jongseongDigraphsN[s]
        }
        // ㄺ ㄻ ㄼ ㄽ ㄾ ㄿ ㅀ
        else if (2 == bufIndex && "\u3139" == states.buf[2] && isJongseongDigraphR(s)) {
            states.buf[2] = jongseongDigraphsR[s]
        }
        // ㅄ
        else if (2 == bufIndex && "\u3142" == states.buf[2] && isJongseongDigraphB(s)) {
            states.buf[2] = jongseongDigraphsB[s]
        }
        // key inputs that bufIndex collides (end compose and accept incoming char as a new char state)
        else if (states.buf[bufIndex] !== undefined) {
            let oldbufstr = bufDebugStringify(states.buf)
            let sendout = ''
            // e.g. 닳 -> 달해
            if (vowelCollision && isThisCharJongseongDigraph(states.buf[2])) {
                let digraphs = detachJongseongDigraph[states.buf[2]]
                let newbuf = [digraphs[1], s]
                states.buf = [states.buf[0], states.buf[1], digraphs[0]]
                sendout = bufAssemble()
                states.buf = newbuf

//                console.log(`rearr1: ${bufDebugStringify(states.buf)} -> 1,${sendout}${bufAssemble()}`)
                return ["1", sendout + bufAssemble()]
            }
            // e.g. 둣 -> 두사
            else if (vowelCollision && states.buf[2]) {
                let newbuf = [states.buf[2], s]
                states.buf = [states.buf[0], states.buf[1]]
                sendout = bufAssemble()
                states.buf = newbuf

//                console.log(`rearr2: ${bufDebugStringify(states.buf)} -> 1,${sendout}${bufAssemble()}`)
                return ["1", sendout + bufAssemble()]
            }
            // e.g. 가 -> 가ㅏ (error; buf: ㄱ ㅏ · -> · ㅏ ·, that illegally allows C ㅏ · if any choseong is directly followed)
            else if (vowelCollision) {
                reset()
//                console.log(`rearr4: 0,${s}`)
                return ["0", s]
            }
            // Choseong after finalised IPK-syllable (ㅇ as in 밥이)
            else {
                reset()
                if (bufIndex == 2)
                    states.buf[0] = s
                else
                    states.buf[bufIndex] = s

//                console.log(`rearr3: ${bufDebugStringify(states.buf)} -> 0,${bufAssemble()}`)
                return ["0", bufAssemble()]
            }
        }
        else {
            let bufferEmpty = (states.buf[0] === undefined)
            states.buf[bufIndex] = s

            // e.g. 2nd ㅏ as in 가ㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏ
            // this merely cuts the "loop" where 4,6,8...th ㅏ are also inflicted
            //
            if (isVowel(states.buf[0]) && isVowel(states.buf[1])) {
                reset()
//                console.log(`assem0: 0,${s}`)
                return ["0", s]
            }
            // e.g. ㅁ and ㅂ as in 물과␣백두산
            else if (bufferEmpty) {
//                console.log(`assem1: ${bufDebugStringify(states.buf)} -> 0,${bufAssemble()}`)
                return ["0", bufAssemble()]
            }
            // e.g. ㅜ ㄹ ㅐ ㄱ ㅜ ㅏ ㄴ as in 물과␣백두산
            else {
//                console.log(`assem2: ${bufDebugStringify(states.buf)} -> 1,${bufAssemble()}`)
                return ["1", bufAssemble()]
            }
        }

//        console.log(`assem-digraph: ${bufDebugStringify(states.buf)} -> 1,${bufAssemble()}`)
        return ["1", bufAssemble()]
    }
    else {
        reset()
        // directly print out the character without using the buffer
//        console.log(`sending1 out: 0,${s}`)
        return ["0", s]
    }
},
"backspace":()=>{
    // disassemble jung/jongseong digraphs
//    let oldbufstr = bufDebugStringify(states.buf)
    let last = states.buf.pop()

    if (last !== undefined) {
        // detach jongseong
        if (isThisCharJongseongDigraph(last))
            states.buf[2] = detachJongseongDigraph[last][0]
        // detach jungseong
        else if (isThisCharJungseongDigraph(last))
            states.buf[1] = detachJungseongDigraph[last][0]
    }

    if (states.buf.length == 0) reset()

//    let newbufstr = bufDebugStringify(states.buf)
//    console.log(`popping assembly: ${oldbufstr} -> ${newbufstr}`)

    return bufAssemble()
},
"end":()=>{
//    console.log(`end composing`)
    let ret = bufAssemble()
    reset()
    return ret
},
"reset":()=>{ reset() },
"composing":()=>(states.code!=0)
})