import AppConfig from '../../lib/appConfig.js';
import constants from '../../lib/constants.js';
import RemoteTarballExtractor from './remoteTarballExtractor.js';
export default class ScriptCreator {
    destination;
    templateName;
    constructor(templateName, destination) {
        this.templateName = templateName;
        this.destination = destination;
    }
    async create() {
        const appConfig = await AppConfig.getInstance();
        const templateFilename = `${this.templateName}.tar.gz`;
        const templateUrl = new URL(templateFilename, appConfig.getConfig("listUrl", "templates") || constants.SCRIPT_TEMPLATES_BASE_URL);
        const destPath = this.destination;
        const extractor = new RemoteTarballExtractor(templateUrl.toString(), destPath);
        try {
            await extractor.extract();
        }
        catch (error) {
            const newError = error instanceof Error && error.message.endsWith("404") ?
                new Error(`Template ${this.templateName} not found. Please try \`${constants.APP_NAME} templates list.\``) :
                error;
            throw newError;
        }
    }
}
