"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailController = void 0;
const api_1 = require("@hed-hog/api");
const api_locale_1 = require("@hed-hog/api-locale");
const api_pagination_1 = require("@hed-hog/api-pagination");
const common_1 = require("@nestjs/common");
const create_dto_1 = require("./dto/create.dto");
const import_dto_1 = require("./dto/import.dto");
const test_mail_dto_1 = require("./dto/test-mail.dto");
const update_dto_1 = require("./dto/update.dto");
const mail_service_1 = require("./mail.service");
let MailController = class MailController {
    constructor(mailService) {
        this.mailService = mailService;
    }
    async list(locale, paginationParams) {
        return this.mailService.list(locale, paginationParams);
    }
    async get(id, queryLocale, decoratorLocale) {
        const locale = queryLocale || decoratorLocale;
        return this.mailService.get(locale, id);
    }
    async getHtml(id, res, queryLocale, decoratorLocale) {
        const locale = queryLocale || decoratorLocale;
        const mail = await this.mailService.get(locale, id);
        const html = `<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>${mail.subject}</title>
    <style>
      body {
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        line-height: 1.6;
        color: #333;
        max-width: 600px;
        margin: 0 auto;
        padding: 20px;
      }
      a {
        color: #0066cc;
        text-decoration: none;
      }
      a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    ${mail.body}
  </body>
</html>`;
        res.setHeader('Content-Disposition', `attachment; filename="${mail.slug}-${locale}.html"`);
        res.send(html);
    }
    async create(data) {
        return this.mailService.create(data);
    }
    async reloadConfig() {
        return this.mailService.reloadConfig();
    }
    async update(id, data) {
        return this.mailService.update({
            id,
            data,
        });
    }
    async delete(data) {
        return this.mailService.delete(data);
    }
    async sendTestMail(locale, data) {
        return this.mailService.sendTestMail(locale, data);
    }
    async import(data) {
        return this.mailService.importTemplates(data);
    }
};
exports.MailController = MailController;
__decorate([
    (0, common_1.Get)(),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, api_pagination_1.Pagination)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "list", null);
__decorate([
    (0, common_1.Get)(':id'),
    __param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
    __param(1, (0, common_1.Query)('locale')),
    __param(2, (0, api_locale_1.Locale)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, String, String]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "get", null);
__decorate([
    (0, common_1.Get)(':id/html'),
    (0, common_1.Header)('Content-Type', 'text/html'),
    __param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
    __param(1, (0, common_1.Res)()),
    __param(2, (0, common_1.Query)('locale')),
    __param(3, (0, api_locale_1.Locale)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Object, String, String]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "getHtml", null);
__decorate([
    (0, common_1.Post)(),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [create_dto_1.CreateDTO]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "create", null);
__decorate([
    (0, common_1.Post)('reload'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], MailController.prototype, "reloadConfig", null);
__decorate([
    (0, common_1.Patch)(':id'),
    __param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, update_dto_1.UpdateDTO]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "update", null);
__decorate([
    (0, common_1.Delete)(),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [api_1.DeleteDTO]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "delete", null);
__decorate([
    (0, common_1.Post)('test'),
    __param(0, (0, api_locale_1.Locale)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, test_mail_dto_1.TestMailDTO]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "sendTestMail", null);
__decorate([
    (0, common_1.Post)('import'),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [import_dto_1.ImportDTO]),
    __metadata("design:returntype", Promise)
], MailController.prototype, "import", null);
exports.MailController = MailController = __decorate([
    (0, api_1.Role)(),
    (0, common_1.Controller)('mail'),
    __param(0, (0, common_1.Inject)((0, common_1.forwardRef)(() => mail_service_1.MailService))),
    __metadata("design:paramtypes", [mail_service_1.MailService])
], MailController);
//# sourceMappingURL=mail.controller.js.map