import * as plugins from './smartarchive.plugins.js';
import * as paths from './smartarchive.paths.js';
export class SmartArchive {
    constructor() { }
    /**
     * extracts an archive from a given url
     */
    async extractArchiveFromUrlToFs(urlArg, targetDir) {
        const parsedPath = plugins.path.parse(urlArg);
        const uniqueFileName = plugins.smartunique.uni() + parsedPath.ext;
        plugins.smartfile.fs.ensureDir(paths.nogitDir); // TODO: totally remove caching needs
        const downloadPath = plugins.path.join(paths.nogitDir, uniqueFileName);
        const downloadedArchive = (await plugins.smartrequest.getBinary(urlArg)).body;
        await plugins.smartfile.memory.toFs(downloadedArchive, downloadPath);
        await this.extractArchiveFromFilePathToFs(downloadPath, targetDir);
        await plugins.smartfile.fs.remove(downloadPath);
    }
    /**
     * extracts an archive from a given filePath on disk
     * @param filePathArg
     * @param targetDirArg
     */
    async extractArchiveFromFilePathToFs(filePathArg, targetDirArg) {
        console.log(`extracting ${filePathArg}`);
        const done = plugins.smartpromise.defer();
        filePathArg = plugins.smartpath.transform.makeAbsolute(filePathArg);
        targetDirArg = plugins.smartpath.transform.makeAbsolute(targetDirArg);
        const readableStream = plugins.smartfile.fsStream.createReadStream(filePathArg);
        const extractPipeStop = plugins.tarStream.extract();
        extractPipeStop.on('entry', async (header, stream, next) => {
            const targetFilePath = plugins.path.join(targetDirArg, header.name);
            const parsedPath = plugins.path.parse(targetFilePath);
            await plugins.smartfile.fs.ensureDir(parsedPath.dir);
            const writeStream = plugins.smartfile.fsStream.createWriteStream(targetFilePath);
            stream.pipe(writeStream);
            stream.on('end', () => {
                console.log(`extracted ${header.name}`);
                next();
            });
            stream.resume();
        });
        extractPipeStop.on('finish', () => {
            console.log(`Sucessfully extracted ${filePathArg}!`);
            done.resolve();
        });
        // lets run the stream
        readableStream.pipe(plugins.gunzipMaybe()).pipe(extractPipeStop);
        await done.promise;
    }
    /**
     * extracts to Observable
     * where the Observable is emitting smartfiles
     */
    async extractArchiveFromBufferToObservable(bufferArg) {
        const { intake, replaySubject } = this.extractArchiveWithIntakeAndReplaySubject();
        intake.pushData(bufferArg);
        intake.signalEnd();
        return replaySubject;
    }
    extractArchiveWithIntakeAndReplaySubject() {
        const intake = new plugins.smartstream.StreamIntake();
        const replaySubject = new plugins.smartrx.rxjs.ReplaySubject();
        const readableStream = intake.getReadableStream();
        const extractPipeStop = plugins.tarStream.extract();
        extractPipeStop.on('entry', (header, stream, next) => {
            let fileBuffer;
            stream.on('data', (chunkArg) => {
                if (!fileBuffer) {
                    fileBuffer = chunkArg;
                }
                else {
                    fileBuffer = Buffer.concat([fileBuffer, chunkArg]);
                }
            });
            stream.on('end', () => {
                replaySubject.next(new plugins.smartfile.Smartfile({
                    base: null,
                    contentBuffer: fileBuffer,
                    path: `${header.name}`,
                }));
                next();
            });
            stream.resume();
        });
        extractPipeStop.on('finish', () => {
            replaySubject.complete();
        });
        // lets run the stream
        readableStream.pipe(plugins.gunzipMaybe()).pipe(extractPipeStop);
        return {
            intake,
            replaySubject,
        };
    }
    /**
     * extracts to Observable
     */
    async extractArchiveFromUrlToObservable(urlArg) {
        const response = await plugins.smartrequest.getBinary(urlArg);
        const replaySubject = this.extractArchiveFromBufferToObservable(response.body);
        return replaySubject;
    }
    // TODO
    async extractArchiveFromUrlToStream() { }
    // TODO
    async extractArchiveFromFilePathToStream() { }
    // TODO
    async extractArchiveFromStreamToStream() { }
    // TODO
    async packFromStreamToStream() { }
    // TODO
    async packFromDirPathToStream() { }
    // TODO
    async packFromDirPathToFs() { }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhcmNoaXZlLmNsYXNzZXMuc21hcnRhcmNoaXZlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRhcmNoaXZlLmNsYXNzZXMuc21hcnRhcmNoaXZlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0sMkJBQTJCLENBQUM7QUFDckQsT0FBTy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