/**
 * FeHelper Chrome Extension Builder By Gulp
 * @author zhaoxianlie
 */
const gulp = require('gulp');
const clean = require('gulp-clean');
const copy = require('gulp-copy');
const zip = require('gulp-zip');
const uglifyjs = require('gulp-uglify-es').default;
const uglifycss = require('gulp-uglifycss');
const htmlmin = require('gulp-htmlmin');
const jsonmin = require('gulp-jsonminify');
const fs = require('fs');
const through = require('through2');
const path = require('path');
const pretty = require('pretty-bytes');
const shell = require('shelljs');
const babel = require('gulp-babel');
const assert = require('assert');
const gulpIf = require('gulp-if');
const imagemin = require('gulp-imagemin');
const imageminGifsicle = require('imagemin-gifsicle');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminSvgo = require('imagemin-svgo');
let isSilentDetect = false; // <-- 添加全局标志位
const FIREFOX_REMOVE_TOOLS = [
    'color-picker', 'postman', 'devtools', 'websocket', 'page-timing',
    'grid-ruler', 'naotu', 'screenshot', 'page-monkey', 'excel2json'
];
// 清理输出目录
function cleanOutput(outputDir = 'output-chrome') {
    return gulp.src(outputDir, {read: false, allowEmpty: true}).pipe(clean({force: true}));
}
// 复制静态资源
function copyAssets(outputDir = 'output-chrome/apps') {
    return gulp.src(['apps/**/*.{gif,png,jpg,jpeg,cur,ico,ttf,woff2,svg,md,txt,json}']).pipe(gulp.dest(outputDir));
}
// 处理JSON文件
function processJson(outputDir = 'output-chrome/apps') {
    return gulp.src('apps/**/*.json').pipe(jsonmin()).pipe(gulp.dest(outputDir));
}
// 处理HTML文件
function processHtml(outputDir = 'output-chrome/apps') {
    return gulp.src('apps/**/*.html').pipe(htmlmin({collapseWhitespace: true})).pipe(gulp.dest(outputDir));
}
// 合并 & 压缩 js
function processJs(outputDir = 'output-chrome/apps') {
    let jsMerge = () => {
        return through.obj(function (file, enc, cb) {
            let contents = file.contents.toString('utf-8');
            let merge = (fp, fc) => {
                return fc.replace(/__importScript\(\s*(['"])([^'"]*)\1\s*\)/gm, function (frag, $1, mod) {
                    let mp = path.resolve(fp, '../' + mod + (/\.js$/.test(mod) ? '' : '.js'));
                    let mc = fs.readFileSync(mp).toString('utf-8');
                    return merge(mp, mc + ';');
                });
            };
            contents = merge(file.path, contents);
            file.contents = Buffer.from(contents);
            this.push(file);
            return cb();
        })
    };
    const shouldSkipProcessing = (file) => {
        const relativePath = path.relative(path.join(process.cwd(), 'apps'), file.path);
        return relativePath === 'chart-maker/lib/xlsx.full.min.js' 
            || relativePath === 'static/vendor/evalCore.min.js' 
            || relativePath === 'code-compress/htmlminifier.min.js';
    };
    return gulp.src('apps/**/*.js')
        .pipe(jsMerge())
        .pipe(gulpIf(file => !shouldSkipProcessing(file), babel({
            presets: [
                ['@babel/preset-env', { modules: false }]
            ]
        })))
        .pipe(gulpIf(file => !shouldSkipProcessing(file), uglifyjs({
            compress: {
                ecma: 2015
            }
        })))
        .pipe(gulp.dest(outputDir));
}
// 合并 & 压缩 css
function processCss(outputDir = 'output-chrome/apps') {
    let cssMerge = () => {
        return through.obj(function (file, enc, cb) {
            let contents = file.contents.toString('utf-8');
            let merge = (fp, fc) => {
                return fc.replace(/\@import\s+(url\()?\s*(['"])(.*)\2\s*(\))?\s*;?/gm, function (frag, $1, $2, mod) {
                    let mp = path.resolve(fp, '../' + mod + (/\.css$/.test(mod) ? '' : '.css'));
                    let mc = fs.readFileSync(mp).toString('utf-8');
                    return merge(mp, mc);
                });
            };
            contents = merge(file.path, contents);
            file.contents = Buffer.from(contents);
            this.push(file);
            return cb();
        })
    };
    return gulp.src('apps/**/*.css').pipe(cssMerge()).pipe(uglifycss()).pipe(gulp.dest(outputDir));
}
// 添加图片压缩任务
function compressImages(outputDir = 'output-chrome/apps') {
    return gulp.src(path.join(outputDir, '**/*.{png,jpg,jpeg,gif,svg}'))
        .pipe(imagemin([
            imageminGifsicle({interlaced: true}),
            imageminMozjpeg({quality: 75, progressive: true}),
            imageminSvgo({
                plugins: [
                    {removeViewBox: true},
                    {cleanupIDs: false}
                ]
            })
        ]))
        .pipe(gulp.dest(outputDir));
}
// 清理冗余文件,并且打包成zip,发布到chrome webstore
function zipPackage(outputRoot = 'output-chrome', cb) {
    let pathOfMF = path.join(outputRoot, 'apps/manifest.json');
    let manifest = require(path.resolve(pathOfMF));
    manifest.name = manifest.name.replace('-Dev', '');
    fs.writeFileSync(pathOfMF, JSON.stringify(manifest));
    let pkgName = 'fehelper.zip';
    if (outputRoot === 'output-firefox') {
        pkgName = 'fehelper.xpi';
    }
    shell.exec(`cd ${outputRoot}/apps && rm -rf ../${pkgName} && zip -r ../${pkgName} ./* > /dev/null && cd ../../`);
    let size = fs.statSync(`${outputRoot}/${pkgName}`).size;
    size = pretty(size);
    console.log('\n\n================================================================================');
    console.log('    当前版本:', manifest.version, '\t文件大小:', size);
    if (outputRoot === 'output-chrome') {
        console.log('    去Chrome商店发布吧:https://chrome.google.com/webstore/devconsole');
    } else if (outputRoot === 'output-edge') {
        console.log('    去Edge商店发布吧:https://partner.microsoft.com/zh-cn/dashboard/microsoftedge/overview');
    } else if (outputRoot === 'output-firefox') {
        console.log('    去Firefox商店发布吧:https://addons.mozilla.org/zh-CN/developers/addon/web前端助手-fehelper/versions');
    }
    console.log('================================================================================\n\n');
    if (cb) cb();
}
// 设置静默标志
function setSilentDetect(cb) {
    isSilentDetect = true;
    cb();
}
function unsetSilentDetect(cb) {
    isSilentDetect = false;
    cb();
}
// 检测未使用的静态文件(参数化outputDir)
function detectUnusedFiles(outputDir = 'output-chrome/apps', cb) {
    const allFiles = new Set();
    const referencedFiles = new Set();
    function shouldExcludeFile(filePath) {
        if (filePath.includes('content-script.js') || filePath.includes('content-script.css')) return true;
        if (filePath.includes('node_modules')) return true;
        if (filePath.endsWith('fh-config.js')) return true;
        return false;
    }
    function getAllFiles(dir) {
        const files = fs.readdirSync(dir);
        files.forEach(file => {
            const fullPath = path.join(dir, file);
            if (fs.statSync(fullPath).isDirectory()) {
                if (file !== 'node_modules') {
                    getAllFiles(fullPath);
                }
            } else {
                if (/\.(js|css|png|jpg|jpeg|gif|svg)$/i.test(file) && !shouldExcludeFile(fullPath)) {
                    const relativePath = path.relative(outputDir, fullPath);
                    allFiles.add(relativePath);
                }
            }
        });
    }
    function findReferences(content, filePath) {
        const fileDir = path.dirname(filePath);
        const patterns = [
            /['"`][^`'\"]*?([./\w-]+\.(?:js|css|png|jpg|jpeg|gif|svg))['"`]/g,
            /url\(['"]?([./\w-]+(?:\.(?:png|jpg|jpeg|gif|svg))?)['"]?\)/gi,
            /@import\s+['"]([^'\"]+\.css)['"];?/gi,
            /(?:src|href)=['"](chrome-extension:\/\/[^/]+\/)?([^'"?#]+(?:\.(?:js|css|png|jpg|jpeg|gif|svg)))['"]/gi
        ];
        patterns.forEach((pattern, index) => {
            let match;
            while ((match = pattern.exec(content)) !== null) {
                let extractedPath = '';
                if (index === 3) {
                    extractedPath = match[2];
                } else {
                    extractedPath = match[1];
                }
                if (!extractedPath || typeof extractedPath !== 'string') continue;
                if (shouldExcludeFile(extractedPath)) continue;
                let finalPathToAdd = '';
                const isChromeExt = index === 3 && match[1];
                if (isChromeExt || extractedPath.startsWith('/')) {
                    finalPathToAdd = extractedPath.startsWith('/') ? extractedPath.slice(1) : extractedPath;
                } else {
                    const absolutePath = path.resolve(fileDir, extractedPath);
                    finalPathToAdd = path.relative(outputDir, absolutePath);
                }
                if (finalPathToAdd && !shouldExcludeFile(finalPathToAdd)) {
                    referencedFiles.add(finalPathToAdd.replace(/\\/g, '/'));
                }
            }
        });
    }
    function processManifest() {
        const manifestPath = path.join(outputDir, 'manifest.json');
        if (fs.existsSync(manifestPath)) {
            const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
            const checkManifestField = (obj) => {
                if (typeof obj === 'string' && /\.(js|css|png|jpg|jpeg|gif|svg)$/i.test(obj)) {
                    const normalizedPath = obj.startsWith('/') ? obj.slice(1) : obj;
                    if (!shouldExcludeFile(normalizedPath)) {
                        referencedFiles.add(normalizedPath);
                    }
                } else if (Array.isArray(obj)) {
                    obj.forEach(item => checkManifestField(item));
                } else if (typeof obj === 'object' && obj !== null) {
                    Object.values(obj).forEach(value => checkManifestField(value));
                }
            };
            if (manifest.content_scripts) {
                manifest.content_scripts.forEach(script => {
                    if (script.js) {
                        script.js.forEach(js => {
                            const normalizedPath = js.startsWith('/') ? js.slice(1) : js;
                            referencedFiles.add(normalizedPath);
                        });
                    }
                    if (script.css) {
                        script.css.forEach(css => {
                            const normalizedPath = css.startsWith('/') ? css.slice(1) : css;
                            referencedFiles.add(normalizedPath);
                        });
                    }
                });
            }
            checkManifestField(manifest);
        }
    }
    function runTests() {
        if (!isSilentDetect) console.log('\n运行单元测试...');
        assert.strictEqual(shouldExcludeFile('path/to/content-script.js'), true, 'Should exclude content-script.js');
        assert.strictEqual(shouldExcludeFile('path/to/content-script.css'), true, 'Should exclude content-script.css');
        assert.strictEqual(shouldExcludeFile('path/to/node_modules/file.js'), true, 'Should exclude node_modules files');
        assert.strictEqual(shouldExcludeFile('path/to/normal.js'), false, 'Should not exclude normal files');
        const testContent = `