<commit_before>/**
 * Dependencies
 */

var analytics = require('analytics');
var config = require('config');
var debounce = require('debounce');
var debug = require('debug')(config.name() + ':plan');
var geocode = require('geocode');
var defaults = require('model-defaults');
var model = require('model');
var otp = require('otp');
var session = require('session');
var store = require('store');

/**
 * Max routes & patterns to show
 */

var MAX_ROUTES = localStorage.getItem('max_routes') || 3;
var MAX_PATTERNS = localStorage.getItem('max_patterns') || MAX_ROUTES;

/**
 * Expose `Plan`
 */

var Plan = module.exports = model('Plan')
  .use(defaults({
    am_pm: 'am',
    bike: false,
    bus: false,
    car: false,
    days: 'M—F',
    end_time: 9,
    from: '',
    routes: [],
    start_time: 7,
    to: '',
    train: false,
    walk: false,
    welcome_complete: false
  }))
  .attr('am_pm')
  .attr('bike')
  .attr('bus')
  .attr('car')
  .attr('days')
  .attr('end_time')
  .attr('from')
  .attr('from_ll')
  .attr('original_modes')
  .attr('patterns')
  .attr('reverse_commute')
  .attr('routes')
  .attr('start_time')
  .attr('to')
  .attr('to_ll')
  .attr('train')
  .attr('walk')
  .attr('welcome_complete');

/**
 * Filters
 */

var filters = ['am_pm', 'bike', 'bus', 'train', 'car', 'walk', 'days',
  'start_time', 'end_time', 'from_ll', 'to_ll'
];

/**
 * Sync plans with localStorage
 */

Plan.on('change', function(plan, name, val) {
  debug('plan.%s changed to %s', name, val);

  // if the type is a filter, trigger `updateRoutes`
  if (plan.welcome_complete()) {
    if (filters.indexOf(name) !== -1) {
      plan.updateRoutes();
    }
  } else if (plan.original_modes() && plan.validCoordinates()) {
    plan.welcome_complete(true);
  }

  // Store in localStorage
  if (name !== 'routes' && name !== 'patterns') plan.store(name, val);
});

/**
 * Keep start/end times in sync
 */

Plan.on('change start_time', function(plan, val, prev) {
  console.log('start_time', val, 'to', plan.end_time());
  if (val >= plan.end_time()) plan.end_time(val + 1);
});

Plan.on('change end_time', function(plan, val, prev) {
  console.log('end_time', plan.start_time(), 'to', val);
  if (val <= plan.start_time()) plan.start_time(val - 1);
});

/**
 * Load plan middleware
 */

Plan.load = function(ctx, next) {
  var plan = session.plan();
  if (!plan) {
    debug('loading plan at %s', ctx.path);

    // check if we have a stored plan
    var opts = store('plan') || {};
    if (session.isLoggedIn() && session.commuter()) {
      var commuter = session.commuter();
      debug('loading plan for logged in commuter %s', commuter._id());

      // if the stored plan is not the logged in commuters, change
      if (opts._commuter !== commuter._id()) {
        debug('load plan from the commuter instead of localStorage');

        opts = commuter.opts();
        var org = commuter._organization();

        opts.from = commuter.fullAddress() || opts.from;
        opts.from_ll = commuter.coordinate() || opts.from_ll;
<commit_msg>Remove console.log statements<commit_after>/**
 * Dependencies
 */

var analytics = require('analytics');
var config = require('config');
var debounce = require('debounce');
var debug = require('debug')(config.name() + ':plan');
var geocode = require('geocode');
var defaults = require('model-defaults');
var model = require('model');
var otp = require('otp');
var session = require('session');
var store = require('store');

/**
 * Max routes & patterns to show
 */

var MAX_ROUTES = localStorage.getItem('max_routes') || 3;
var MAX_PATTERNS = localStorage.getItem('max_patterns') || MAX_ROUTES;

/**
 * Expose `Plan`
 */

var Plan = module.exports = model('Plan')
  .use(defaults({
    am_pm: 'am',
    bike: false,
    bus: false,
    car: false,
    days: 'M—F',
    end_time: 9,
    from: '',
    routes: [],
    start_time: 7,
    to: '',
    train: false,
    walk: false,
    welcome_complete: false
  }))
  .attr('am_pm')
  .attr('bike')
  .attr('bus')
  .attr('car')
  .attr('days')
  .attr('end_time')
  .attr('from')
  .attr('from_ll')
  .attr('original_modes')
  .attr('patterns')
  .attr('reverse_commute')
  .attr('routes')
  .attr('start_time')
  .attr('to')
  .attr('to_ll')
  .attr('train')
  .attr('walk')
  .attr('welcome_complete');

/**
 * Filters
 */

var filters = ['am_pm', 'bike', 'bus', 'train', 'car', 'walk', 'days',
  'start_time', 'end_time', 'from_ll', 'to_ll'
];

/**
 * Sync plans with localStorage
 */

Plan.on('change', function(plan, name, val) {
  debug('plan.%s changed to %s', name, val);

  // if the type is a filter, trigger `updateRoutes`
  if (plan.welcome_complete()) {
    if (filters.indexOf(name) !== -1) {
      plan.updateRoutes();
    }
  } else if (plan.original_modes() && plan.validCoordinates()) {
    plan.welcome_complete(true);
  }

  // Store in localStorage
  if (name !== 'routes' && name !== 'patterns') plan.store(name, val);
});

/**
 * Keep start/end times in sync
 */

Plan.on('change start_time', function(plan, val, prev) {
  if (val >= plan.end_time()) plan.end_time(val + 1);
});

Plan.on('change end_time', function(plan, val, prev) {
  if (val <= plan.start_time()) plan.start_time(val - 1);
});

/**
 * Load plan middleware
 */

Plan.load = function(ctx, next) {
  var plan = session.plan();
  if (!plan) {
    debug('loading plan at %s', ctx.path);

    // check if we have a stored plan
    var opts = store('plan') || {};
    if (session.isLoggedIn() && session.commuter()) {
      var commuter = session.commuter();
      debug('loading plan for logged in commuter %s', commuter._id());

      // if the stored plan is not the logged in commuters, change
      if (opts._commuter !== commuter._id()) {
        debug('load plan from the commuter instead of localStorage');

        opts = commuter.opts();
        var org = commuter._organization();

        opts.from = commuter.fullAddress() || opts.from;
        opts.from_ll = commuter.coordinate() || opts.from_ll;

{'fraction_non_alphanumeric': 0.19108811748998664, 'fraction_numerical': 0.0020026702269692926, 'mean_word_length': 3.4392592592592592, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3785638', 'n_tokens_mistral': 2087, 'n_tokens_neox': 2062, 'n_words': 508}