import 'package:flutter/material.dart';

extension DoubleFormatter on double {
  String toStringWithoutTrailingZero() {
    if (this % 1 == 0) {
      return toStringAsFixed(0);
    } else {
      return toString();
    }
  }

  String toRatingFormat({int maxDecimal = 2}) {
    final rounded = double.parse(toStringAsFixed(maxDecimal));
    String result = rounded.toString();
    if (result.contains('.')) {
      result = result.replaceFirst(RegExp(r'\.?0+$'), '');
    }

    return result;
  }

  String get toCompact {
    if (this < 1000) {
      return toInt().toString();
    } else {
      double result = this / 1000;

      String s = result.toStringAsFixed(2);

      s = s.replaceAll(RegExp(r"([.]*0+)(?!.*\d)"), "");

      s = s.replaceAll('.', ',');

      return "${s}k";
    }
  }
}

extension StringFormatter on String {
  String toUniqueNumberCleaner() {
    String clean = this.replaceFirst(RegExp(r'-[A-Z0-9]{8}-'), '-');

    clean = clean.replaceFirst(RegExp(r'-[A-Z0-9]{3}(?=\d{5})'), '-');
    
    return clean;
  }

  String capitalize() {
    return "${this[0].toUpperCase()}${substring(1)}";
  }
}

extension TimeOfDayExtension on TimeOfDay {
  bool isAfterOrEqual(TimeOfDay other) {
    return this.compareTo(other) >= 0;
  }
}
