
mapping = {
    'a': 'ੳ',
    'A': 'ਅ',
    's': 'ਸ',
    'S': 'ਸ਼',
    'd': 'ਦ',
    'D': 'ਧ',
    'f': 'ਡ',
    'F': 'ਢ',
    'g': 'ਗ',
    'G': 'ਘ',
    'h': 'ਹ',
    'H': '੍ਹ',
    'j': 'ਜ',
    'J': 'ਝ',
    'k': 'ਕ',
    'K': 'ਖ',
    'l': 'ਲ',
    'L': 'ਲ਼',
    'q': 'ਤ',
    'Q': 'ਥ',
    'w': 'ਾ',
    'W': 'ਾਂ',
    'e': 'ੲ',
    'E': 'ਓ',
    'r': 'ਰ',
    'R': '੍ਰ',
    '®': '੍ਰ',
    't': 'ਟ',
    'T': 'ਠ',
    'y': 'ੇ',
    'Y': 'ੈ',
    'u': 'ੁ',
    'ü': 'ੁ',
    'U': 'ੂ',
    '¨': 'ੂ',
    'i': 'ਿ',
    'I': 'ੀ',
    'o': 'ੋ',
    'O': 'ੌ',
    'p': 'ਪ',
    'P': 'ਫ',
    'z': 'ਜ਼',
    'Z': 'ਗ਼',
    'x': 'ਣ',
    'X': 'ਯ',
    'c': 'ਚ',
    'C': 'ਛ',
    'v': 'ਵ',
    'V': 'ੜ',
    'b': 'ਬ',
    'B': 'ਭ',
    'n': 'ਨ',
    'ƒ': 'ਨੂੰ',
    'N': 'ਂ',
    'ˆ': 'ਂ',
    'm': 'ਮ',
    'M': 'ੰ',
    'µ': 'ੰ',
    '`': 'ੱ',
    '~': 'ੱ',
    '¤': 'ੱ',
    'Í': '੍ਵ',
    'ç': '੍ਚ',
    '†': '੍ਟ',
    'œ': '੍ਤ',
    '˜': '੍ਨ',
    '´': 'ੵ',
    'Ï': 'ੵ',
    'æ': '਼',
    'Î': '੍ਯ',
    'ì': 'ਯ',
    'í': '੍ਯ',
    '1': '੧',
    '2': '੨',
    '3': '੩',
    '4': '੪',
    '5': '੫',
    '6': '੬',
    '^': 'ਖ਼',
    '7': '੭',
    '&': 'ਫ਼',
    '8': '੮',
    '9': '੯',
    '0': '੦',
    '\\': 'ਞ',
    '|': 'ਙ',
    '[': '।',
    ']': '॥',
    '<': 'ੴ',
    '¡': 'ੴ',
    'Å': 'ੴ',
    'Ú': 'ਃ',
    'Ç': '☬',
    '@': 'ੑ',
    '‚': '❁',
    '•': '੶',
    ' ': ' ',
}

