Files
Siro/siro_driver/lib/controller/functions/country_logic.dart
2026-06-11 18:22:59 +03:00

95 lines
3.6 KiB
Dart

import 'package:siro_driver/constant/box_name.dart';
import 'package:siro_driver/main.dart';
class CountryLogic {
/// Formats the phone number based on the country's dialing rules.
static String formatPhone(String phone, String country) {
phone = phone.replaceAll(RegExp(r'[ \-\(\)\+]'), '').trim();
if (country == 'Egypt') {
// Rule: 010, 011, 012, 015 -> 2010, 2011, 2012, 2015
if (phone.startsWith('0020')) phone = phone.replaceFirst('0020', '20');
if (phone.startsWith('01') && phone.length >= 10) {
return '20${phone.substring(1)}';
}
if (phone.startsWith('1') &&
phone.length >= 9 &&
!phone.startsWith('20')) {
return '20$phone';
}
if (!phone.startsWith('20') && phone.length > 8) return '20$phone';
} else if (country == 'Jordan') {
// Rule: 07x -> 9627x
if (phone.startsWith('00962')) phone = phone.replaceFirst('00962', '962');
if (phone.startsWith('07') && phone.length >= 9) {
return '962${phone.substring(1)}';
}
if (phone.startsWith('7') &&
phone.length >= 8 &&
!phone.startsWith('962')) {
return '962$phone';
}
if (!phone.startsWith('962') && phone.length > 7) return '962$phone';
} else {
// Default to Syria
if (phone.startsWith('00963')) phone = phone.replaceFirst('00963', '963');
if (phone.startsWith('0963')) phone = phone.replaceFirst('0963', '963');
if (phone.startsWith('096309')) {
phone = phone.replaceFirst('096309', '963');
}
if (phone.startsWith('96309')) phone = '9639${phone.substring(5)}';
if (phone.startsWith('9630')) phone = '9639${phone.substring(4)}';
if (phone.startsWith('9639') && phone.length == 12) return phone;
if (phone.startsWith('963') &&
phone.length > 3 &&
!phone.startsWith('9639')) {
phone = '9639${phone.substring(3)}';
}
if (phone.startsWith('09')) return '963${phone.substring(1)}';
if (phone.startsWith('9') && phone.length == 9) return '963$phone';
if (phone.startsWith('0') && phone.length == 10) {
return '963${phone.substring(1)}';
}
}
return phone;
}
/// Returns the default country prefix (EG, JO, SY) for UI initial selection.
static String getCountryPrefix(String country) {
if (country == 'Egypt') return 'EG';
if (country == 'Jordan') return 'JO';
return 'SY';
}
/// Returns the default emergency number for the country.
static String getEmergencyNumber(String country) {
if (country == 'Egypt') return '122';
if (country == 'Jordan') return '911';
return '112'; // Syria
}
/// Returns the hint text for phone inputs based on the country.
static String getPhoneHint(String country) {
if (country == 'Egypt') return 'e.g. 01012345678 (Default +20)';
if (country == 'Jordan') return 'e.g. 0791234567 (Default +962)';
return 'e.g. 0912345678 (Default +963)'; // Syria
}
/// Helper to format phone using the current country in box.
static String formatCurrentCountryPhone(String phone) {
String cleanPhone = phone.replaceAll(RegExp(r'[ \-\(\)]'), '').trim();
if (cleanPhone.startsWith('+963') || cleanPhone.startsWith('00963')) {
return formatPhone(cleanPhone, 'Syria');
}
if (cleanPhone.startsWith('+20') || cleanPhone.startsWith('0020')) {
return formatPhone(cleanPhone, 'Egypt');
}
if (cleanPhone.startsWith('+962') || cleanPhone.startsWith('00962')) {
return formatPhone(cleanPhone, 'Jordan');
}
final country = box.read(BoxName.countryCode) ?? 'Syria';
return formatPhone(cleanPhone, country);
}
}