import { html } from 'lit-html';
import { db } from './dataStorage.js';
import { go } from './router.js';
import { settings, subscribeSettings } from './settings.js';
import { showLoader, hideOverlay, showModal, showAlertModal } from './overlays.js';
import { languages } from './languages.js';
import { isEmptyObject, generateSlug, loyaltyCardToString, loyaltyCardFromString } from './helpers.js';
import { formatDateTime } from './formatters.js';
import { getHafasClient, client } from './hafasClient.js';
import { trainsearchToHafas, hafasToTrainsearch } from './refresh_token/index.js';
import { default as ds100 } from './ds100.json';
let ds100R = {};
const journeySettings = () => {
return {
stopovers: true,
polylines: false,
tickets: true,
language: settings.language,
};
if (settings.profile !== 'db') {
params.accessibility = settings.accessibility;
params.walkingSpeed = settings.walkingSpeed;
} else {
const card = loyaltyCardFromString(settings.loyaltyCard)
params.loyaltyCard = card;
params.ageGroup = settings.ageGroup;
}
return params;
};
export const getFrom = journeys => journeys[0].legs[0].origin;
export const getTo = journeys => journeys[0].legs[journeys[0].legs.length-1].destination;
export const addJourneys = async data => {
if (!data) return false;
const historyEntry = {
profile: data.profile,
fromPoint: getFrom(data.journeys),
viaPoint: data.params.via,
toPoint: getTo(data.journeys),
slug: data.slug
};
const journeyEntries = data.journeys.map(j => {
return {
...j,
settings: data.settings,
slug: data.slug,
};
});
if (typeof data.params.loyaltyCard === 'object') data.params.loyaltyCard = loyaltyCardToString(data.params.loyaltyCard);
const journeysOverviewEntry = {
...data,
journeys: data.journeys.map(j => j.refreshToken),
};
await db.addJourneys(journeyEntries, journeysOverviewEntry, historyEntry);
};
const processJourneys = data => { for (const journey of data.journeys) processJourney(journey) };
const processJourney = journey => { for (const leg of journey.legs) processLeg(leg) };
export const processLeg = leg => {
if (leg.plannedDeparture) leg.plannedDeparture = new Date(leg.plannedDeparture);
if (leg.plannedArrival) leg.plannedArrival = new Date(leg.plannedArrival);
if (leg.plannedWhen) leg.plannedWhen = new Date(leg.plannedWhen);
if (leg.departure) leg.departure = new Date(leg.departure);
if (leg.arrival) leg.arrival = new Date(leg.arrival);
if (leg.when) leg.when = new Date(leg.when);
for (const stopover of (leg.stopovers || [])) {
if (stopover.plannedDeparture) stopover.plannedDeparture = new Date(stopover.plannedDeparture);
if (stopover.plannedArrival) stopover.plannedArrival = new Date(stopover.plannedArrival);
if (stopover.plannedWhen) stopover.plannedWhen = new Date(stopover.plannedWhen);
if (stopover.departure) stopover.departure = new Date(stopover.departure);
if (stopover.arrival) stopover.arrival = new Date(stopover.arrival);
if (stopover.when) stopover.when = new Date(stopover.when);
}
};
export const newJourneys = async (params) => {
const { from, to, ...moreOpts } = params;
let data;
data = await client.journeys(from, to, { ...journeySettings(), ...moreOpts });
for (const journey of data.journeys) {
journey.refreshToken = hafasToTrainsearch(journey.refreshToken);
}
data.slug = generateSlug();
data.indexOffset = 0;
data.params = params;
data.settings = journeySettings();
data.profile = settings.profile;
await addJourneys(data);
processJourneys(data);
return data;
};
export const getJourneys = async slug => {
let data = await db.getJourneysOverview(slug);
if (!data) return null;
return {
...data,
journeys: await Promise.all(data.journeys.map(x => getJourney(x, data.profile))),
};
};
export const getMoreJourneys = async (slug, mode) => {
const saved = await db.getJourneysOverview(slug);
const params = { ...saved.params, ...journeySettings() };
if (typeof params.loyaltyCard === 'string') params.loyaltyCard = loyaltyCardFromString(params.loyaltyCard);
params[mode+'Than'] = saved[mode+'Ref'];
let { departure, arrival, from, to, ...moreOpt } = params;
const [newData, ...existingJourneys] = await Promise.all(
[ client.journeys(from, to, moreOpt) ]
.concat(saved.journeys.map(x => getJourney(x, saved.profile)))
);
const res = {
...saved,
...newData,
};
for (const journey of newData.journeys) journey.refreshToken = hafasToTrainsearch(journey.refreshToken);
if (mode === 'earlier') {
res.journeys = newData.journeys.concat(existingJourneys);
res.indexOffset += newData.journeys.length;
// keep old laterRef
res.laterRef = saved.laterRef;
} else {
res.journeys = existingJourneys.concat(newData.journeys);
// keep old earlierRef
res.earlierRef = saved.earlierRef;
}
await addJourneys(res);
};
export const refreshJourneys = async (slug) => {
const saved = await db.getJourneysOverview(slug);
await Promise.all(saved.journeys.map(x => refreshJourney(x, saved.profile || "db")));
};
export const getJourney = async (refreshToken, profile) => {
let journeyObject = await db.getJourney(refreshToken);
if (!journeyObject || JSON.stringify(journeyObject.settings) != JSON.stringify(journeySettings()))
journeyObject = await refreshJourney(refreshToken, profile);
processJourney(journeyObject);
return journeyObject;
};
export const refreshJourney = async (refreshToken, profile) => {
const client = await getHafasClient(profile || settings.profile || "db");
const [saved, data] = await Promise.all([
db.getJourney(refreshToken),
client.refreshJourney(trainsearchToHafas(refreshToken), journeySettings())
]);
const {journey} = data;
journey.settings = journeySettings();
journey.refreshToken = hafasToTrainsearch(journey.refreshToken);
if (saved) journey.slug = saved.slug;
db.updateJourney(journey);
return journey;
};
export const ds100Name = (id) => {
if (!settings.showDS100) return null;
if (!ds100[Number(id)]) return null;
return ds100[Number(id)];
};
export const ds100Reverse = (name) => {
if (!settings.showDS100) return null;
if (isEmptyObject(ds100R)) {
for (let [id, names] of Object.entries(ds100)) {
for (let name of names.split(", ")) ds100R[name] = id;
}
}
if (!ds100R[name]) return null;
return ds100R[name];
};