import CSSClient from '../cssClient.js';
import OutputManager from '../outputManager.js';
import TablePrinter from '../tablePrinter.js';
export default class ScriptListDisplayer {
    cssClient;
    constructor() {
        this.cssClient = new CSSClient();
    }
    async displayScripts() {
        const scripts = await this.cssClient.get('/scripts');
        if (!scripts || scripts.length === 0) {
            OutputManager.newline();
            OutputManager.info('No scripts found.');
            OutputManager.newline();
            return;
        }
        const runSpecs = (await this.cssClient.get('/runSpecs')) || [];
        // eslint-disable-next-line unicorn/no-array-reduce
        const scriptRunSpecCounts = runSpecs.reduce((acc, runSpec) => {
            acc[runSpec.scriptId] = (acc[runSpec.scriptId] || 0) + 1;
            return acc;
        }, {});
        const scriptsTableData = scripts.map((script) => ({
            name: script.name,
            runSpecCount: scriptRunSpecCounts[script.id] || 0,
            updatedAt: new Date(script.updatedAt).toLocaleString(),
            updatedAtDate: new Date(script.updatedAt)
        }));
        scriptsTableData.sort((a, b) => b.updatedAtDate.getTime() - a.updatedAtDate.getTime());
        new TablePrinter('Scripts', scriptsTableData, [
            { key: 'name', name: 'Name' },
            { key: 'runSpecCount', name: 'Run specs' },
            { key: 'updatedAt', name: 'Last updated' }
        ]).print();
    }
}
