text: const dayjs = require('dayjs');
  var Parser = require('../parser').Parser;
  var ParsedResult = require('../../result').ParsedResult;
  
  var DAYS_OFFSET = { 'sunday': 0, 'sun': 0, 'monday': 1, 'mon': 1,'tuesday': 2, 'tues':2, 'tue':2, 'wednesday': 3, 'wed': 3,
      'thursday': 4, 'thurs':4, 'thur': 4, 'thu': 4,'friday': 5, 'fri': 5,'saturday': 6, 'sat': 6};
  
  var PATTERN = new RegExp('(\\W|^)' +
      '(?:(?:\\,|\\(|\\（)\\s*)?' +
      '(?:on\\s*?)?' +
      '(?:(this|last|past|next)\\s*)?' +
      '(' + Object.keys(DAYS_OFFSET).join('|') + ')' +
      '(?:\\s*(?:\\,|\\)|\\）))?' +
      '(?:\\s*(this|last|past|next)\\s*week)?' +
      '(?=\\W|$)', 'i');
  
  var PREFIX_GROUP = 2;
  var WEEKDAY_GROUP = 3;
  var POSTFIX_GROUP = 4;
  
  
  exports.updateParsedComponent = function updateParsedComponent(result, ref, offset, modifier) {
  
      var startMoment = dayjs(ref);
      var startMomentFixed = false;
      var refOffset = startMoment.day();
  
      if(modifier == 'last' || modifier == 'past') {
          startMoment =startMoment.day(offset - 7);
          startMomentFixed = true;
      } else if(modifier == 'next') {
          startMoment = startMoment.day(offset + 7);
          startMomentFixed = true;
      } else if(modifier == 'this') {
          startMoment = startMoment.day(offset);
      } else {
          if (Math.abs(offset - 7 - refOffset) < Math.abs(offset - refOffset)) {
              startMoment = startMoment.day(offset - 7);
          } else if (Math.abs(offset + 7 - refOffset) < Math.abs(offset - refOffset)) {
              startMoment = startMoment.day(offset + 7);
          } else {
              startMoment = startMoment.day(offset);
          }
      }
  
      result.start.assign('weekday', offset);
      if (startMomentFixed) {
          result.start.assign('day', startMoment.date());
          result.start.assign('month', startMoment.month() + 1);
          result.start.assign('year', startMoment.year());
      } else {
          result.start.imply('day', startMoment.date());
          result.start.imply('month', startMoment.month() + 1);
          result.start.imply('year', startMoment.year());
      }
  
      return result;
  };
  
  
  exports.Parser = function ENWeekdayParser() {
      Parser.apply(this, arguments);
  
      this.pattern = function() { return PATTERN; };
  
      this.extract = function(text, ref, match, opt){
          var index = match.index + match[1].length;
          var text = match[0].substr(match[1].length, match[0].length - match[1].length);
          var result = new ParsedResult({
              index: index,
              text: text,
              ref: ref
          });
  
          var dayOfWeek = match[WEEKDAY_GROUP].toLowerCase();
          var offset = DAYS_OFFSET[dayOfWeek];
          if(offset === undefined) {
              return null;
          }
  
          var prefix = match[PREFIX_GROUP];
          var postfix = match[POSTFIX_GROUP];
          var norm = prefix || postfix;
          norm = norm || '';
          norm = norm.toLowerCase();
  
          exports.updateParsedComponent(result, ref, offset, norm);
          result.tags['ENWeekdayParser'] = true;
  
          return result;
      }
  };
  