import { error } from '../model/error.js';

export const getInfo = async ({ console, profile, fetch }) => {
    const serverUrl = profile?.server_url;
    if (!serverUrl) {
        const err = new Error('Server URL of profile is not set');
        throw err;
    }
    const infoUrl = new URL(serverUrl);
    infoUrl.pathname = '/info';
    infoUrl.searchParams.set('apptid', profile.apptid);
    console.debug(`Getting tenant info from ${String(infoUrl)}`);
    const res = await fetch(infoUrl);
    if (res.status !== 200) {
        throw error(
            `Failed to get tenant information: ${res.status} ${res.statusText} ` +
            await res.text(),
            res.status
        );
    }
    return await res.json();
};
