"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genAchievementPng = genAchievementPng;
const node_path_1 = __importDefault(require("node:path"));
const canvas_1 = require("@napi-rs/canvas");
const achievement_1 = require("../renderers/achievement");
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 genAchievementPng(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,
        hideDate: true,
        removeBadges: true,
        rankData: {
            currentXp: options.unlockedCount ?? 0,
            requiredXp: options.totalCount ?? 0,
            level: 0,
            hideRank: true,
            hideLevel: true,
            showPercentage: false,
            barColor: options.accentColor,
        },
    };
    const canvas = (0, canvas_1.createCanvas)(canvasWidth, canvasHeight);
    const ctx = canvas.getContext('2d');
    const userAvatar = `${assets?.avatarURL ?? assets?.defaultAvatarURL}?size=512`;
    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, null);
    ctx.drawImage(cardBase, 0, 0);
    const cardFrame = await (0, rank_1.genFrame)([], scaledOptions);
    ctx.drawImage(cardFrame, 0, 0);
    const hasFrame = !scaledOptions?.removeAvatarFrame && !!data?.decoration?.avatarFrame;
    const cardTextAndAvatar = await (0, achievement_1.genAchievementTextAndAvatar)(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 (options.unlockedCount !== undefined && options.totalCount !== undefined) {
    }
    if (options.unlockedCount !== undefined) {
        const xpBar = (0, achievement_1.genAchievementProgressBar)(scaledOptions);
        ctx.drawImage(xpBar, 0, 0);
    }
    if (hasFrame && data?.decoration?.avatarFrame) {
        const frameOptions = {
            ...scaledOptions,
            presenceStatus: (scaledOptions.achievementEmoji ? 'online' : undefined),
        };
        const avatarFrame = await (0, rank_1.genAvatarFrame)(data, frameOptions);
        ctx.drawImage(avatarFrame, 0, 0);
    }
    return canvas.toBuffer('image/png');
}
//# sourceMappingURL=achievement.js.map