import CSSClient from '../../lib/cssClient.js';
export default class ScriptDestroyer {
    cssClient;
    scriptName;
    constructor(scriptName) {
        this.scriptName = scriptName;
        this.cssClient = new CSSClient();
    }
    async destroy() {
        const scripts = await this.cssClient.get('/scripts', { name: this.scriptName });
        if (!scripts || scripts.length === 0 || scripts[0]?.name !== this.scriptName) {
            throw new Error(`Script with name "${this.scriptName}" not found.`);
        }
        const scriptId = scripts[0].id;
        // Delete all packages associated with the script
        const packages = await this.cssClient.get('/packages', { scriptId });
        for (const pkg of packages) {
            // eslint-disable-next-line no-await-in-loop
            await this.cssClient.delete(`/packages/${pkg.id}`);
        }
        // Delete all runSpecs associated with the script
        const runSpecs = await this.cssClient.get('/runSpecs', { scriptId });
        for (const runSpec of runSpecs) {
            // eslint-disable-next-line no-await-in-loop
            await this.cssClient.delete(`/runSpecs/${runSpec.id}`);
        }
        // Delete the script
        await this.cssClient.delete(`/scripts/${scriptId}`);
    }
}
