"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var express = require("express");
var bodyParser = require("body-parser");
var cors = require("cors");
var morgan = require("morgan");
var fs = require("fs-extra");
var config_service_1 = require("./config-service");
var artifactory_service_1 = require("./artifactory-service");
var cache_service_1 = require("./cache-service");
var portNumber = 30001;
var app = express();
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(cors());
function handleError(error, targetResponse, msg) {
    targetResponse.status(500).send("Error: " + msg + " Please verify your server settings.\n\n" + error);
}
app.get('/packages', function (req, res) {
    artifactory_service_1.default
        .fetchPackages()
        .then(function (response) {
        res.send(response.data);
    })
        .catch(function (error) {
        handleError(error, res, "Could not get packagages from " + artifactory_service_1.default.baseURL + ".");
    });
});
app.get('/package/:scope?/:packageName/dist-tags', function (req, res) {
    artifactory_service_1.default
        .getDistTags(req.params)
        .then(function (response) {
        res.send(response.data);
    })
        .catch(function (error) {
        handleError(error, res, "Could not get dist-tags for package \"" + req.params.scope + "/" + req.params.packageName + "\n      \" from " + artifactory_service_1.default.baseURL + ".");
    });
});
app.get('/packageDetail/:scope/:packageName/:version?', function (req, res) {
    artifactory_service_1.default
        .getPackageDetail(req.params)
        .then(function (response) {
        res.send(response.data);
    })
        .catch(function (error) {
        handleError(error, res, "Could not get package details for package \"" + req.params.scope + "/" + req.params.packageName + "\" from " + artifactory_service_1.default.baseURL + ".");
    });
});
app.get('/packageDetail/:scope/:packageName/:version/files/:path', function (req, res) {
    try {
        artifactory_service_1.default
            .getFileContent({
            scope: req.params.scope,
            packageName: req.params.packageName,
            version: req.params.version,
        }, req.params.path, req.query.format)
            .then(function (response) {
            res.send(response);
        });
    }
    catch (error) {
        handleError(error, res, "Could not get file content from " + req.params.path + ".");
    }
});
app.get('/config', function (req, res) {
    config_service_1.default
        .getConfig()
        .then(function (config) {
        res.send({
            artifactory: {
                host: config.artifactory.host,
                repoKey: config.artifactory.repoKey,
                https: config.artifactory.https,
            },
            howto: {
                default: config.howto && config.howto.default === false ? config.howto.default : true,
                additional: {
                    markup: config.howto && config.howto.additional ? config.howto.additional.markup : '',
                    heading: config.howto && config.howto.additional ? config.howto.additional.heading : '',
                },
            },
            companyScope: config.companyScope,
        });
    })
        .catch(function (error) {
        handleError(error, res, "Could not get npmFrog config from server.");
    });
});
app.get('/meta', function (req, res) {
    try {
        var packageJson = fs.readJSONSync('../../package.json');
        res.send(packageJson);
    }
    catch (error) {
        handleError(error, res, "Could not get npmFrog package.json.");
    }
});
app.listen(portNumber);
cache_service_1.default.fillCacheFromDisc();
