import { InlineKeyboard } from 'grammy' ; import Engine from '@/helpers/engine/Engine' ; import EngineRecognizer from '@/helpers/engine/EngineRecognizer' ; import Language from '@/helpers/engine/Language' ; import engines from '@/engines' ; const pageSize = 10 ; function buttonFromItem ( item , isCommand , engine ) { return { text : item . name , callback_data : `li~${ isCommand ? 1 : 0 }~${ engine }~${ item . code }` , } ; } function pageFromList ( list , page , isCommand , engine ) { const items = list . slice ( page * pageSize , page * pageSize + pageSize ) ; return items . reduce ( ( p , c , i ) => { p . add ( buttonFromItem ( c , isCommand , engine ) ) ; if ( i % 2 !== 0 ) { p . row ( ) ; } return p ; } , new InlineKeyboard ( ) ) ; } export default function languageKeyboard ( engine , isCommand , page = 0 ) { const engineObject = engines [ engine ] ; const list = engineObject . languages ; list . sort ( ( a , b ) => ( a . name < b . name ? - 1 : 1 ) ) ; const keyboard = pageFromList ( list , page , isCommand , engine ) ; if ( list . length > pageSize ) { if ( page > 0 ) { keyboard . row ( ) ; keyboard . add ( { text : '⬅️' , callback_data : `li~${ isCommand ? 1 : 0 }~${ engine }~<~${ page }` , } ) ; } if ( page < Object . keys ( list ) . length / 10 - 1 ) { if ( page <= 0 ) { keyboard . row ( ) ; } keyboard . add ( { text : '➡️' , callback_data : `li~${ isCommand ? 1 : 0 }~${ engine }~>~${ page }` , } ) ; } } return keyboard ; }