<commit_before>'use strict'

const so     = require('so')
const time   = require('parse-messy-time')

const _      = require('./index')
const render = require('./render')



const unknownLocation = `\
I don't know about this location, please double-check for typos.
If you're sure it's my fault, please let my creator @derhuerst know.`
const textOrLocation = `\
Please enter a location like "U mehringdamm", "Kaiserdamm 26" or send your location.`

const currentLocation = (msg) => ({
	type: 'address', name: 'your location',
	latitude: msg.location.latitude,
	longitude: msg.location.longitude
})

const routes = so(function* (ctx, msg) {
	const state = yield ctx.get('state')

	if (state === 'time') {
		if (!msg.text) return ctx.message(`Please enter text.`)
		const from = yield ctx.get('from')
		const to = yield ctx.get('to')
		const when = time(msg.text)
		ctx.typing()
		const routes = yield _.routes(from, to, when)
		yield ctx.keyboard(render.routes(routes), ctx.keys)
		yield ctx.done()
	}

	else if (state === 'to') {
		ctx.typing()
		let location
		if (msg.text) {
			location = yield _.location(msg.text)
			if (!location) return ctx.message(unknownLocation)
		} else if (msg.location) location = currentLocation(msg)
		else return ctx.message(textOrLocation)
		yield ctx.set('to', location)
		yield ctx.set('state', 'time')
		yield ctx.message(`\
*When?*
e.g. "now", "in 10 minutes" or "tomorrow 17:20"`)
	}

	else if (state === 'from') {
		ctx.typing()
		let location
		if (msg.text) {
			location = yield _.location(msg.text)
			if (!location) return ctx.message(unknownLocation)
		} else if (msg.location) location = currentLocation(msg)
		else return ctx.message(textOrLocation)
		yield ctx.set('from', location)
		yield ctx.set('state', 'to')
		yield ctx.requestLocation(`\
*Where do you want to go?*`, 'send location')
	}

	else {
		yield ctx.set('state', 'from')
		yield ctx.requestLocation(`\
*Where do you start?*
Enter a location like "U mehringdamm", "Kaiserdamm 26" or send your location.`, 'send location')
	}

})
<commit_msg>refactor /route<commit_after>'use strict'

const so     = require('so')
const time   = require('parse-messy-time')

const _      = require('./index')
const render = require('./render')



const when = so(function* (ctx, msg) {
	if (!msg.text) return ctx.message(`Please enter text.`)
	const from = yield ctx.get('origin')
	const to = yield ctx.get('destination')
	const when = time(msg.text)
	ctx.typing()
	const routes = yield _.routes(from, to, when)
	yield ctx.keyboard(render.routes(routes), ctx.keys)
})



const unknownLocation = `\
I don't know about this location, please double-check for typos.
If you're sure it's my fault, please let my creator @derhuerst know.`
const textOrLocation = `\
Please enter a location like "U mehringdamm", "Kaiserdamm 26" or send your location.`
const currentLocation = (msg) => ({
	type: 'address', name: 'your location',
	latitude: msg.location.latitude,
	longitude: msg.location.longitude
})

const where = so(function* (key, ctx, msg) {
	ctx.typing()
	let location
	if (msg.text) {
		location = yield _.location(msg.text)
		if (!location) return ctx.message(unknownLocation)
	} else if (msg.location) location = currentLocation(msg)
	else return ctx.message(textOrLocation)
	yield ctx.set(key, location)
})



const promptWhen = `\
*When?*
e.g. "now", "in 10 minutes" or "tomorrow 17:20"`
const promptDestination = `\
*Where do you want to go?*`
const promptOrigin = `\
*Where do you start?*
Enter a location like "U mehringdamm", "Kaiserdamm 26" or send your location.`

const routes = so(function* (ctx, msg) {
	const state = yield ctx.get('state')

	if (state === 'when') {
		yield when(ctx, msg)
		yield ctx.done()
	}
	else if (state === 'destination') {
		yield where('destination', ctx, msg)
		yield ctx.set('state', 'when')
		yield ctx.message(promptWhen)
	}
	else if (state === 'origin') {
		yield where('origin', ctx, msg)
		yield ctx.set('state', 'destination')
		yield ctx.message(promptDestination, 'send location')
	}
	else {
		yield ctx.set('state', 'origin')
		yield ctx.requestLocation(promptOrigin, 'send location')
	}
})

{'fraction_non_alphanumeric': 0.14986705342035292, 'fraction_numerical': 0.0048344210780759, 'mean_word_length': 4.205031446540881, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1012148', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1370, 'n_words': 426}