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() {
    return this.replaceFirst(RegExp(r'-[A-Z0-9]{8}-'), '-');
  }

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

  String toRelativeImagePath() {
    final uri = Uri.parse(this);
    return uri.path.replaceFirst('/storage/', '');
  }

  String normalizePhoneNumber() {
    var phone = this.replaceAll(RegExp(r'[^0-9+]'), '');

    if (phone.startsWith('+62')) {
      phone = '62${phone.substring(3)}';
    } else if (phone.startsWith('08')) {
      phone = '62${phone.substring(1)}';
    }

    if (!phone.startsWith('62')) {
      throw Exception('Nomor bukan format Indonesia');
    }

    return phone;
  }
}
