'use strict';

// GLOBAL
const path = require('path');
const concat = require('concat');

// LOCAL
const tools = require('../tools');

module.exports.concatFiles = (pkg, workingPath, euiVersionNumber = 10) => {
    return Promise.resolve()
        .then(() => {

            tools.logInfo('Concatenating JS files into main.js');

            tools.move(path.join(workingPath, 'main.js'), path.join(workingPath, 'main-old.js'));

            const files = [
                // covers both webpack and esbuild
                ...(tools.isFileExists(path.join(workingPath, 'runtime.js')) ? ['runtime.js'] : []),
                ...(tools.isFileExists(path.join(workingPath, 'polyfills.js')) ? ['polyfills.js'] : []),
                ...(tools.isFileExists(path.join(workingPath, 'scripts.js')) ? ['scripts.js'] : []),
                ...(tools.isFileExists(path.join(workingPath, 'vendor.js')) ? ['vendor.js'] : []),
                'main-old.js',
            ].map(f => path.join(workingPath, f));

            const outFilePath = path.join(workingPath, 'main.js');

            console.log(files);

            return concat(files, outFilePath);
        })

        .catch((e) => {
            throw e;
        });
};

module.exports.createEntryJsFile = (pkg, workingPath, euiVersionNumber = 15) => {
    return Promise.resolve()
        .then(() => {

            tools.logInfo('Importing files into entry.js');

            const files = [
                // covers both webpack and esbuild
                ...(tools.isFileExists(path.join(workingPath, 'runtime.js')) ? ['runtime.js'] : []),
                ...(tools.isFileExists(path.join(workingPath, 'polyfills.js')) ? ['polyfills.js'] : []),
                ...(tools.isFileExists(path.join(workingPath, 'scripts.js')) ? ['scripts.js'] : []),
                ...(tools.isFileExists(path.join(workingPath, 'vendor.js')) ? ['vendor.js'] : []),
                ...(tools.isFileExists(path.join(workingPath, 'main.js')) ? ['main.js'] : []),
            ];

            const outFilePath = path.join(workingPath, 'entry.js');

            console.log(files);

            let content = ``;
            files.forEach(f => content = `${content}\nimport \'./${f}\';`.trim());

            return tools.writeFileContent(outFilePath, content);
        })

        .catch((e) => {
            throw e;
        });
};

module.exports.copyPackageJson = (pkg, srcPath, destPath, euiVersion, entryName) => {
    return Promise.resolve()
        .then(() => {
            tools.logInfo('Copying package.json to dist');

            tools.copy(path.join(srcPath, 'package.json'), path.join(destPath, 'package.json'));

            if (euiVersion || entryName) {
                const packageJsonFile = path.join(destPath, 'package.json');
                const packageJson = require(packageJsonFile);

                packageJson.euiVersion = euiVersion;
                packageJson.entryName = entryName;

                tools.writeJsonFileSync(packageJsonFile, packageJson);
            }
        })

        .catch((e) => {
            throw e;
        });
};


module.exports.emptyIndexHtml = (pkg, destPath) => {
    return Promise.resolve()
        .then(() => {
            try {
                if (pkg.build.injectPiwikScript) {
                    tools.logInfo('Inject index.html with piwik script embedded');

                    const emptyIndexHtmlFile = path.join(__dirname, 'skeletons', 'elements', 'index-piwik.html');
                    const outputIndexHmtlFile = path.join(destPath, 'index.html');
                    return tools.copy(emptyIndexHtmlFile, outputIndexHmtlFile);

                } else {
                    tools.logInfo('Set empty index.html');

                    const emptyIndexHtmlFile = path.join(__dirname, 'skeletons', 'elements', 'index.html');
                    const outputIndexHmtlFile = path.join(destPath, 'index.html');
                    return tools.copy(emptyIndexHtmlFile, outputIndexHmtlFile);
                }

            } catch (e) {
                console.log(e);
            }
        })

        .catch((e) => {
            throw e;
        });
};


module.exports.cleanupUnusedAssets = (assetsPath, euiVersionNumber = 10) => {
    tools.logInfo(`Cleanup unused assets for version : ${euiVersionNumber} in ${assetsPath}`);

    if (euiVersionNumber > 10) {

        return Promise.resolve()
            .then(() => {
                return tools.rimraf(path.join(assetsPath, 'ecl'));
            })
            .then(() => {
                return tools.rimraf(path.join(assetsPath, 'icons', 'ionicons'));
            })
            .then(() => {
                return tools.rimraf(path.join(assetsPath, 'icons', 'svg'));
            })
            .then(() => {
                return tools.rimraf(path.join(assetsPath, 'icons', 'svg-src'));
            })
            .then(() => {
                return tools.rimraf(path.join(assetsPath, 'icons', 'flags'));
            })
            .then(() => {
                return tools.rimraf(path.join(assetsPath, 'fonts'));
            })
            .then(() => {
                return tools.rimraf(path.join(assetsPath, 'i18n-eui'));
            })

            .catch((e) => {
                throw e;
            });
    }
};
