"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedirectGeneratedResponse = exports.XmlGeneratedResponse = exports.HtmlGeneratedResponse = exports.TextGeneratedResponse = exports.BodyGeneratedResponse = exports.JsonGeneratedResponse = exports.BufferGeneratedResponse = exports.ZipGeneratedResponse = exports.Base64GeneratedResponse = exports.FileGeneratedResponse = exports.normalizeFileName = exports.GeneratedResponse = void 0;
const AdmZipConst = require('adm-zip');
class GeneratedResponse {
}
exports.GeneratedResponse = GeneratedResponse;
function normalizeFileName(name) {
    const authorizedChars = `A-Z|a-z|0-9|:$\\.\\-_`;
    const regexpName = new RegExp('^[' + authorizedChars + ']+$', 'g');
    if (!name.match(regexpName)) {
        name = name.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
        name = name
            .replace(new RegExp('[^' + authorizedChars + ']', 'g'), '_')
            .replace(/__+/g, '_');
    }
    return name;
}
exports.normalizeFileName = normalizeFileName;
class FileGeneratedResponse extends GeneratedResponse {
    constructor(filePath, fileName) {
        super();
        this.filePath = filePath;
        this.fileName = fileName;
    }
    getResponse(response) {
        response.download(this.filePath, this.fileName);
        return response;
    }
}
exports.FileGeneratedResponse = FileGeneratedResponse;
class Base64GeneratedResponse extends GeneratedResponse {
    constructor(name, base64, type) {
        super();
        this.name = name;
        this.base64 = base64;
        this.type = type;
    }
    getResponse(response) {
        response.header('Content-Disposition', 'attachment; filename="' + this.name + '"');
        if (this.type) {
            response.type(this.type);
        }
        const buffer = Buffer.from(this.base64, 'base64');
        response.header('Content-Length', JSON.stringify(buffer.length));
        response.send(buffer);
        return response;
    }
}
exports.Base64GeneratedResponse = Base64GeneratedResponse;
class ZipGeneratedResponse extends GeneratedResponse {
    constructor(name) {
        super();
        this.name = name;
        this.addedBufferSize = 0;
        this.addedBase64Size = 0;
        this.zip = new AdmZipConst();
    }
    addBuffer(name, buffer) {
        this.addedBufferSize += buffer.length;
        this.zip.addFile(name, buffer);
    }
    addBase64(name, base64) {
        this.addedBase64Size += base64.length;
        this.addBuffer(name, Buffer.from(base64, 'base64'));
    }
    getResponse(response) {
        return new BufferGeneratedResponse(this.name + '.zip', this.zip.toBuffer(), 'application/zip').getResponse(response);
    }
}
exports.ZipGeneratedResponse = ZipGeneratedResponse;
class BufferGeneratedResponse extends GeneratedResponse {
    constructor(name, buffer, type) {
        super();
        this.name = name;
        this.buffer = buffer;
        this.type = type;
    }
    getResponse(response) {
        response.header('Content-Disposition', 'attachment; filename="' + normalizeFileName(this.name) + '"');
        if (this.type) {
            response.type(this.type);
        }
        response.header('Content-Length', JSON.stringify(this.buffer.length));
        response.send(this.buffer);
        return response;
    }
}
exports.BufferGeneratedResponse = BufferGeneratedResponse;
class JsonGeneratedResponse extends GeneratedResponse {
    constructor(json) {
        super();
        this.json = json;
    }
    getResponse(response) {
        response.json(this.json);
        return response;
    }
}
exports.JsonGeneratedResponse = JsonGeneratedResponse;
class BodyGeneratedResponse extends GeneratedResponse {
    constructor(response, contentType, noCache = true) {
        super();
        this.response = response;
        this.contentType = contentType;
        this.noCache = noCache;
    }
    getResponse(response) {
        if (this.contentType && '' !== this.contentType) {
            response.set('Content-Type', this.contentType);
        }
        else {
            response.set('Content-Type', 'text/html');
        }
        if (this.noCache) {
            response.set('Cache-Control', 'private,no-cache,no-store,max-age=0,must-revalidate,pre-check=0,post-check=0');
        }
        response.send(this.response);
        return response;
    }
}
exports.BodyGeneratedResponse = BodyGeneratedResponse;
class TextGeneratedResponse extends BodyGeneratedResponse {
    constructor(response, noCache = true) {
        super(response, 'text/plain', noCache);
        this.response = response;
        this.noCache = noCache;
    }
}
exports.TextGeneratedResponse = TextGeneratedResponse;
class HtmlGeneratedResponse extends BodyGeneratedResponse {
    constructor(response, noCache = true) {
        super(response, 'text/html', noCache);
        this.response = response;
        this.noCache = noCache;
    }
}
exports.HtmlGeneratedResponse = HtmlGeneratedResponse;
class XmlGeneratedResponse extends BodyGeneratedResponse {
    constructor(response, noCache = true) {
        super(response, 'application/xml', noCache);
        this.response = response;
        this.noCache = noCache;
    }
}
exports.XmlGeneratedResponse = XmlGeneratedResponse;
class RedirectGeneratedResponse extends GeneratedResponse {
    constructor(uri) {
        super();
        this.uri = uri;
    }
    getResponse(response) {
        response.redirect(this.uri);
        return response;
    }
}
exports.RedirectGeneratedResponse = RedirectGeneratedResponse;
//# sourceMappingURL=generated-response.js.map