<commit_before>//
//  PhoneNumberKit.swift
//  PhoneNumberKit
//
//  Created by <NAME> on 03/10/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation
import CoreTelephony

public class PhoneNumberKit: NSObject {
    
    let metadata = Metadata.sharedInstance
    let regex = RegularExpressions.sharedInstance

    // MARK: Multiple Parsing
    
    /**
    Fastest way to parse an array of phone numbers. Uses default region code.
    - Parameter rawNumbers: An array of raw number strings.
    - Returns: An array of valid PhoneNumber objects.
    */
    public func parseMultiple(rawNumbers: [String]) -> [PhoneNumber] {
        return self.parseMultiple(rawNumbers, region: self.defaultRegionCode())
    }
    
    /**
    Fastest way to parse an array of phone numbers. Uses custom region code.
    - Parameter rawNumbers: An array of raw number strings.
    - Parameter region: ISO 639 compliant region code.
    - Returns: An array of valid PhoneNumber objects.
    */
    public func parseMultiple(rawNumbers: [String], region: String) -> [PhoneNumber] {
        return ParseManager().parseMultiple(rawNumbers, region: region)
    }

    // MARK: Country and region code
    
    /**
    Get a list of all the countries in the metadata database
    - Returns: An array of ISO 639 compliant region codes.
    */
    public func allCountries() -> [String] {
        let results = metadata.items.map{$0.codeID}
        return results
    }
    
    /**
    Get an array of ISO 639 compliant region codes corresponding to a given country code.
    - Parameter code: An international country code (e.g 44 for the UK).
    - Returns: An optional array of ISO 639 compliant region codes.
    */
    public func countriesForCode(code: UInt64) -> [String]? {
        let results = metadata.fetchCountriesForCode(code)?.map{$0.codeID}
        return results
    }
    
    /**
    Get an main ISO 639 compliant region code for a given country code.
    - Parameter code: An international country code (e.g 1 for the US).
    - Returns: A ISO 639 compliant region code string.
    */
    public func mainCountryForCode(code: UInt64) -> String? {
        let country = metadata.fetchMainCountryMetadataForCode(code)
        return country?.codeID
    }

    /**
    Get the region code for the given phone number
    - Paramter number: The phone number
    - Returns: Region code, eg "US", or nil if the region cannot be determined
    */
    public func regionCodeForNumber(number: PhoneNumber) -> String? {
        let countryCode = number.countryCode
        let regions = metadata.items.filter { $0.countryCode == countryCode }
        if regions.count == 1 {
            return regions[0].codeID
        }

        return getRegionCodeForNumber(number, fromRegionList: regions)
    }

    private func getRegionCodeForNumber(number: PhoneNumber, fromRegionList regions: [MetadataTerritory]) -> String? {
        let nationalNumber = String(number.nationalNumber)<commit_msg>Fix typo in comment<commit_after>//
//  PhoneNumberKit.swift
//  PhoneNumberKit
//
//  Created by <NAME> on 03/10/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation
import CoreTelephony

public class PhoneNumberKit: NSObject {
    
    let metadata = Metadata.sharedInstance
    let regex = RegularExpressions.sharedInstance

    // MARK: Multiple Parsing
    
    /**
    Fastest way to parse an array of phone numbers. Uses default region code.
    - Parameter rawNumbers: An array of raw number strings.
    - Returns: An array of valid PhoneNumber objects.
    */
    public func parseMultiple(rawNumbers: [String]) -> [PhoneNumber] {
        return self.parseMultiple(rawNumbers, region: self.defaultRegionCode())
    }
    
    /**
    Fastest way to parse an array of phone numbers. Uses custom region code.
    - Parameter rawNumbers: An array of raw number strings.
    - Parameter region: ISO 639 compliant region code.
    - Returns: An array of valid PhoneNumber objects.
    */
    public func parseMultiple(rawNumbers: [String], region: String) -> [PhoneNumber] {
        return ParseManager().parseMultiple(rawNumbers, region: region)
    }

    // MARK: Country and region code
    
    /**
    Get a list of all the countries in the metadata database
    - Returns: An array of ISO 639 compliant region codes.
    */
    public func allCountries() -> [String] {
        let results = metadata.items.map{$0.codeID}
        return results
    }
    
    /**
    Get an array of ISO 639 compliant region codes corresponding to a given country code.
    - Parameter code: An international country code (e.g 44 for the UK).
    - Returns: An optional array of ISO 639 compliant region codes.
    */
    public func countriesForCode(code: UInt64) -> [String]? {
        let results = metadata.fetchCountriesForCode(code)?.map{$0.codeID}
        return results
    }
    
    /**
    Get an main ISO 639 compliant region code for a given country code.
    - Parameter code: An international country code (e.g 1 for the US).
    - Returns: A ISO 639 compliant region code string.
    */
    public func mainCountryForCode(code: UInt64) -> String? {
        let country = metadata.fetchMainCountryMetadataForCode(code)
        return country?.codeID
    }

    /**
    Get the region code for the given phone number
    - Parameter number: The phone number
    - Returns: Region code, eg "US", or nil if the region cannot be determined
    */
    public func regionCodeForNumber(number: PhoneNumber) -> String? {
        let countryCode = number.countryCode
        let regions = metadata.items.filter { $0.countryCode == countryCode }
        if regions.count == 1 {
            return regions[0].codeID
        }

        return getRegionCodeForNumber(number, fromRegionList: regions)
    }

    private func getRegionCodeForNumber(number: PhoneNumber, fromRegionList regions: [MetadataTerritory]) -> String? {
        let nationalNumber = String(number.nationalNumber)
{'fraction_non_alphanumeric': 0.0845961698584513, 'fraction_numerical': 0.013988343047460449, 'mean_word_length': 2.7028360049321827, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7372500', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1571, 'n_words': 598}