"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.login = login;
exports.refresh = refresh;
exports.validate = validate;
exports.signout = signout;
exports.ChangeAuthApi = ChangeAuthApi;
const crypto_1 = __importDefault(require("crypto"));
let api_url = 'https://authserver.mojang.com';
async function login(username, password) {
    const UUID = crypto_1.default.randomBytes(16).toString('hex');
    if (!password) {
        return {
            access_token: UUID,
            client_token: UUID,
            uuid: UUID,
            name: username,
            user_properties: '{}',
            meta: { online: false, type: 'Mojang' }
        };
    }
    const response = await fetch(`${api_url}/authenticate`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            agent: { name: 'Minecraft', version: 1 },
            username,
            password,
            clientToken: UUID,
            requestUser: true
        })
    });
    const message = (await response.json());
    if (message.error)
        return message;
    const selectedProfile = message.selectedProfile;
    return {
        access_token: message.accessToken,
        client_token: message.clientToken,
        uuid: selectedProfile.id,
        name: selectedProfile.name,
        user_properties: '{}',
        meta: { online: true, type: 'Mojang' }
    };
}
async function refresh(acc) {
    const response = await fetch(`${api_url}/refresh`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            accessToken: acc.access_token,
            clientToken: acc.client_token,
            requestUser: true
        })
    });
    const message = (await response.json());
    if (message.error)
        return message;
    const selectedProfile = message.selectedProfile;
    return {
        access_token: message.accessToken,
        client_token: message.clientToken,
        uuid: selectedProfile.id,
        name: selectedProfile.name,
        user_properties: '{}',
        meta: { online: true, type: 'Mojang' }
    };
}
async function validate(acc) {
    const response = await fetch(`${api_url}/validate`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ accessToken: acc.access_token, clientToken: acc.client_token })
    });
    return response.status === 204;
}
async function signout(acc) {
    const response = await fetch(`${api_url}/invalidate`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ accessToken: acc.access_token, clientToken: acc.client_token })
    });
    const text = await response.text();
    return text === '';
}
function ChangeAuthApi(url) {
    api_url = url;
}
