"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 uuidv4 = require("uuid/v4");
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var app = express();
var adapter = new FileSync("db.json");
var db = low(adapter);
db.defaults({ users: [] })
    .write();
// 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: [] })
            .write();
        res.cookie("uid", id, { maxAge: 21474836470000, httpOnly: true });
    }
    next();
});
app.use(express.static(path.join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/users", usersRouter);
// 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