import type { StrapiApp } from '@strapi/strapi/admin';

const isProduction = process.env.NODE_ENV === 'production';

export default {
  config: {
    locales: [
      // Add locales if needed
    ],
    // Hide development features in production
    ...(isProduction && {
      // Disable telemetry
      telemetry: {
        enabled: false,
      },
    }),
  },
  bootstrap(app: StrapiApp) {
    if (isProduction) {
      // Hide marketplace icon using CSS
      if (typeof document !== 'undefined') {
        // Inject CSS to hide marketplace menu item
        const style = document.createElement('style');
        style.id = 'hide-marketplace-styles';
        style.textContent = `
          /* Hide marketplace menu item */
          a[href*="/marketplace"],
          a[href*="/plugins/marketplace"],
          nav a[aria-label*="marketplace" i],
          nav a[aria-label*="Marketplace" i],
          /* Hide by icon class if marketplace has specific icon */
          nav [data-testid*="marketplace" i],
          /* Hide by menu item structure */
          nav ul li a[href*="marketplace"] {
            display: none !important;
            visibility: hidden !important;
          }
          
          /* Alternative: Hide by Strapi's internal menu structure */
          [data-strapi-sidebar] a[href*="marketplace"],
          [data-strapi-sidebar] a[href*="/plugins/marketplace"] {
            display: none !important;
          }
        `;
        document.head.appendChild(style);

        // Also try to hide after DOM loads
        const hideMarketplace = () => {
          // Find all links that might be marketplace
          const links = document.querySelectorAll('a[href*="marketplace"], nav a');
          links.forEach((link: any) => {
            const href = link.getAttribute('href') || '';
            const ariaLabel = link.getAttribute('aria-label') || '';
            const text = link.textContent || '';
            
            if (
              href.includes('marketplace') ||
              ariaLabel.toLowerCase().includes('marketplace') ||
              text.toLowerCase().includes('marketplace')
            ) {
              link.style.display = 'none';
              link.style.visibility = 'hidden';
              // Also hide parent list item if exists
              const parent = link.closest('li');
              if (parent) {
                parent.style.display = 'none';
              }
            }
          });
        };

        // Run immediately and after DOM loads
        hideMarketplace();
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', hideMarketplace);
        }
        
        // Also use MutationObserver to catch dynamically added menu items
        const observer = new MutationObserver(hideMarketplace);
        observer.observe(document.body, {
          childList: true,
          subtree: true,
        });

        // Clean up observer after a delay (menu should be loaded by then)
        setTimeout(() => {
          observer.disconnect();
        }, 5000);
      }

      // Hide development and system information in production
      try {
        // Remove console logs in production admin panel
        if (typeof window !== 'undefined') {
          const originalConsole = { ...console };
          console.log = () => {};
          console.debug = () => {};
          console.info = () => {};
          
          // Restore on error (for debugging critical issues)
          window.addEventListener('error', () => {
            console.log = originalConsole.log;
            console.error = originalConsole.error;
          });
        }
      } catch (e) {
        // Silently fail if console manipulation fails
      }
    }
  },
};
