"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const core_1 = require("@oclif/core");
const json_parse_better_errors_1 = __importDefault(require("json-parse-better-errors"));
const base_1 = __importDefault(require("../base"));
const fsPromises = fs_1.default.promises;
class SetDefaultSession extends base_1.default {
    async run() {
        const { flags: runFlags } = await this.parse(SetDefaultSession);
        const { session, currentSession, delete: deleteDefaultSession } = runFlags;
        const output = runFlags.target || runFlags.out || '.';
        const isDir = (await fsPromises.lstat(output)).isDirectory();
        this.target = isDir ? `${output}/config.json` : output;
        const configContents = await this.readJsonFile(this.target);
        if (deleteDefaultSession) {
            configContents.defaultSession = undefined;
            await this.writeJsonFile(this.target, configContents);
        }
        else if (currentSession) {
            this.log(`The current default session is ${JSON.stringify(configContents.defaultSession)}`);
            this.exit();
        }
        else if (!session) {
            this.error('Please provide a --session file', { exit: 120 });
        }
        else if (session) {
            await this.writeJsonFile(this.target, {
                ...configContents,
                defaultSession: await this.readDefaultSessionFile(session),
            });
        }
    }
    async readDefaultSessionFile(defaultSessionFile) {
        let defaultSessionJson;
        try {
            defaultSessionJson = await fsPromises.readFile(defaultSessionFile, {
                encoding: 'utf8',
            });
        }
        catch (error) {
            return this.error('Could not read the provided file', { exit: 150 });
        }
        try {
            const session = (0, json_parse_better_errors_1.default)(defaultSessionJson);
            // return top-level "session" if it exists, such as in files created by
            // "File -> Export session"
            return session.session || session;
        }
        catch (error) {
            return this.error('Could not parse the given default session file', {
                exit: 160,
            });
        }
    }
}
SetDefaultSession.description = 'Set a default session with views and tracks';
SetDefaultSession.examples = [
    '# set default session for the config.json in your current directory',
    '$ jbrowse set-default-session --session /path/to/default/session.json',
    '',
    '# make session.json the defaultSession on the specified target config.json file',
    '$ jbrowse set-default-session --target /path/to/jb2/installation/config.json --session session.json',
    '',
    '# print current default session',
    '$ jbrowse set-default-session --currentSession # Prints out current default session',
];
SetDefaultSession.flags = {
    session: core_1.Flags.string({
        char: 's',
        description: 'set path to a file containing session in json format (required, unless using delete/currentSession flags)',
    }),
    name: core_1.Flags.string({
        char: 'n',
        description: 'Give a name for the default session',
        default: 'New Default Session',
    }),
    currentSession: core_1.Flags.boolean({
        char: 'c',
        description: 'List out the current default session',
    }),
    target: core_1.Flags.string({
        description: 'path to config file in JB2 installation directory to write out to',
    }),
    out: core_1.Flags.string({
        description: 'synonym for target',
    }),
    delete: core_1.Flags.boolean({
        description: 'Delete any existing default session.',
    }),
    help: core_1.Flags.help({
        char: 'h',
    }),
};
exports.default = SetDefaultSession;
