
export const telegramUtils = {
  // Haptic feedback functions
  haptic: {
    impact: (style: 'light' | 'medium' | 'heavy' | 'rigid' | 'soft' = 'medium') => {
      if (window.Telegram?.WebApp?.HapticFeedback) {
        try {
          window.Telegram.WebApp.HapticFeedback.impactOccurred(style);
        } catch (error) {
          console.warn('Haptic feedback not supported:', error);
        }
      }
    },

    notification: (type: 'error' | 'success' | 'warning') => {
      if (window.Telegram?.WebApp?.HapticFeedback) {
        try {
          window.Telegram.WebApp.HapticFeedback.notificationOccurred(type);
        } catch (error) {
          console.warn('Haptic feedback not supported:', error);
        }
      }
    },

    selection: () => {
      if (window.Telegram?.WebApp?.HapticFeedback) {
        try {
          window.Telegram.WebApp.HapticFeedback.selectionChanged();
        } catch (error) {
          console.warn('Haptic feedback not supported:', error);
        }
      }
    }
  },

  // Main button functions
  mainButton: {
    show: (text: string, onClick: () => void) => {
      if (window.Telegram?.WebApp?.MainButton) {
        try {
          window.Telegram.WebApp.MainButton.setText(text);
          window.Telegram.WebApp.MainButton.show();
          window.Telegram.WebApp.MainButton.onClick(onClick);
          window.Telegram.WebApp.MainButton.enable();
        } catch (error) {
          console.warn('Main button not supported:', error);
        }
      }
    },

    hide: (onClick: () => void) => {
      if (window.Telegram?.WebApp?.MainButton) {
        try {
          window.Telegram.WebApp.MainButton.offClick(onClick);
          window.Telegram.WebApp.MainButton.hide();
        } catch (error) {
          console.warn('Main button not supported:', error);
        }
      }
    },

    enable: () => {
      if (window.Telegram?.WebApp?.MainButton) {
        try {
          window.Telegram.WebApp.MainButton.enable();
        } catch (error) {
          console.warn('Main button not supported:', error);
        }
      }
    },

    disable: () => {
      if (window.Telegram?.WebApp?.MainButton) {
        try {
          window.Telegram.WebApp.MainButton.disable();
        } catch (error) {
          console.warn('Main button not supported:', error);
        }
      }
    }
  },

  // Back button functions
  backButton: {
    show: (onClick: () => void) => {
      if (window.Telegram?.WebApp?.BackButton) {
        try {
          window.Telegram.WebApp.BackButton.show();
          window.Telegram.WebApp.BackButton.onClick(onClick);
        } catch (error) {
          console.warn('Back button not supported:', error);
        }
      }
    },

    hide: (onClick: () => void) => {
      if (window.Telegram?.WebApp?.BackButton) {
        try {
          window.Telegram.WebApp.BackButton.offClick(onClick);
          window.Telegram.WebApp.BackButton.hide();
        } catch (error) {
          console.warn('Back button not supported:', error);
        }
      }
    }
  },

  // Theme functions
  theme: {
    setColors: (bgColor: string = '#0a0a0a', headerColor: string = '#1a1a1a') => {
      if (window.Telegram?.WebApp) {
        try {
          window.Telegram.WebApp.setBackgroundColor?.(bgColor);
          window.Telegram.WebApp.setHeaderColor?.(headerColor);
        } catch (error) {
          console.warn('Theme setting not supported:', error);
        }
      }
    }
  },

  // Utility functions
  utils: {
    isTelegramWebApp: (): boolean => {
      return !!window.Telegram?.WebApp;
    },

    getInitData: (): string | null => {
      return window.Telegram?.WebApp?.initData || null;
    },

    expand: () => {
      if (window.Telegram?.WebApp) {
        try {
          window.Telegram.WebApp.expand();
        } catch (error) {
          console.warn('Expand not supported:', error);
        }
      }
    },

    close: () => {
      if (window.Telegram?.WebApp) {
        try {
          window.Telegram.WebApp.close();
        } catch (error) {
          console.warn('Close not supported:', error);
        }
      }
    }
  }
};