"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const express_1 = __importDefault(require("express"));
const parse_1 = require("./parse");
const client_1 = require("../prisma/client");
const prisma = new client_1.PrismaClient();
const app = express_1.default();
const port = process.env.PORT || 4001;
const siteUrl = process.env.SITE_URL || `http://localhost:${port}`;
async function main() {
    const bundleCount = await prisma.bundle.count();
    console.log(`🟢 ${bundleCount} bundles in the database!`);
    app.use(express_1.default.json({ limit: '20mb' }));
    app.use(express_1.default.static(path.resolve(__dirname, '../public')));
    app.get('/bundle.json', (req, res) => {
        const json = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../../assets/stats.json')).toString());
        const filesAndSizes = parse_1.getFilesAndSizes(json.modules);
        const tree = parse_1.convertToTree(filesAndSizes);
        res.json(tree);
    });
    app.get('/bundle/:bundle.json', async (req, res) => {
        const { bundle } = req.params;
        const bundleModel = await prisma.bundle.findFirst({
            where: {
                id: bundle,
            },
        });
        if (bundleModel) {
            res.json(bundleModel.tree);
        }
        else {
            res.status(404).json({ error: '404 not found' });
        }
    });
    app.post('/upload', async (req, res) => {
        try {
            const { tree, meta } = req.body;
            const bundle = await prisma.bundle.create({
                data: { tree, meta },
            });
            const url = `${siteUrl}/b/${bundle.id}`;
            res.json({ success: true, url, message: `📦 Bundle Tracked: ${url}` });
            console.log(url);
        }
        catch (error) {
            res.json({ success: false, message: '🔴 Could not store bundle!' });
        }
    });
    app.listen(port, () => {
        console.log(`🟢 BundleTracker Server listening at ${siteUrl}`);
    });
}
main()
    .catch((e) => {
    throw e;
})
    .finally(async () => {
    await prisma.$disconnect();
});
//# sourceMappingURL=server.js.map