"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.EmailBodyParser = void 0;
const cheerio_1 = __importDefault(require("cheerio"));
const url_1 = require("url");
const axios_1 = __importDefault(require("axios"));
const image_size_1 = __importDefault(require("image-size"));
class EmailBodyParser {
    constructor(input) {
        this.input = input;
    }
    parse(options) {
        return __awaiter(this, void 0, void 0, function* () {
            this.dom = cheerio_1.default.load(`${this.input}`, {});
            if (options.maxImageWidth) {
                yield this.addImageSizes(options.maxImageWidth);
            }
            return this.dom.html();
        });
    }
    addImageSizes(maxWidth) {
        return __awaiter(this, void 0, void 0, function* () {
            const imgs = this.dom('img:not(.do-not-resize)');
            for (const img of imgs) {
                const src = img.attribs.src;
                const alt = img.attribs.alt || 'Image';
                img.attribs.alt = alt;
                try {
                    const url = new url_1.URL(src);
                    const response = yield axios_1.default.get(url.href || '', {
                        responseType: 'arraybuffer',
                    });
                    const { width = maxWidth, height = 1 } = (0, image_size_1.default)(response.data);
                    const specifiedWidth = parseInt(img.attribs.width || '0', 10);
                    const specifiedHeight = parseInt(img.attribs.height || '0', 10);
                    img.attribs.style = '';
                    if (specifiedWidth > maxWidth) {
                        img.attribs.width = maxWidth.toString();
                    }
                    if (!img.attribs.width) {
                        const newWidth = width > maxWidth ? maxWidth : width;
                        img.attribs.width = newWidth.toString();
                        if (specifiedHeight) {
                            const ratio = width / height;
                            img.attribs.width = Math.round(specifiedHeight * ratio).toString();
                        }
                    }
                }
                catch (e) {
                    console.error(e);
                }
            }
        });
    }
}
exports.EmailBodyParser = EmailBodyParser;
