"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mailable = void 0;
const server_1 = __importDefault(require("react-dom/server"));
const juice_1 = __importDefault(require("juice"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const email_body_parser_1 = require("./email-body-parser");
class Mailable {
    categories() {
        return [];
    }
    customArgs() {
        return {};
    }
    attachments() {
        return __awaiter(this, void 0, void 0, function* () {
            return [];
        });
    }
    css() {
        return '';
    }
    defaultCss() {
        return fs_1.default.readFileSync(path_1.default.join(__dirname, './styles/main.css')).toString();
    }
    render() {
        return __awaiter(this, void 0, void 0, function* () {
            const email = server_1.default.renderToString(yield this.view());
            const rawHtml = fs_1.default
                .readFileSync(path_1.default.join(__dirname, './email.html'))
                .toString()
                .replace('<div id="email"></div>', email)
                .replace('<title></title>', `<title>${this.subject}</title>`);
            const parser = new email_body_parser_1.EmailBodyParser(rawHtml);
            const html = yield parser.parse({
                maxImageWidth: this.maxImageWidth,
            });
            return (0, juice_1.default)(html, {
                applyAttributesTableElements: false,
                removeStyleTags: false,
                preserveImportant: true,
                extraCss: [this.defaultCss(), yield this.css()].join('\n'),
            })
                .split('text-decoration: none !important;')
                .join('text-decoration: none !important;text-decoration: none;text-underline: none;');
        });
    }
}
exports.Mailable = Mailable;
