const AdvancedNameGenerator = require('./nameGenerator');
const SelfModifyingCode = require('./selfModifying');
const AntiStaticAnalysis = require('./antiStatic');
const ControlFlowFlattening = require('./controlFlow');
const AdvancedLMAOEncryption = require('./lmaoEncryption')
const UltraCodeCryptor = require('./codeCryptor')

class UltimateUnreadableObfuscator {
    constructor() {
        this.nameGen = new AdvancedNameGenerator();
        this.lmaoEncryption = new AdvancedLMAOEncryption();
        this.codeCryptor = new UltraCodeCryptor();
        this.controlFlow = new ControlFlowFlattening();
        this.selfModifying = new SelfModifyingCode();
        this.antiStatic = new AntiStaticAnalysis();
        this.varMap = new Map();
    }

    obfuscateStrings(code) {
        return code.replace(/"([^"\\]*(\\.[^"\\]*)*)"/g, (match, str) => {
            if (str.includes('lmaobfuscator_') || str.match(/^[LMAO!]+$/)) {
                return match;
            }
            return this.lmaoEncryption.obfuscateString(str);
        });
    }

    obfuscateVariables(code) {
        return code.replace(/\b(var|let|const)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/g, (match, keyword, varName) => {
            if (varName.startsWith('lmaobfuscator_') || varName.includes('\u200C')) {
                return match;
            }
            
            if (!this.varMap.has(varName)) {
                this.varMap.set(varName, this.nameGen.generateObfuscatedName());
            }
            return `${keyword} ${this.varMap.get(varName)}`;
        });
    }

    replaceVariableUsages(code) {
        for (let [original, obfuscated] of this.varMap) {
            const regex = new RegExp(`\\b${original}\\b`, 'g');
            code = code.replace(regex, obfuscated);
        }
        return code;
    }

    generateJunkCode() {
        const junkVar1 = this.nameGen.generateObfuscatedName();
        const junkVar2 = this.nameGen.generateObfuscatedName();
        const junkVar3 = this.nameGen.generateObfuscatedName();
        
        return `
        var ${junkVar1} = function() {
            var ${junkVar2} = Math.random() * 1000;
            for (var i = 0; i < ${junkVar2}; i++) {
                Math.sin(i) * Math.cos(i);
            }
            return ${junkVar2};
        };
        
        var ${junkVar3} = ${junkVar1}();
        
        if (false) {
            console.log(${this.lmaoEncryption.obfuscateString("You found a fake secret!")});
        }`;
    }

    obfuscate(code) {
        console.log("🔄 Starting ULTIMATE unreadable obfuscation...");
        
        console.log("🔤 Step 1: Advanced string encryption...");
        let result = this.obfuscateStrings(code);
        
        console.log("🔀 Step 2: Variable name destruction...");
        result = this.obfuscateVariables(result);
        result = this.replaceVariableUsages(result);
        
        console.log("🗑️ Step 3: Massive junk code injection...");
        const junk1 = this.generateJunkCode();
        const junk2 = this.generateJunkCode();
        const junk3 = this.generateJunkCode();
        result = junk1 + result + junk2 + junk3;
        
        console.log("🌊 Step 4: Control flow flattening...");
        result = this.controlFlow.flattenCode(result);
        
        console.log("🤖 Step 5: Self-modifying code injection...");
        result = this.selfModifying.generatePolymorphicFunction(result);
        
        console.log("🛡️ Step 6: Anti-static analysis...");
        const antiStatic = this.antiStatic.generateDynamicEval() + 
                          this.antiStatic.generateAntiDebugger();
        result = antiStatic + result;
        
        console.log("📦 Step 7: Extreme minification...");
        result = result.replace(/\s+/g, ' ').replace(/; /g, ';').replace(/{ /g, '{').replace(/ }/g, '}').trim(); // FIXED: better minification
        
        console.log("🔐 Step 8: ULTRA CODE ENCRYPTION (Fixed)...");
        const encryptedCode = this.codeCryptor.encryptCode(result);
        let finalCode = this.codeCryptor.generateUltraDecryptor(encryptedCode);
        
        // NEW: Add watermark at the beginning
        const watermark = `
/*
  _      __  __          ____  ____  ______ _    _  _____  _____       _______ ____  _____  
 | |    |  \/  |   /\   / __ \|  _ \|  ____| |  | |/ ____|/ ____|   /\|__   __/ __ \|  __ \ 
 | |    | \  / |  /  \ | |  | | |_) | |__  | |  | | (___ | |       /  \  | | | |  | | |__) |
 | |    | |\/| | / /\ \| |  | |  _ <|  __| | |  | |\___ \| |      / /\ \ | | | |  | |  _  / 
 | |____| |  | |/ ____ \ |__| | |_) | |    | |__| |____) | |____ / ____ \| | | |__| | | \ \ 
 |______|_|  |_/_/    \_\____/|____/|_|     \____/|_____/ \_____/_/    \_\_|  \____/|_|  \_\
                                                                                            
                L M A O B F U S C A T O R
            DEVELOPED BY BOTSFELO (id375)
*/   
        \n`;
        finalCode = watermark + finalCode;
        
        console.log("✅ ULTIMATE unreadable obfuscation complete!");
        console.log(`📊 Complexity level: EXTREME`);
        console.log(`📏 Final size: ${finalCode.length} characters`);
        console.log(`🛡️ Protection layers: 8+`);
        console.log(`🧠 AI resistance: MAXIMUM`);
        
        return finalCode;
    }
}
module.exports = UltimateUnreadableObfuscator;
почини watermark от этого /*
  _      __  __          ____  ____  ______ _    _  _____  _____       _______ ____  _____  
 | |    |  /  |   /   / __ |  _ |  ____| |  | |/ ____|/ ____|   /|__   __/ __ |  __  
 | |    |   / |  /   | |  | | |_) | |__  | |  | | (___ | |       /    | | | |  | | |__) |
 | |    | |/| | / / | |  | |  _ <|  __| | |  | |___ | |      / /  | | | |  | |  _  / 
 | |____| |  | |/ ____  |__| | |_) | |    | |__| |____) | |____ / ____ | | | |__| | |   
 |______|_|  |_/_/    _____/|____/|_|     ____/|_____/ _____/_/    __|  ____/|_|  _                                                                                            
                L M A O B F U S C A T O R
            DEVELOPED BY BOTSFELO (id375)
*/