reverseMapping = {
    'ਇ': 'ie',
    'ਉ': 'au',
    'ਊ': 'aU',
    'ਆ': 'Aw',
    'ਆਂ': 'AW',
    'ਐ': 'AY',
    'ਔ': 'AO',
    'ਈ': 'eI',
    'ਏ': 'ey',
    'ੋੁ': 'uo',
    '੍ਵ': 'Í',
    '੍ਚ': 'ç',
    '੍ਟ': '†',
    '੍ਤ': 'œ',
    '੍ਨ': '˜',
    'ੵ': '´',
    '਼': 'æ',
    '੍ਯ': 'Î',
    'ਨੂੰ': 'ƒ',
    '੍ਹ': 'H',
    '੍ਰ': 'R',
    '': '₁₅',
    '੦': '0',
    '੧': '1',
    '੨': '2',
    '੩': '3',
    '੪': '4',
    '੫': '5',
    '੬': '6',
    '੭': '7',
    '੮': '8',
    '੯': '9',
    'ੳ': 'a',
    'ਅ': 'A',
    'ਸ': 's',
    'ਸ਼': 'S',
    'ਦ': 'd',
    'ਧ': 'D',
    'ਡ': 'f',
    'ਢ': 'F',
    'ਗ': 'g',
    'ਘ': 'G',
    'ਹ': 'h',
    'ਜ': 'j',
    'ਝ': 'J',
    'ਕ': 'k',
    'ਖ': 'K',
    'ਲ': 'l',
    'ਲ਼': 'L',
    'ਤ': 'q',
    'ਥ': 'Q',
    'ਾ': 'w',
    'ਾਂ': 'W',
    'ੲ': 'e',
    'ਓ': 'E',
    'ਰ': 'r',
    'ਟ': 't',
    'ਠ': 'T',
    'ੇ': 'y',
    'ੈ': 'Y',
    'ੁ': 'u',
    'ੂ': 'U',
    'ਿ': 'i',
    'ੀ': 'I',
    'ੋ': 'o',
    'ੌ': 'O',
    'ਪ': 'p',
    'ਫ': 'P',
    'ਜ਼': 'z',
    'ਗ਼': 'Z',
    'ਣ': 'x',
    'ਯ': 'X',
    'ਚ': 'c',
    'ਛ': 'C',
    'ਵ': 'v',
    'ੜ': 'V',
    'ਬ': 'b',
    'ਭ': 'B',
    'ਨ': 'n',
    'ਂ': 'N',
    'ਮ': 'm',
    'ੰ': 'M',
    'ੱ': '`',
    'ਖ਼': '^',
    'ਫ਼': '&',
    'ਞ': '\\',
    'ਙ': '|',
    '।': '[',
    '॥': ']',
    'ੴ': '<>',
    'ਃ': 'Ú',
    '☬': 'Ç',
    'ੑ': '@',
    '❁': '‚',
    '੶': '•',
    '': '₁',
    '': '₂',
    '': '₃',
    '': '₄',
    '': '₅',
    '': '₆',
    '': '₈',
    ' ': ' ',
    '': '₁',
    '': '₂',
    '': '₃',
    '': '₄',
    '': '₆',
}


asciiCorrections = [
    '@W',
    '@w',
    '@o',
    '@O',
    '@y',
    '@Y',
    '@ü',
    '@`',
    'ÍY',
    'Ry',
    'RY',
    'RM',
    'RN',
    'YN',
    'yN',
    'YM',
    'yM',
    'uN',
    'UN',
    'üN',
    'uM',
    'UM',
    'üM',
    'R`',
    'u`',
    'U`',
    'ü`',
    'Iˆ',
    'IN',
]


halfChars = [
    'H',
    'R',
    '®',
    'Í',
    'ç',
    '†',
    'œ',
    '˜',
    '´',
    'Î',
    'Ï',
    'í',
    'æ',
]

aboveChars = [
    'ੇ',
    'ੈ',
    'ੋ',
    'ੌ',
]


def ascii(text='', simplify=False):
    convertedText = []
    chars = list(text)

    j = 0
    while j < len(chars):
        currentChar = chars[j]
        nextChar = chars[j + 1] if j + 1 < len(chars) else None
        nextNextChar = chars[j + 2] if j + 2 < len(chars) else None

        if currentChar == 'ਿ':
            lastElement = convertedText.pop()
            convertedText.append('i')
            convertedText.append(lastElement)
        elif currentChar == '੍':
            if nextNextChar == 'ਿ':
                lastElement = convertedText.pop()
                convertedText.append('i')
                convertedText.append(lastElement)
                j += 1
            convertedText.append(reverseMapping.get(currentChar + nextChar))
            if nextNextChar == 'ੁ':
                convertedText.append('ü')
                j += 1
            elif nextNextChar == 'ੂ':
                convertedText.append('¨')
                j += 1
            j += 1
        elif currentChar == 'ੑ':
            convertedText.append(reverseMapping.get(currentChar, currentChar))
            if nextChar == 'ੁ':
                convertedText.append('ü')
                j += 1
            elif nextChar == 'ੂ':
                convertedText.append('¨')
                j += 1
        elif currentChar == 'ਨ' and nextChar == 'ੂ' and nextNextChar == 'ੰ':
            convertedText.append('ƒ')
            j += 2
        elif currentChar == 'ੋ' and nextChar == 'ੁ':
            convertedText.append(reverseMapping.get(currentChar + nextChar))
            j += 1
        elif currentChar == 'ੱ' and nextNextChar in reverseMapping.keys():
            convertedText.append('¤')
        elif currentChar == 'ਾ' and nextChar == 'ਂ':
            convertedText.append('W')
            j += 1
        elif currentChar == 'ਆ' and nextChar == 'ਂ':
            convertedText.append('AW')
            j += 1
        elif currentChar == 'ਈ' and nextChar == '':
            convertedText.append('eˆØI')
            j += 1
        elif currentChar == 'ਈ' and nextChar == '':
            convertedText.append('eµØI')
            j += 1
        elif (currentChar == 'ਂ' and nextChar == 'ੀ') or (currentChar == 'ੀ' and nextChar == ''):
            convertedText.append('ˆØI')
            j += 1
        elif (currentChar == 'ੰ' and nextChar == 'ੀ') or (currentChar == 'ੀ' and nextChar == ''):
            convertedText.append('µØI')
            j += 1
        elif simplify and nextChar == '਼':
            if currentChar == 'ਸ':
                convertedText.append('S')
                j += 1
            elif currentChar == 'ਜ':
                convertedText.append('z')
                j += 1
            elif currentChar == 'ਖ':
                convertedText.append('^')
                j += 1
            elif currentChar == 'ਗ':
                convertedText.append('Z')
                j += 1
            elif currentChar == 'ਫ':
                convertedText.append('&')
                j += 1
            elif currentChar == 'ਲ':
                convertedText.append('L')
                j += 1
            else:
                convertedText.append(reverseMapping.get(currentChar, currentChar))
        else:
            convertedText.append(reverseMapping.get(currentChar, currentChar))
        
        j+=1

    return ''.join(convertedText)

