<commit_before>//
//  Form.m
//  mage-ios-sdk
//
//

import Foundation
import SSZipArchive
import CoreData

@objc public class Form: NSManagedObject {
    
    @objc public static let MAGEFormFetched = "mil.nga.giat.mage.form.fetched";
    
    @discardableResult
    @objc public static func deleteAllFormsForEvent(eventId: NSNumber, context: NSManagedObjectContext) -> Bool {
        return Form.mr_deleteAll(matching: NSPredicate(format: "eventId == %@", eventId), in: context)
    }
    
    @discardableResult
    @objc public static func createForm(eventId: NSNumber, formJson: [AnyHashable : Any], context: NSManagedObjectContext) -> Form? {
        if let formId = formJson[FormKey.id.key] as? NSNumber, let form = Form.mr_createEntity(in: context), let formJsonEntity = FormJson.mr_createEntity(in: context) {
            formJsonEntity.json = formJson
            formJsonEntity.formId = formId
            form.json = formJsonEntity
            form.eventId = eventId
            form.archived = formJson[FormKey.archived.key] as? Bool ?? false
            form.formId = formId
            
            if let formFields = formJson[FormKey.fields.key] as? [[AnyHashable: Any]] {
                if let primaryMapFieldName = formJson[FormKey.primaryField.key] as? String {
                    form.primaryMapField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == primaryMapFieldName
                        }
                        return false
                    }
                }
                if let secondaryMapFieldName = formJson[FormKey.secondaryField.key] as? String {
                    form.secondaryMapField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == secondaryMapFieldName
                        }
                        return false
                    }
                }
                if let primaryFeedFieldName = formJson[FormKey.primaryFeedField.key] as? String {
                    form.primaryFeedField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == primaryFeedFieldName
                        }
                        return false
                    }
                }
                if let secondaryFeedFieldName = formJson[FormKey.secondaryFeedField.key] as? String {
                    form.secondaryFeedField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == secondaryFeedFieldName
                        }
                        return false
                    }
                }
            }
            
            return form
        }
        return nil
    }
    
    @discardableResult
    @objc public static func deleteAndRecreateForms(eventId: NSNumber, formsJson:[[AnyHashable: Any]], context: NSManagedObjectContext) -> [Form] {
        Form.deleteAllFormsForEvent(eventId: eventId, context: context)
        var forms: [Form] = []
        for formJson in formsJson {
            if let form = Form.createForm(eventId: eventId, formJson: formJson, context: context) {
                forms.append(form)
            }
        }
        return forms
    }
    
    @objc public var name: String? {
        get {
            return json?.json?[FormKey.name.key] as? String
        }
    }
    
    @objc public var formDescription: String? {
        get {
            return json?.json?[FormKey.description.key] as? String
        }
    }
    
    @objc public var fields: [[String: AnyHashable]]? {
        get {
            return json?.json?[FormKey.fields.key] as? [[String: AnyHashable]]
        }
    }
    
    public var min: Int? {
        get {
            return json?.json?[FormKey.min.key] as? Int
        }
    }
    
    public var max: Int? {
        get {
            return json?.json?[FormKey.max.key] as? Int
        }
    }
    
    public var isDefault: Bool {
        get {
            return json?.json?[FormKey.isDefault.key] as? Bool ?? false
        }
    }
    
    public var color: String? {
        get {
            return json?.json?[FormKey.color.key] as? String
        }
    }
    
    @objc public var style: [AnyHashable:Any]? {<commit_msg>fixed bad merge from master<commit_after>//
//  Form.m
//  mage-ios-sdk
//
//

import Foundation
import SSZipArchive
import CoreData

@objc public class Form: NSManagedObject {
    
    @objc public static let MAGEFormFetched = "mil.nga.giat.mage.form.fetched";
    
    @discardableResult
    @objc public static func deleteAllFormsForEvent(eventId: NSNumber, context: NSManagedObjectContext) -> Bool {
        return Form.mr_deleteAll(matching: NSPredicate(format: "eventId == %@", eventId), in: context)
    }
    
    @discardableResult
    @objc public static func createForm(eventId: NSNumber, formJson: [AnyHashable : Any], context: NSManagedObjectContext) -> Form? {
        if let formId = formJson[FormKey.id.key] as? NSNumber, let form = Form.mr_createEntity(in: context), let formJsonEntity = FormJson.mr_createEntity(in: context) {
            formJsonEntity.json = formJson
            formJsonEntity.formId = formId
            form.json = formJsonEntity
            form.eventId = eventId
            form.archived = formJson[FormKey.archived.key] as? Bool ?? false
            form.formId = formId
            
            if let formFields = formJson[FormKey.fields.key] as? [[AnyHashable: Any]] {
                if let primaryMapFieldName = formJson[FormKey.primaryField.key] as? String {
                    form.primaryMapField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == primaryMapFieldName
                        }
                        return false
                    }
                }
                if let secondaryMapFieldName = formJson[FormKey.secondaryField.key] as? String {
                    form.secondaryMapField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == secondaryMapFieldName
                        }
                        return false
                    }
                }
                if let primaryFeedFieldName = formJson[FormKey.primaryFeedField.key] as? String {
                    form.primaryFeedField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == primaryFeedFieldName
                        }
                        return false
                    }
                }
                if let secondaryFeedFieldName = formJson[FormKey.secondaryFeedField.key] as? String {
                    form.secondaryFeedField = formFields.first { field in
                        if let fieldName = field[FieldKey.name.key] as? String {
                            return fieldName == secondaryFeedFieldName
                        }
                        return false
                    }
                }
            }
            
            return form
        }
        return nil
    }
    
    @discardableResult
    @objc public static func deleteAndRecreateForms(eventId: NSNumber, formsJson:[[AnyHashable: Any]], context: NSManagedObjectContext) -> [Form] {
        Form.deleteAllFormsForEvent(eventId: eventId, context: context)
        var forms: [Form] = []
        for formJson in formsJson {
            if let form = Form.createForm(eventId: eventId, formJson: formJson, context: context) {
                forms.append(form)
            }
        }
        return forms
    }
    
    @objc public var name: String? {
        get {
            return json?.json?[FormKey.name.key] as? String
        }
    }
    
    @objc public var formDescription: String? {
        get {
            return json?.json?[FormKey.description.key] as? String
        }
    }
    
    @objc public var fields: [[String: AnyHashable]]? {
        get {
            return json?.json?[FormKey.fields.key] as? [[String: AnyHashable]]
        }
    }
    
    public var min: Int? {
        get {
            return json?.json?[FormKey.min.key] as? Int
        }
    }
    
    public var max: Int? {
        get {
            return json?.json?[FormKey.max.key] as? Int
        }
    }
    
    public var isDefault: Bool {
        get {
            return json?.json?[FormKey.isDefault.key] as? Bool ?? false
        }
    }
    
    @objc public var color: String? {
        get {
            return json?.json?[FormKey.color.key] as? String
        }
    }
    
    @objc public var style: [AnyHashable:Any]? {
{'fraction_non_alphanumeric': 0.08540648600621946, 'fraction_numerical': 0.0, 'mean_word_length': 1.5394811054709532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '778362', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2351, 'n_words': 571}