const FSWrapper = require('@sisense-ps/fs-wrapper');
const TarWrapper = require('../lib/TarWrapper');

const CONSTANTS = require('../constants/WindowsConsts');

class AddonSourcesHelper {
    static async addonSourcesStatus(sourcePath, targetPath, ignorePattern) {
        const { PLUGIN_SOURCES_FOLDER } = CONSTANTS;
        const unpacked = FSWrapper.isPathExistSync(PLUGIN_SOURCES_FOLDER);

        const tarballResult = await TarWrapper.getTarFilesCount(sourcePath, targetPath, undefined, ignorePattern);
        const unpackCount = AddonSourcesHelper._extractedSourcesCount();

        return {
            unpacked,
            tarballCount: tarballResult.data,
            unpackCount
        }
    }

    static _extractedSourcesCount() {
        const { IGNORE_INSTALL_FOLDERS, PLUGIN_SOURCES_FOLDER } = CONSTANTS;
        let count = 0;

        FSWrapper.recursiveSearch(
            FSWrapper.combinePath([FSWrapper.getRootPath(), PLUGIN_SOURCES_FOLDER]),
            new RegExp('.*'),
            IGNORE_INSTALL_FOLDERS,
            true,
            (path, lstat) => {
                if (!lstat.isDirectory()) {
                    count++;
                }
            });

        return count;
    }
}

module.exports = AddonSourcesHelper;
