import 'package:flutter/material.dart';
import 'package:flux/l10n/app_localizations.dart';

extension MyStringExtensions on String? {
  // Gestiamo anche il nullable per sicurezza
  String myFormat() {
    if (this == null || this!.trim().isEmpty) return '';

    return this!
        .trim()
        .toLowerCase()
        .split(' ')
        .where((word) => word.isNotEmpty) // Evita problemi con doppi spazi
        .map((word) {
          if (word.contains('iphone') || word.contains('ipad')) {
            return word.replaceFirst('ip', 'iP');
          }
          if (word.contains('imac')) {
            return word.replaceFirst('im', 'iM');
          }
          return word[0].toUpperCase() + word.substring(1);
        })
        .join(' ');
  }

  String fileExtension() {
    if (this == null || this!.trim().isEmpty) return '';

    final parts = this!.split('.');
    if (parts.length < 2) return ''; // Nessuna estensione trovata

    return parts.last.toLowerCase();
  }

  String fileNameWithoutExtension() {
    if (this == null || this!.trim().isEmpty) return '';
    this!.replaceAll(RegExp(r'[^a-zA-Z0-9\.\-]'), '_');
    final parts = this!.split('.');
    if (parts.length < 2) return this!; // Nessuna estensione trovata

    return parts
        .sublist(0, parts.length - 1)
        .join('.'); // Ritorna tutto tranne l'ultima parte
  }
}

extension LocalizationExtension on BuildContext {
  AppLocalizations get l10n => AppLocalizations.of(this)!;
}