def unicode(text='', reverse=False, simplify=False):
    

    if reverse:
        return ascii(text, simplify)

    convertedText = ''

    str = text.replace('>', '').replace('Ø', '').replace('Æ', '')

    for e in asciiCorrections:
        str = str.replace(e[::-1], e)

    chars = list(str)

    j = 0
    while j < len(chars):
        currentChar = chars[j]
        nextChar = chars[j + 1] if j + 1 < len(chars) else None
        nextNextChar = chars[j + 2] if j + 2 < len(chars) else None

        if currentChar == 'i':
            if nextChar is not None:
                if nextChar == 'e':
                    convertedText += 'ਇ'
                elif nextNextChar in halfChars:
                    convertedText += mapping[nextChar]
                    convertedText += mapping[nextNextChar]
                    convertedText += 'ਿ'
                    j += 1
                else:
                    convertedText += mapping[nextChar]
                    convertedText += 'ਿ'
                j += 1
            else:
                convertedText += mapping[currentChar]
        elif currentChar == 'a':
            if nextChar == 'u':
                convertedText += 'ਉ'
                j += 1
            elif nextChar == 'U':
                convertedText += 'ਊ'
                j += 1
            else:
                convertedText += mapping[currentChar]
        elif currentChar == 'A':
            if nextChar == 'w':
                convertedText += 'ਆ'
                j += 1
            elif nextChar == 'W':
                convertedText += 'ਆਂ'
                j += 1
            elif nextChar == 'Y':
                convertedText += 'ਐ'
                j += 1
            elif nextChar == 'O':
                convertedText += 'ਔ'
                j += 1
            else:
                convertedText += mapping[currentChar]
        elif currentChar == 'e':
            if nextChar == 'I':
                convertedText += 'ਈ'
                j += 1
            elif nextChar == 'y':
                convertedText += 'ਏ'
                j += 1
            else:
                convertedText += mapping[currentChar]
        elif currentChar == '1' and nextChar == 'E' and nextNextChar == 'å':
            convertedText += 'ੴ'
            j += 2
        elif currentChar == 'u' and nextChar == 'o':
            convertedText += 'ੋੁ'
            j += 1
        elif simplify and nextChar == 'æ':
            if currentChar == 's':
                convertedText += 'ਸ਼'
                j += 1
            elif currentChar == 'j':
                convertedText += 'ਜ਼'
                j += 1
            elif currentChar == 'K':
                convertedText += 'ਖ਼'
                j += 1
            elif currentChar == 'g':
                convertedText += 'ਗ਼'
                j += 1
            elif currentChar == 'P':
                convertedText += 'ਫ਼'
                j += 1
            elif currentChar == 'l':
                convertedText += 'ਲ਼'
                j += 1
            else:
                convertedText += mapping[currentChar]
        else:
            convertedText += mapping.get(currentChar, currentChar)

        j += 1

    return convertedText

