"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var cookieParser = require("cookie-parser");
var express = require("express");
var createError = require("http-errors");
var low = require("lowdb");
var FileSync = require("lowdb/adapters/FileSync");
var logger = require("morgan");
var path = require("path");
var read = require("read-file");
var uuidv4 = require("uuid/v4");
var adminRouter = require("./routes/admin");
var exportRouter = require("./routes/export");
var indexRouter = require("./routes/index");
var recommendsRouter = require("./routes/recommends");
var updateRouter = require("./routes/update");
var app = express();
var adapter = new FileSync("db.json");
var db = low(adapter);
app.db = db;
db.defaults({ users: [] })
    .write();
app.cocktails = [];
read("data/output.csv", "utf8", function (err, buffer) {
    // console.log(buffer);
    var cocktailList = [];
    var result = buffer.split(/\n|\r|\n\r|\r\n/);
    result = result.map(function (line) {
        line = line.split("\t");
        if (line.length !== 8) {
            return null;
        }
        line[0] = parseFloat(line[0]);
        line[1] = parseFloat(line[1]);
        line[4] = line[4].substr(0, line[4].length - 2) + "," + line[4].substr(-2) + " \u20AC";
        line[5] = line[5].substr(0, line[5].length - 2) + "," + line[5].substr(-2) + " \u20AC";
        cocktailList.push(line[3]);
        return line;
    });
    result = result.filter(function (f) { return !!f; });
    app.cocktails = result;
    app.cocktailList = cocktailList;
});
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(function (req, res, next) {
    var user = db.get("users").find({ id: req.cookies.uid }).value();
    if (!req.cookies || !req.cookies.uid || !user) {
        var id = uuidv4();
        db.get("users")
            .push({ id: id, age: -1, sex: "na", cocktails: [], lastused: Date.now() })
            .write();
        res.cookie("uid", id, { maxAge: 21474836470000 /*, httpOnly: true*/ });
        if (!req.cookies) {
            req.cookies = {};
        }
        req.cookies.uid = id;
    }
    else {
        db.get("users")
            .find({ id: req.cookies.uid })
            .assign({ lastused: Date.now() })
            .write();
    }
    next();
});
app.use(express.static(path.join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/admin", adminRouter);
app.use("/export", exportRouter);
app.use("/update", updateRouter);
app.use("/recommends", recommendsRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get("env") === "development" ? err : {};
    // render the error page
    res.status(err.status || 500);
    res.render("error");
});
module.exports = app;
//# sourceMappingURL=app.js.map