<commit_before>//
//  WebBrowserManager.swift
//  Rocket.Chat
//
//  Created by <NAME> on 21/03/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import Foundation
import SafariServices

struct WebBrowserManager {
    private static let defaults = UserDefaults.standard
    static let defaultBrowserKey = "DefaultBrowserKey"
    static var browser: WebBrowserApp {
        guard
            let browserRaw = UserDefaults.standard.string(forKey: defaultBrowserKey),
            let browser = WebBrowserApp(rawValue: browserRaw),
            browser.isInstalled
        else {
            return .inAppSafari
        }

        return browser
    }

    static func open(url: URL) {
        browser.open(url: url)
    }

    static func set(defaultBrowser: WebBrowserApp) {
        defaults.set(defaultBrowser.rawValue, forKey: defaultBrowserKey)
    }
}

enum WebBrowserApp: String {
    case safari, inAppSafari, chrome

    internal enum URLScheme: String {
        case http = "http", https = "https", chrome = "googlechrome", chromeSecure = "googlechromes"
    }

    var isInstalled: Bool {
        let urlSuffix = "://"
        switch self {
        case .safari, .inAppSafari:
            return true
        case .chrome:
            guard let url = URL(string: URLScheme.chrome.rawValue + urlSuffix) else { return false }
            return UIApplication.shared.canOpenURL(url)
        }
    }

}

extension WebBrowserApp {
    func appSchemeURL(forURL url: URL) -> URL? {
        let scheme = url.scheme ?? ""
        var absoluteString = url.absoluteString

        switch self {
        case .chrome where scheme == URLScheme.http.rawValue:
            absoluteString = absoluteString.replacingOccurrences(
                of: URLScheme.http.rawValue,
                with: URLScheme.chrome.rawValue
            )<commit_msg>Add name property on WebBrowserApp enum<commit_after>//
//  WebBrowserManager.swift
//  Rocket.Chat
//
//  Created by <NAME> on 21/03/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import Foundation
import SafariServices

struct WebBrowserManager {
    private static let defaults = UserDefaults.standard
    static let defaultBrowserKey = "DefaultBrowserKey"
    static var browser: WebBrowserApp {
        guard
            let browserRaw = UserDefaults.standard.string(forKey: defaultBrowserKey),
            let browser = WebBrowserApp(rawValue: browserRaw),
            browser.isInstalled
        else {
            return .inAppSafari
        }

        return browser
    }

    static func open(url: URL) {
        browser.open(url: url)
    }

    static func set(defaultBrowser: WebBrowserApp) {
        defaults.set(defaultBrowser.rawValue, forKey: defaultBrowserKey)
    }
}

enum WebBrowserApp: String {
    case safari, inAppSafari, chrome

    internal enum URLScheme: String {
        case http = "http", https = "https", chrome = "googlechrome", chromeSecure = "googlechromes"
    }

    var name: String {
        switch self {
        case .safari: return "Safari"
        case .inAppSafari: return "In-App Browser"
        case .chrome: return "Chrome"
        }
    }

    var isInstalled: Bool {
        let urlSuffix = "://"
        switch self {
        case .safari, .inAppSafari:
            return true
        case .chrome:
            guard let url = URL(string: URLScheme.chrome.rawValue + urlSuffix) else { return false }
            return UIApplication.shared.canOpenURL(url)
        }
    }

}

extension WebBrowserApp {
    func appSchemeURL(forURL url: URL) -> URL? {
        let scheme = url.scheme ?? ""
        var absoluteString = url.absoluteString

        switch self {
        case .chrome where scheme == URLScheme.http.rawValue:
            absoluteString = absoluteString.replacingOccurrences(
                of: URLScheme.http.rawValue,
                with: URLScheme.chrome.rawValue
            )
{'fraction_non_alphanumeric': 0.0868898543317148, 'fraction_numerical': 0.005111167901865576, 'mean_word_length': 2.2055692055692058, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, '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': '2640371', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1072, 'n_words': 289}