"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
 * penguins-eggs
 * class: compressor.ts
 * test available compressor
 * author: Piero Proietti
 * email: piero.proietti@gmail.com
 * license: MIT
 */
const shelljs_1 = tslib_1.__importDefault(require("shelljs"));
class Compressors {
    constructor() {
        this.isEnabled = {
            error: false,
            gzip: true,
            lzma: false,
            lzo: false,
            lz4: false,
            xz: false,
            zstd: false,
        };
        this.source = '/tmp/eggs-mksquash-test';
        this.dest = '/tmp/eggs-mksquash-dest';
    }
    /**
     * populate
     */
    async populate() {
        await this.prepareCheck();
        this.isEnabled.error = await this.check('error');
        this.isEnabled.lzma = await this.check('lzma');
        this.isEnabled.lzo = await this.check('lzo');
        this.isEnabled.lz4 = await this.check('lz4');
        this.isEnabled.xz = await this.check('xz');
        this.isEnabled.zstd = await this.check('zstd');
        await this.removeCheck();
    }
    /**
     * fast
     * @returns
     */
    fast() {
        let comp = 'gzip';
        if (this.isEnabled.zstd) {
            comp = 'zstd -b 1M -Xcompression-level 1';
        }
        else if (this.isEnabled.lz4) {
            comp = 'lz4';
        }
        return comp;
    }
    /**
     * standard
     * @returns
     */
    standard() {
        let comp = 'xz -b 1M';
        return comp;
    }
    /**
     * max
     * @returns
     */
    max() {
        let filter = 'x86';
        if (process.arch === 'arm64') {
            filter = 'arm';
        }
        let options = '-b 1M -no-duplicates -no-recovery -always-use-fragments';
        if (process.arch === 'ia32') {
            // options = '-b 1M'
        }
        let comp = `xz -Xbcj ${filter} ${options}`;
        return comp;
    }
    /**
     * prepareCheck
     */
    async prepareCheck() {
        shelljs_1.default.exec('rm -rf ' + this.source, { silent: true });
        shelljs_1.default.exec('mkdir ' + this.source, { silent: true });
    }
    /**
     * removeCheck
     */
    async removeCheck() {
        shelljs_1.default.exec('rm -rf ' + this.source, { silent: true });
        shelljs_1.default.exec('rm -f  ' + this.dest, { silent: true });
    }
    /**
     * check mksquashfs exists
     * @param compressor
     * @returns
     */
    async check(compressor) {
        let result = false;
        const stderr = shelljs_1.default.exec('mksquashfs ' + this.source + ' ' + this.dest + ' -comp ' + compressor + ' -ef ' + this.dest, { silent: true }).stderr;
        if (stderr === '') {
            result = true;
        }
        return result;
    }
}
exports.default = Compressors;
