<commit_before>//
//  DiscoveryConfig.swift
//  edX
//
//  Created by <NAME> on 1/5/16.
//  Copyright © 2016 edX. All rights reserved.
//

import Foundation
import edXCore

enum DiscoveryConfigType: String {
    case native = "native"
    case webview = "webview"
    case none = "none"
}

enum DiscoveryKeys: String, RawStringExtractable {
    case discovery = "DISCOVERY"
    case discoveryType = "TYPE"
    case webview = "WEBVIEW"
    case baseURL = "BASE_URL"
    case detailTemplate = "DETAIL_TEMPLATE"
    case searchEnabled = "SEARCH_ENABLED"
    case course = "COURSE"
    case subjectFilterEnabled = "SUBJECT_FILTER_ENABLED"
    case exploreSubjectsURL = "EXPLORE_SUBJECTS_URL"
    case program = "PROGRAM"
    case degree = "DEGREE"
}

class DiscoveryWebviewConfig: NSObject {
    let baseURL: URL?
    let exploreSubjectsURL: URL?
    let detailTemplate: String?
    let searchEnabled: Bool
    let subjectFilterEnabled: Bool
    
    init(dictionary: [String: AnyObject]) {
        baseURL = (dictionary[DiscoveryKeys.baseURL] as? String).flatMap { URL(string:$0)}
        detailTemplate = dictionary[DiscoveryKeys.detailTemplate] as? String
        searchEnabled = dictionary[DiscoveryKeys.searchEnabled] as? Bool ?? false
        subjectFilterEnabled = dictionary[DiscoveryKeys.subjectFilterEnabled] as? Bool ?? false
        exploreSubjectsURL = (dictionary[DiscoveryKeys.exploreSubjectsURL] as? String).flatMap { URL(string:$0)}
    }
}

class DiscoveryConfig: NSObject {
    let course: CourseDiscovery
    let program: ProgramDiscovery
    let degree: DegreeDiscovery
    
    init(dictionary: [String: AnyObject]) {
        course = CourseDiscovery(dictionary: dictionary[DiscoveryKeys.course] as? [String: AnyObject] ?? [:])
        program = ProgramDiscovery(with: course, dictionary: dictionary[DiscoveryKeys.program] as? [String: AnyObject] ?? [:])
        degree = DegreeDiscovery(with: course, dictionary: dictionary[DiscoveryKeys.degree] as? [String: AnyObject] ?? [:])
    }
}

class CourseDiscovery: DiscoveryBase {
    
    var isEnabled: Bool {
        return type != .none
    }
    
    // Associated swift enums can not be used in objective-c, that's why this extra computed property needed
    var isCourseDiscoveryNative: Bool {
        return type == .native
    }
}

class ProgramDiscovery: DiscoveryBase {
    
    private let courseDiscovery: CourseDiscovery
    
    init(with courseDiscovery: CourseDiscovery, dictionary: [String : AnyObject]) {
        self.courseDiscovery = courseDiscovery
        super.init(dictionary: dictionary)
    }
    
    var isEnabled: Bool {
        return courseDiscovery.type == .webview && type == .webview
    }
}

class DegreeDiscovery: ProgramDiscovery { }

class DiscoveryBase: NSObject {
    private(set) var type: DiscoveryConfigType
    let webview: DiscoveryWebviewConfig
    
    init(dictionary: [String: AnyObject]) {
        type = (dictionary[DiscoveryKeys.discoveryType] as? String).flatMap { DiscoveryConfigType(rawValue: $0) } ?? .none
        webview = DiscoveryWebviewConfig(dictionary: dictionary[DiscoveryKeys.webview] as? [String: AnyObject] ?? [:])
    }
}

extension OEXConfig {
    
