import fs from 'node:fs';

import JavaScriptObfuscator from 'javascript-obfuscator';

/**
 * Лёгкие настройки: без selfDefending / controlFlowFlattening — меньше шансов сломать Electron main.
 * @param {string} filePath
 */
export function obfuscateMainBundleFile(filePath) {
  const code = fs.readFileSync(filePath, 'utf8');
  const obfuscated = JavaScriptObfuscator.obfuscate(code, {
    compact: true,
    controlFlowFlattening: false,
    deadCodeInjection: false,
    debugProtection: false,
    disableConsoleOutput: false,
    identifierNamesGenerator: 'hexadecimal',
    renameGlobals: false,
    selfDefending: false,
    simplify: true,
    stringArray: true,
    stringArrayEncoding: [],
    stringArrayThreshold: 0.75,
    transformObjectKeys: false,
  }).getObfuscatedCode();
  fs.writeFileSync(filePath, obfuscated, 'utf8');
}
