import fs from 'node:fs';
import path from 'node:path';
import * as tar from 'tar';
import StatusManager from '../../lib/statusManager.js';
import RESTClient from '../restClient.js';
export default class RemoteTarballExtractor {
    extractDest;
    srcUrl;
    constructor(srcUrl, extractDest) {
        this.srcUrl = srcUrl;
        this.extractDest = extractDest;
    }
    static async tarX(extractDest) {
        return tar.x({ cwd: extractDest, strict: true });
    }
    async extract() {
        const extractDest = path.resolve(this.extractDest);
        if (fs.existsSync(extractDest)) {
            throw new Error(`Destination path ${extractDest} already exists`);
        }
        StatusManager.startTask("Retrieving content");
        const restClient = new RESTClient(this.srcUrl);
        const response = await restClient.call({
            method: 'get',
            responseType: 'stream'
        });
        StatusManager.startTask(`Extracting to ${extractDest}`);
        fs.mkdirSync(extractDest, { recursive: true });
        response.pipe(await RemoteTarballExtractor.tarX(extractDest));
    }
}