    var discovery: DiscoveryConfig {
        return DiscoveryConfig(dictionary: self[DiscoveryKeys.discovery.rawValue] as? [String:AnyObject] ?? [:])
    }
}
<commit_msg>LEARNER-7049, make degree tab dependent on program (#1242)<commit_after>//
//  DiscoveryConfig.swift
//  edX
//
//  Created by <NAME> on 1/5/16.
//  Copyright © 2016 edX. All rights reserved.
//

import Foundation
import edXCore

enum DiscoveryConfigType: String {
    case native = "native"
    case webview = "webview"
    case none = "none"
}

enum DiscoveryKeys: String, RawStringExtractable {
    case discovery = "DISCOVERY"
    case discoveryType = "TYPE"
    case webview = "WEBVIEW"
    case baseURL = "BASE_URL"
    case detailTemplate = "DETAIL_TEMPLATE"
    case searchEnabled = "SEARCH_ENABLED"
    case course = "COURSE"
    case subjectFilterEnabled = "SUBJECT_FILTER_ENABLED"
    case exploreSubjectsURL = "EXPLORE_SUBJECTS_URL"
    case program = "PROGRAM"
    case degree = "DEGREE"
}

class DiscoveryWebviewConfig: NSObject {
    let baseURL: URL?
    let exploreSubjectsURL: URL?
    let detailTemplate: String?
    let searchEnabled: Bool
    let subjectFilterEnabled: Bool
    
    init(dictionary: [String: AnyObject]) {
        baseURL = (dictionary[DiscoveryKeys.baseURL] as? String).flatMap { URL(string:$0)}
        detailTemplate = dictionary[DiscoveryKeys.detailTemplate] as? String
        searchEnabled = dictionary[DiscoveryKeys.searchEnabled] as? Bool ?? false
        subjectFilterEnabled = dictionary[DiscoveryKeys.subjectFilterEnabled] as? Bool ?? false
        exploreSubjectsURL = (dictionary[DiscoveryKeys.exploreSubjectsURL] as? String).flatMap { URL(string:$0)}
    }
}

class DiscoveryConfig: NSObject {
    let course: CourseDiscovery
    let program: ProgramDiscovery
    let degree: DegreeDiscovery
    
    init(dictionary: [String: AnyObject]) {
        course = CourseDiscovery(dictionary: dictionary[DiscoveryKeys.course] as? [String: AnyObject] ?? [:])
        program = ProgramDiscovery(with: course, dictionary: dictionary[DiscoveryKeys.program] as? [String: AnyObject] ?? [:])
        degree = DegreeDiscovery(with: program, dictionary: dictionary[DiscoveryKeys.degree] as? [String: AnyObject] ?? [:])
    }
}

class CourseDiscovery: DiscoveryBase {
    
    var isEnabled: Bool {
        return type != .none
    }
    
    // Associated swift enums can not be used in objective-c, that's why this extra computed property needed
    var isCourseDiscoveryNative: Bool {
        return type == .native
    }
}

class ProgramDiscovery: DiscoveryBase {
    
    private let courseDiscovery: CourseDiscovery
    
    init(with courseDiscovery: CourseDiscovery, dictionary: [String : AnyObject]) {
        self.courseDiscovery = courseDiscovery
        super.init(dictionary: dictionary)
    }
    
    var isEnabled: Bool {
        return courseDiscovery.type == .webview && type == .webview
    }
}

class DegreeDiscovery: DiscoveryBase {
    
    private let programDiscovery: ProgramDiscovery
    
    init(with programDiscovery: ProgramDiscovery, dictionary: [String : AnyObject]) {
        self.programDiscovery = programDiscovery
        super.init(dictionary: dictionary)
    }
    
    var isEnabled: Bool {
        return programDiscovery.isEnabled && type == .webview
    }
}

class DiscoveryBase: NSObject {
    private(set) var type: DiscoveryConfigType
    let webview: DiscoveryWebviewConfig
    
    init(dictionary: [String: AnyObject]) {
        type = (dictionary[DiscoveryKeys.discoveryType] as? String).flatMap { DiscoveryConfigType(rawValue: $0) } ?? .none
        webview = DiscoveryWebviewConfig(dictionary: dictionary[DiscoveryKeys.webview] as? [String: AnyObject] ?? [:])
    }
}

extension OEXConfig {
    
    var discovery: DiscoveryConfig {
        return DiscoveryConfig(dictionary: self[DiscoveryKeys.discovery.rawValue] as? [String:AnyObject] ?? [:])
    }
}

{'fraction_non_alphanumeric': 0.09770195967855633, 'fraction_numerical': 0.004229522064006767, 'mean_word_length': 3.5155951623169956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6584590', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2024, 'n_words': 518}