// Anti-reverse engineering and tampering protection
(function() {
  'use strict';

  // Obfuscated strings
  const _0x9a = [100,101,98,117,103,103,101,114]; // debugger
  const _0x8b = [99,111,110,115,111,108,101]; // console
  const _0x7c = [116,111,83,116,114,105,110,103]; // toString

  // String decoder
  const _$ = (arr) => arr.map(c => String.fromCharCode(c)).join('');

  // Anti-debugger protection
  const _0x1a2b = function() {
    const _0x4f = function() {
      const _0x8d = function() {
        return !![];
      };
      return _0x8d[_$(_0x7c)]()[_$([115,101,97,114,99,104])](_$([40,92,43,92,43,92,41])) !== -1;
    };

    if (_0x4f()) {
      setInterval(function() {
        (function() {
          return ![];
        })[_$([99,111,110,115,116,114,117,99,116,111,114])](_$(_0x9a) + _$([59]))[_$([99,97,108,108])](_$([97,99,116,105,111,110]));
      }, 4000);
    }
  };

  // Console protection (disable in production)
  const _0x2b3c = function() {
    const _0x3e = window[_$(_0x8b)] || {};
    const _0x4f = [108,111,103,119,97,114,110,101,114,114,111,114,105,110,102,111,100,101,98,117,103];

    ['log', 'warn', 'error', 'info', 'debug'].forEach((method, i) => {
      try {
        const original = _0x3e[method];
        _0x3e[method] = function() {
          // Allow in dev, block sensitive logs in production
          if (localStorage.getItem(_$([100,101,118,77,111,100,101])) === 'true') {
            original && original.apply(_0x3e, arguments);
          }
        };
      } catch(e) {}
    });
  };

  // Integrity check for critical functions
  const _0x3c4d = function() {
    const _0x5a = window.firebase;
    const _0x6b = [115,105,103,110,73,110,65,110,111,110,121,109,111,117,115,108,121];

    if (!_0x5a || typeof _0x5a[_$([97,117,116,104])] !== 'function') {
      return false;
    }

    const _0x7c = _0x5a[_$([97,117,116,104])]();
    if (!_0x7c || typeof _0x7c[_$(_0x6b)] !== 'function') {
      return false;
    }

    return true;
  };

  // DevTools detection
  const _0x4d5e = function() {
    const _0x8e = {
      open: false,
      orientation: null
    };

    const _0x9f = setInterval(function() {
      const widthThreshold = window.outerWidth - window.innerWidth > 160;
      const heightThreshold = window.outerHeight - window.innerHeight > 160;
      const orientation = widthThreshold ? 'vertical' : 'horizontal';

      if (!(heightThreshold && widthThreshold) &&
          ((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)) {
        if (!_0x8e.open || _0x8e.orientation !== orientation) {
          _0x8e.open = true;
          _0x8e.orientation = orientation;

          // Optionally: Trigger protection action
          // location.reload(); // Reload page if DevTools detected
        }
      } else {
        _0x8e.open = false;
        _0x8e.orientation = null;
      }
    }, 500);

    return _0x8e;
  };

  // Code integrity check using checksum
  const _0x5e6f = function(funcStr) {
    let hash = 0;
    for (let i = 0; i < funcStr.length; i++) {
      const char = funcStr.charCodeAt(i);
      hash = ((hash << 5) - hash) + char;
      hash = hash & hash;
    }
    return hash;
  };

  // Function wrapper to detect modification
  const _0x6f7g = function(fn, expectedHash) {
    return function() {
      const currentHash = _0x5e6f(fn.toString());
      if (currentHash !== expectedHash) {
        console.warn(_$([70,117,110,99,116,105,111,110,32,109,111,100,105,102,105,101,100]));
        return;
      }
      return fn.apply(this, arguments);
    };
  };

  // Timing attack prevention
  const _0x7g8h = function(fn, minDelay = 100) {
    let lastCall = 0;
    return function() {
      const now = Date.now();
      if (now - lastCall < minDelay) {
        return; // Prevent rapid calls (potential automation)
      }
      lastCall = now;
      return fn.apply(this, arguments);
    };
  };

  const _init = function() {
    try {
      const _checkFirebase = function() {
        if (_0x3c4d()) {
          console.log(_$([65,110,116,105,45,116,97,109,112,101,114,32,105,110,105,116,105,97,108,105,122,101,100]));
        } else {
          setTimeout(_checkFirebase, 500);
        }
      };

      setTimeout(_checkFirebase, 1000);

      if (!localStorage.getItem(_$([100,101,118,77,111,100,101]))) {
        // _0x2b3c();
      }
    } catch(e) {
      // Silent fail
    }
  };

  // Auto-initialize on load
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', _init);
  } else {
    _init();
  }

  // Export utilities for use in other modules
  window.AntiTamper = {
    protect: _0x6f7g,
    throttle: _0x7g8h,
    checkIntegrity: _0x3c4d,
    hash: _0x5e6f
  };

})();
