/* eslint-disable no-underscore-dangle */
/* eslint-disable no-console */

class TelegrafLogger {
  constructor(options) {
    this.options = Object.assign({
      log: console.log,
      format: '%updateType => @%username %firstName %lastName (%fromId): <%updateSubType> %content',
      contentLength: 100,
    }, options);
  }

  middleware() {
    return (ctx, next) => {
      let content;
      let updateTypeId;

      switch (ctx.updateType) {
        case 'message':
          updateTypeId = ctx.message.message_id;

          if (ctx.message.text) {
            content = ctx.message.text;
          } else if (ctx.message.sticker) {
            content = ctx.message.sticker.emoji;
          } else {
            content = '';
          }

          break;

        case 'edited_message':
          updateTypeId = ctx.editedMessage.message_id;
          content = ctx.editedMessage.text;
          break;

        case 'channel_post':
          updateTypeId = ctx.channelPost.message_id;

          if (ctx.channelPost.text) {
            content = ctx.channelPost.text;
          } else if (ctx.channelPost.sticker) {
            content = ctx.channelPost.sticker.emoji;
          } else {
            content = '';
          }

          break;

        case 'edited_channel_post':
          updateTypeId = ctx.editedChannelPost.message_id;
          content = ctx.editedChannelPost.text;
          break;

        case 'callback_query':
          updateTypeId = ctx.callbackQuery.id;
          content = ctx.callbackQuery.data;
          break;

        case 'inline_query':
          updateTypeId = ctx.inlineQuery.id;
          content = ctx.inlineQuery.query;
          break;

        case 'chosen_inline_result':
          updateTypeId = ctx.chosenInlineResult.result_id;
          content = ctx.chosenInlineResult.query;
          break;

        case 'shipping_query':
          updateTypeId = ctx.shippingQuery.id;
          content = ctx.shippingQuery.invoice_payload;
          break;

        case 'pre_checkout_query':
          updateTypeId = ctx.preCheckoutQuery.id;
          content = ctx.preCheckoutQuery.invoice_payload;
          break;

        default:
          content = '';
          updateTypeId = 0;
      }

      const text = this.options.format
        .replace(/%botUsername\b/igm, ctx.me || null)
        .replace(/%username\b/igm, ctx.from.username || null)
        .replace(/%firstName\b/igm, ctx.from.first_name)
        .replace(/%lastName\b/igm, ctx.from.last_name || '')
        .replace(/%fromId\b/igm, ctx.from.id)
        .replace(/%chatId\b/igm, ctx.chat && ctx.chat.id)
        .replace(/%chatType\b/igm, ctx.chat && ctx.chat.type)
        .replace(/%chatTitle\b/igm, (ctx.chat && ctx.chat.title) || null)
        .replace(/%chatUsername\b/igm, (ctx.chat && ctx.chat.username) || null)
        .replace(/%updateId\b/igm, ctx.update.update_id)
        .replace(/%updateType\b/igm, ctx.updateType)
        .replace(/%updateTypeId\b/igm, updateTypeId)
        .replace(/%updateSubType\b/igm, ctx.updateSubType || ctx.updateSubTypes[0] || ctx.updateType)
        .replace(/%sceneId\b/igm, (ctx.session && ctx.session._flow && ctx.session._flow.id) || null)
        .replace(/ +|\n/g, ' ');

      if (content.length > this.options.contentLength) {
        content = `${content.slice(0, this.options.contentLength)}...`;
      }

      this.options.log(text.replace(/%content\b/igm, content).replace(/\n/g, ' '));
      return next();
    };
  }
}

module.exports = TelegrafLogger;

@@ -2238,24 +2238,82 @@
 0;%0A      %7D%0A%0A
+      const %7B from = %7B%7D, chat = %7B%7D, session = %7B%7D %7D = ctx;%0A
       const 
@@ -2424,28 +2424,24 @@
 name%5Cb/igm, 
-ctx.
 from.usernam
@@ -2483,28 +2483,24 @@
 Name%5Cb/igm, 
-ctx.
 from.first_n
@@ -2535,28 +2535,24 @@
 Name%5Cb/igm, 
-ctx.
 from.last_na
@@ -2594,20 +2594,16 @@
 %5Cb/igm, 
-ctx.
 from.id)
@@ -2641,30 +2641,22 @@
 m, c
-tx.chat && ctx.chat.id
+hat.id %7C%7C null
 )%0A  
@@ -2693,32 +2693,16 @@
 m, c
-tx.chat && ctx.c
 hat.type
 )%0A  
@@ -2697,16 +2697,24 @@
 hat.type
+ %7C%7C null
 )%0A      
@@ -2743,33 +2743,16 @@
 %5Cb/igm, 
-(ctx.chat && ctx.
 chat.tit
@@ -2753,17 +2753,16 @@
 at.title
-)
  %7C%7C null
@@ -2806,25 +2806,8 @@
 gm, 
-(ctx.chat && ctx.
 chat
@@ -2815,17 +2815,16 @@
 username
-)
  %7C%7C null
@@ -3129,27 +3129,8 @@
 m, (
-ctx.session && ctx.
 sess
@@ -3142,20 +3142,16 @@
 flow && 
-ctx.
 session.
@@ -3193,11 +3193,8 @@
 (/ +
-%7C%5Cn
 /g, 
@@ -3398,17 +3398,16 @@
  content
-)
 .replace
@@ -3419,16 +3419,17 @@
 g, ' '))
+)
 ;%0A      

Fix mapping