class AddressCreateRequest {
  const AddressCreateRequest({
    required this.label,
    required this.street,
    required this.colonia,
  });

  final String label;
  final String street;
  final String colonia;

  factory AddressCreateRequest.fromJson(Map<String, dynamic> json) {
    return AddressCreateRequest(
      label: _pickString(<dynamic>[json['label'], json['alias']]) ?? '',
      street: _pickString(<dynamic>[json['calle'], json['street']]) ?? '',
      colonia: _pickString(<dynamic>[json['colonia'], json['colony']]) ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'label': label,
      'calle': street,
      'colonia': colonia,
    };
  }

  AddressCreateRequest copyWith({
    String? label,
    String? street,
    String? colonia,
  }) {
    return AddressCreateRequest(
      label: label ?? this.label,
      street: street ?? this.street,
      colonia: colonia ?? this.colonia,
    );
  }

  static String? _pickString(Iterable<dynamic> candidates) {
    for (final candidate in candidates) {
      if (candidate is String && candidate.isNotEmpty) {
        return candidate;
      }
      if (candidate != null && candidate.toString().isNotEmpty) {
        return candidate.toString();
      }
    }

    return null;
  }
}
