"use strict";
/**
 * @author NovaStepStudios
 * @alias StepnickaSantiago
 * @license Apache-2.0
 * @link https://www.apache.org/licenses/LICENSE-2.0
 */
Object.defineProperty(exports, "__esModule", { value: true });
const node_buffer_1 = require("node:buffer");
class NovaAZauth {
    url;
    skinAPI;
    constructor(url) {
        this.url = new URL('/api/auth', url).toString();
        this.skinAPI = new URL('/api/skin-api/skins', url).toString();
    }
    async login(username, password, A2F = null) {
        const response = await fetch(`${this.url}/authenticate`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ email: username, password, code: A2F })
        });
        const data = (await response.json());
        if (data.status === 'pending' && data.reason === '2fa')
            return { A2F: true };
        if (data.status === 'error')
            return { error: true, reason: data.reason ?? undefined, message: data.message ?? undefined };
        const id = data.id ?? undefined;
        return {
            access_token: data.access_token ?? undefined,
            client_token: data.uuid ?? undefined,
            uuid: data.uuid ?? undefined,
            name: data.username ?? undefined,
            user_properties: '{}',
            user_info: {
                id,
                banned: data.banned ?? undefined,
                money: data.money ?? undefined,
                role: data.role ?? undefined,
                verified: data.email_verified ?? undefined
            },
            meta: { online: false, type: 'AZauth' },
            profile: { skins: id ? [await this.skin(id)] : [] }
        };
    }
    async verify(user) {
        const response = await fetch(`${this.url}/verify`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ access_token: user.access_token })
        });
        const data = (await response.json());
        if (data.status === 'error')
            return { error: true, reason: data.reason ?? undefined, message: data.message ?? undefined };
        const id = data.id ?? undefined;
        return {
            access_token: data.access_token ?? undefined,
            client_token: data.uuid ?? undefined,
            uuid: data.uuid ?? undefined,
            name: data.username ?? undefined,
            user_properties: '{}',
            user_info: {
                id,
                banned: data.banned ?? undefined,
                money: data.money ?? undefined,
                role: data.role ?? undefined,
                verified: data.email_verified ?? undefined
            },
            meta: { online: false, type: 'AZauth' },
            profile: { skins: id ? [await this.skin(id)] : [] }
        };
    }
    async signout(user) {
        const response = await fetch(`${this.url}/logout`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ access_token: user.access_token })
        });
        const data = (await response.json());
        return !data.error;
    }
    async skin(uuid) {
        const response = await fetch(`${this.skinAPI}/${uuid}`, { method: 'GET' });
        if (response.status === 404)
            return { url: `${this.skinAPI}/${uuid}` };
        const buffer = node_buffer_1.Buffer.from(await response.arrayBuffer());
        return { url: `${this.skinAPI}/${uuid}`, base64: `data:image/png;base64,${buffer.toString('base64')}` };
    }
}
exports.default = NovaAZauth;
