"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createServer = void 0;
const express_1 = __importDefault(require("express"));
const body_parser_1 = __importDefault(require("body-parser"));
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const chalk_1 = __importDefault(require("chalk"));
const watch_1 = __importDefault(require("watch"));
const fast_glob_1 = __importDefault(require("fast-glob"));
function updateSnapShots(source, name, kind) {
    let filePath = path_1.default.join(source, name);
    let current = filePath + '.current.png';
    let diff = filePath + '.diff.png';
    let original = filePath + '.png';
    console.log(chalk_1.default.green(`image ${name}.png use ${kind}`));
    if (kind === 'current') {
        fs_extra_1.default.copyFileSync(current, original);
        fs_extra_1.default.removeSync(current);
    }
    else {
        fs_extra_1.default.removeSync(current);
    }
    fs_extra_1.default.removeSync(diff);
}
function collectFailedImages(source) {
    let imgs = fast_glob_1.default.sync('**/*.current.png', {
        cwd: source,
        deep: 10
    });
    return imgs.map(img => getName(img, '.current.png'));
    function getName(file, prefix) {
        return file.substr(0, file.length - prefix.length);
    }
}
function createServer(source, port) {
    let images = collectFailedImages(source);
    const app = express_1.default();
    const buildDir = path_1.default.join(__dirname, '../client_build');
    app.use(express_1.default.static(source));
    app.use('/css', express_1.default.static(path_1.default.join(buildDir, 'css')));
    app.use('/js', express_1.default.static(path_1.default.join(buildDir, 'js')));
    app.use('/assets', express_1.default.static(path_1.default.join(buildDir, 'assets')));
    app.use('/', express_1.default.static(buildDir));
    app.use('/imageBox', express_1.default.static(source));
    app.use(body_parser_1.default.json());
    watch_1.default.watchTree(source, function (f, curr, prev) {
        if (typeof f == "object" && prev === null && curr === null) {
            // Finished walking the tree
        }
        else {
            images = collectFailedImages(source);
        }
    });
    app.get('/', (req, res) => {
        res.redirect('/snapshot-view');
    });
    app.get('/snapshot-view', (req, res) => {
        const readStream = fs_extra_1.default.createReadStream(path_1.default.join(buildDir, 'index.html'));
        readStream.pipe(res);
    });
    app.get('/api/get_images', (req, res) => {
        return res.json(images);
    });
    app.post('/api/update_image', (req, res) => {
        let filename = req.body.filename;
        let kind = req.body.kind;
        let index = req.body.index;
        try {
            updateSnapShots(source, filename, kind);
            images.splice(index, 1);
        }
        catch (e) {
            res.status(500);
            return res.json({
                errno: 1,
                errmsg: e.message
            });
        }
        return res.json({
            errno: 0
        });
    });
    app.listen(port);
}
exports.createServer = createServer;
