"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genPng = genPng;
const node_path_1 = __importDefault(require("node:path"));
const canvas_1 = require("@napi-rs/canvas");
const rank_1 = require("../renderers/rank");
canvas_1.GlobalFonts.registerFromPath(`${node_path_1.default.join(__dirname, '..', '..', 'public', 'fonts')}/HelveticaBold.ttf`, 'Helvetica Bold');
canvas_1.GlobalFonts.registerFromPath(`${node_path_1.default.join(__dirname, '..', '..', 'public', 'fonts')}/Helvetica.ttf`, 'Helvetica');
async function genPng(data, options) {
    const { assets } = data;
    const defaultWidth = 885;
    const defaultHeight = 303;
    const canvasWidth = options?.customWidth ?? defaultWidth;
    const canvasHeight = options?.customHeight ?? defaultHeight;
    const scaleX = canvasWidth / defaultWidth;
    const scaleY = canvasHeight / defaultHeight;
    const scaledOptions = {
        ...options,
        _scaleX: scaleX,
        _scaleY: scaleY,
        _canvasWidth: canvasWidth,
        _canvasHeight: canvasHeight,
    };
    const canvas = (0, canvas_1.createCanvas)(canvasWidth, canvasHeight);
    const ctx = canvas.getContext('2d');
    const userAvatar = `${assets?.avatarURL ?? assets?.defaultAvatarURL}?size=512`;
    const userBanner = assets?.bannerURL ? `${assets.bannerURL}?size=512` : null;
    const badges = await (0, rank_1.getBadges)(data, scaledOptions);
    if (scaledOptions?.removeBorder) {
        ctx.roundRect(9 * scaleX, 9 * scaleY, 867 * scaleX, 285 * scaleY, [
            26 * Math.min(scaleX, scaleY),
        ]);
    }
    else {
        ctx.roundRect(0, 0, canvasWidth, canvasHeight, [
            34 * Math.min(scaleX, scaleY),
        ]);
    }
    ctx.clip();
    const cardBase = await (0, rank_1.genBase)(scaledOptions, userAvatar, userBanner);
    ctx.drawImage(cardBase, 0, 0);
    const cardFrame = await (0, rank_1.genFrame)(badges, scaledOptions);
    ctx.drawImage(cardFrame, 0, 0);
    const hasFrame = !scaledOptions?.removeAvatarFrame && !!data?.decoration?.avatarFrame;
    const cardTextAndAvatar = await (0, rank_1.genTextAndAvatar)(data, scaledOptions, userAvatar, hasFrame);
    const textAvatarShadow = (0, rank_1.addShadow)(cardTextAndAvatar);
    ctx.drawImage(textAvatarShadow, 0, 0);
    ctx.drawImage(cardTextAndAvatar, 0, 0);
    if (!scaledOptions?.disableThemeColors &&
        data?.decoration?.profileColors &&
        typeof scaledOptions?.borderColor === 'undefined') {
        scaledOptions.borderColor = data?.decoration?.profileColors;
        if (!scaledOptions?.borderAllign) {
            scaledOptions.borderAllign = 'vertical';
        }
    }
    if ((typeof scaledOptions?.borderColor === 'string' &&
        scaledOptions.borderColor) ||
        (Array.isArray(scaledOptions?.borderColor) &&
            scaledOptions.borderColor.length > 0)) {
        const border = (0, rank_1.genBorder)(scaledOptions);
        ctx.drawImage(border, 0, 0);
    }
    if (data.bot) {
        const botVerifBadge = await (0, rank_1.genBotVerifBadge)(data, scaledOptions);
        const shadowVerifBadge = (0, rank_1.addShadow)(botVerifBadge);
        ctx.drawImage(shadowVerifBadge, 0, 0);
        ctx.drawImage(botVerifBadge, 0, 0);
    }
    if (!scaledOptions?.removeBadges) {
        const cardBadges = (0, rank_1.genBadges)(badges, scaledOptions);
        const badgesShadow = (0, rank_1.addShadow)(cardBadges);
        ctx.drawImage(badgesShadow, 0, 0);
        ctx.drawImage(cardBadges, 0, 0);
    }
    if (scaledOptions?.rankData) {
        const xpBar = (0, rank_1.genXpBar)(scaledOptions);
        ctx.drawImage(xpBar, 0, 0);
    }
    if (hasFrame && data?.decoration?.avatarFrame) {
        const avatarFrame = await (0, rank_1.genAvatarFrame)(data, scaledOptions);
        ctx.drawImage(avatarFrame, 0, 0);
    }
    return canvas.toBuffer('image/png');
}
//# sourceMappingURL=rank.js.map