"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listAccounts = listAccounts;
exports.useAccount = useAccount;
const config_1 = require("../config");
const messages_1 = require("../messages");
function listAccounts() {
    const accounts = (0, config_1.listAccountSessions)();
    if (accounts.length === 0) {
        const cfg = (0, config_1.loadConfig)();
        if (cfg.token && cfg.env) {
            console.log('Your current Playdrop session is using the legacy single-account format.');
            console.log('Next: run "playdrop auth whoami" to migrate it into the multi-account store.');
            return;
        }
        console.log('No Playdrop accounts are stored.');
        console.log('Next: run "playdrop auth login" to add an account.');
        return;
    }
    const current = (0, config_1.getCurrentAccountSession)();
    for (const account of accounts) {
        const marker = current && current.username === account.username && current.env === account.env ? '*' : ' ';
        console.log(`${marker} ${account.username} (${account.env})`);
    }
    console.log('Next: run "playdrop auth use <username>" to switch the default account.');
}
function useAccount(username, env) {
    const normalizedUsername = username.trim();
    if (!normalizedUsername) {
        (0, messages_1.printErrorWithHelp)('Provide the username of the account you want to use.', ['Run "playdrop auth accounts" to list stored accounts.'], { command: 'auth use' });
        process.exitCode = 1;
        return;
    }
    const existing = (0, config_1.findAccountSession)(normalizedUsername, env);
    if (!existing) {
        (0, messages_1.printErrorWithHelp)(env
            ? `No stored Playdrop session was found for ${normalizedUsername} on ${env}.`
            : `No stored Playdrop session was found for ${normalizedUsername}.`, [
            'Run "playdrop auth accounts" to list stored accounts.',
            `Run "playdrop auth login${env ? ` --env ${env}` : ''}" to add the missing session.`,
        ], { command: 'auth use' });
        process.exitCode = 1;
        return;
    }
    (0, config_1.setCurrentAccount)({ username: normalizedUsername, env: env?.trim() || undefined });
    console.log(`Current Playdrop account is now ${existing.username} (${existing.env}).`);
    console.log('Next: run "playdrop auth whoami" to confirm your session.');
}
