"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.genWelcomeBase = genWelcomeBase;
exports.genWelcomeAvatar = genWelcomeAvatar;
exports.genWelcomeAvatarFrame = genWelcomeAvatarFrame;
exports.genWelcomeText = genWelcomeText;
const canvas_1 = require("@napi-rs/canvas");
const canvas_2 = require("../utils/canvas");
const validation_1 = require("../utils/validation");
async function genWelcomeBase(options, backgroundImage) {
    const { width, height } = (0, canvas_2.getWelcomeCanvasDimensions)(options);
    const canvas = (0, canvas_1.createCanvas)(width, height);
    const ctx = canvas.getContext('2d');
    let bgImage = null;
    if (backgroundImage) {
        try {
            bgImage = await (0, canvas_1.loadImage)((0, validation_1.parseImg)(backgroundImage));
        }
        catch {
            bgImage = null;
        }
    }
    if (bgImage) {
        const blurAmount = options?.backgroundBlur ?? 3;
        ctx.filter = `blur(${blurAmount}px)`;
        if (options?.backgroundBrightness) {
            ctx.filter += ` brightness(${options.backgroundBrightness + 100}%)`;
        }
        const imgRatio = bgImage.width / bgImage.height;
        const canvasRatio = width / height;
        let drawWidth;
        let drawHeight;
        let offsetX;
        let offsetY;
        if (imgRatio > canvasRatio) {
            drawHeight = height + blurAmount * 2;
            drawWidth = drawHeight * imgRatio;
            offsetX = (width - drawWidth) / 2;
            offsetY = -blurAmount;
        }
        else {
            drawWidth = width + blurAmount * 2;
            drawHeight = drawWidth / imgRatio;
            offsetX = -blurAmount;
            offsetY = (height - drawHeight) / 2;
        }
        ctx.save();
        ctx.beginPath();
        ctx.rect(0, 0, width, height);
        ctx.clip();
        ctx.drawImage(bgImage, offsetX, offsetY, drawWidth, drawHeight);
        ctx.restore();
        ctx.filter = 'none';
    }
    else {
        ctx.fillStyle = '#18191c';
        ctx.fillRect(0, 0, width, height);
    }
    const overlayColor = options?.overlayColor ?? 'rgba(0, 0, 0, 0.4)';
    ctx.fillStyle = overlayColor;
    ctx.fillRect(0, 0, width, height);
    return canvas;
}
async function genWelcomeAvatar(avatarUrl, options, hasFrame = false) {
    const { width, height } = (0, canvas_2.getWelcomeCanvasDimensions)(options);
    const canvas = (0, canvas_1.createCanvas)(width, height);
    const ctx = canvas.getContext('2d');
    const avatarSize = options?.avatarSize ?? 220;
    const avatarRadius = avatarSize / 2;
    const avatarX = width / 2;
    const avatarY = options?.avatarY ?? 80;
    const avatar = await (0, canvas_1.loadImage)(avatarUrl);
    const borderConfig = options?.avatarBorder ?? { width: 8, color: '#FFFFFF' };
    if (!hasFrame && borderConfig.width > 0) {
        ctx.strokeStyle = (0, validation_1.parseHex)(borderConfig.color);
        ctx.lineWidth = borderConfig.width;
        ctx.beginPath();
        ctx.arc(avatarX, avatarY + avatarRadius, avatarRadius + borderConfig.width / 2, 0, Math.PI * 2);
        ctx.stroke();
    }
    ctx.save();
    ctx.beginPath();
    ctx.arc(avatarX, avatarY + avatarRadius, avatarRadius, 0, Math.PI * 2);
    ctx.closePath();
    ctx.clip();
    ctx.drawImage(avatar, avatarX - avatarRadius, avatarY, avatarSize, avatarSize);
    ctx.restore();
    return canvas;
}
async function genWelcomeAvatarFrame(frameUrl, options) {
    const { width, height } = (0, canvas_2.getWelcomeCanvasDimensions)(options);
    const canvas = (0, canvas_1.createCanvas)(width, height);
    const ctx = canvas.getContext('2d');
    const avatarSize = options?.avatarSize ?? 220;
    const frameSize = avatarSize * (269 / 225);
    const avatarRadius = avatarSize / 2;
    const centerX = width / 2;
    const centerY = (options?.avatarY ?? 80) + avatarRadius;
    const frameX = centerX - frameSize / 2;
    const frameY = centerY - frameSize / 2;
    if (frameUrl) {
        const avatarFrame = await (0, canvas_1.loadImage)(frameUrl);
        ctx.drawImage(avatarFrame, frameX, frameY, frameSize, frameSize);
    }
    return canvas;
}
function genWelcomeText(username, options) {
    const { width, height } = (0, canvas_2.getWelcomeCanvasDimensions)(options);
    const canvas = (0, canvas_1.createCanvas)(width, height);
    const ctx = canvas.getContext('2d');
    const customFont = options?.customFont ?? 'Helvetica';
    const welcomeText = options?.welcomeText ??
        (options?.type === 'goodbye' ? 'GOODBYE' : 'WELCOME');
    const customFontSize = options?.customFontSize ?? 80;
    const customUsernameSize = options?.customUsernameSize ?? 40;
    const welcomeColor = options?.welcomeColor ?? '#FFFFFF';
    const usernameColor = options?.usernameColor ?? '#FFFFFF';
    const textShadow = options?.textShadow ?? true;
    const textStroke = options?.textStroke;
    const avatarSize = options?.avatarSize ?? 220;
    const avatarY = options?.avatarY ?? 80;
    const textOffsetY = options?.textOffsetY ?? 0;
    const welcomeY = avatarY + avatarSize + customFontSize * 0.8 + 20 + textOffsetY;
    const usernameY = welcomeY + customUsernameSize * 0.8 + 15;
    if (textShadow) {
        ctx.shadowColor = 'rgba(0, 0, 0, 0.8)';
        ctx.shadowBlur = 10;
        ctx.shadowOffsetX = 3;
        ctx.shadowOffsetY = 3;
    }
    if (textStroke?.width && textStroke?.color) {
        ctx.strokeStyle = (0, validation_1.parseHex)(textStroke.color);
        ctx.lineWidth = textStroke.width;
        ctx.lineJoin = 'round';
    }
    const weight = options?.fontWeight ?? 'bold';
    ctx.font = `${weight} ${customFontSize}px ${customFont}`.trim();
    ctx.fillStyle = (0, validation_1.parseHex)(welcomeColor);
    ctx.textAlign = 'center';
    if (textStroke?.width && textStroke?.color) {
        ctx.strokeText(welcomeText, width / 2, welcomeY);
    }
    ctx.fillText(welcomeText, width / 2, welcomeY);
    ctx.font = `${weight} ${customUsernameSize}px ${customFont}`.trim();
    ctx.fillStyle = (0, validation_1.parseHex)(usernameColor);
    if (textStroke?.width && textStroke?.color) {
        ctx.strokeText(username, width / 2, usernameY);
    }
    ctx.fillText(username, width / 2, usernameY);
    ctx.shadowColor = 'transparent';
    ctx.shadowBlur = 0;
    ctx.shadowOffsetX = 0;
    ctx.shadowOffsetY = 0;
    return canvas;
}
//# sourceMappingURL=welcome.js.map