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","A","日"],
["b","B","月"],
["c","C","金"],
["d","D","木"],
["e","E","水"],
["f","F","火"],
["g","G","土"],
["h","H","竹"],
["i","I","戈"],
["j","J","十"],
["k","K","大"],
["l","L","中"],
["m","M","一"],
["n","N","弓"],
["o","O","人"],
["p","P","心"],
["q","Q","手"],
["r","R","口"],
["s","S","尸"],
["t","T","廿"],
["u","U","山"],
["v","V","女"],
["w","W","田"],
["x","X","難"],
["y","Y","卜"],
["z","Z","造"],
["，","《"],
["。","》"],
[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]
],
"dict":IMEProvider.requestDictionary("cj5-tc.han"),
"code":0, //0: not composing, 1: composing (has candidates), 2: composing (no candidates)
"buf":"",
"candidates":""/*comma-separated values*/}
let reset = () => {
    states.code = 0
    states.buf = ""
    states.candidates = ""
}
let getCandidatesUsingBuf = () =>  {
    states.candidates = states.dict.getCangjie(states.buf) // \x1E-separated values
    states.code = 1 + (states.candidates.length == 0)
//    console.log(`cangjie in, buf: ${states.buf}, candidates: ${states.candidates}`)
    return `${states.buf}\u001E${states.candidates}`
}
return Object.freeze({"n":"五倉正體 Qwerty","v":"many","c":"CuriousTo\uA75Bvald, 倉頡之友 。馬來西亞  http://www.chinesecj.com","m":"candidates",
"t":states.keylayouts.map(it => [(it[2]||it[0]),it[1]]),
"l":"zhTW",
// return: [displayed output, composed output]
"accept":(headkey,shiftin,altgrin)=>{
    let layer = 1*shiftin// + 2*altgrin

    let cjkey = states.keylayouts[headkey][layer] || states.keylayouts[headkey][0]
    let cjkeyAsc = cjkey.codePointAt(0)

    if (states.code == 1 && 48 <= cjkeyAsc && cjkeyAsc <= 57) {
        let raw = ''+states.buf
        let selection = states.candidates.split('\u001E')[cjkeyAsc - 49]
        reset()
        return ['', selection || raw]
    }
    else if (1 == states.code && " " == cjkey) {
        let ret = (1 == states.code) ? states.candidates[0] : (''+states.buf)
        reset()
        return ['', ret]
    }
    else if (states.code < 2 && states.buf.length < 5 && 97 <= cjkeyAsc && cjkeyAsc <= 122 || cjkeyAsc == 42) {
        states.buf += cjkey
        return [getCandidatesUsingBuf(), '']
    }
    else {
        let ret = ''+states.buf+cjkey
        reset()
        return ['', ret]
    }
},
"backspace":()=>{
    if (states.buf.length <= 1) {
        reset()
        return ''
    }

    states.buf = states.buf.substring(0, states.buf.length - 1)
    return getCandidatesUsingBuf()
},
"end":()=>{
    let ret = (1 == states.code) ? states.candidates[0] : (''+states.buf)
    reset()
    return ret
},
"reset":()=>{ reset() },
"composing":()=>(states.code!=0)
})