//
//  StringArrayTransformer.swift
//  Em-call
//
//  Created by Alex Sh on 17.04.2022.
//

import Foundation

/// A value transformer which transforms `UIColor` instances into data using `NSSecureCoding`.
@objc(StringArrayTransformer)
public final class StringArrayTransformer: ValueTransformer, NSCoding {
   // let coder:NSCoder
    public func encode(with coder: NSCoder) {
        
    }
    override init() {
          // self.value = value
           super.init()
       }   // public init?(coder: NSCoder) {
       // self.coder = coder
    //    print("fatal error: require coder")
   // }
    required public init?(coder aDecoder: NSCoder) {
          //  self.value = aDecoder.decodeInteger(forKey:"value")
        print("fatal error: require coder")
        }
    
    override public class func transformedValueClass() -> AnyClass {
          return NSData.self    }
      override public class func allowsReverseTransformation() -> Bool {
          return true    }
    
    override public func transformedValue(_ value: Any?) -> Any? {
           guard let stringArray = value as? [NSData] else {
          //  guard let image = value as? NSData else {
                return nil }
           do {
             //  let data = try NSKeyedArchiver.archivedData(withRootObject: image, requiringSecureCoding: true)
            let data = try NSKeyedArchiver.archivedData(withRootObject: stringArray, requiringSecureCoding: true)
               // let data = image
                return data
           } catch {
            assertionFailure("Failed to transform `NSArray` to `Data` ");     return nil      }
        }
        
        override public func reverseTransformedValue(_ value: Any?) -> Any? {
            guard let data = value as? NSData else {
                return nil }
          let result = try? NSKeyedUnarchiver.unarchivedObject( ofClass: NSArray.self, from: data as Data)
           // let result = try? NSKeyedUnarchiver.unarchivedObject( ofClass: NSData.self, from: data as Data)
           // let result = data
            return result
            // do {
               // let stringArray = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: (data as NSData) as Data)
               // let imgArray =  NSKeyedUnarchiver.unarchiveObject(with: data as Data) as? NSArray
               // return imgArray
           // } catch {      assertionFailure("Failed to transform `Data` to `NSArray` ")     return nil         }
        }
    
  // public static  let name = NSValueTransformerName(rawValue: String(describing: StringArrayTransformer.self))
}

extension StringArrayTransformer {
    /// The name of the transformer. This is the name used to register the transformer using `ValueTransformer.setValueTrandformer(_"forName:)`.
    public static  let name = NSValueTransformerName(rawValue: String(describing: StringArrayTransformer.self.classForCoder()))
   // public static var transformerName: NSValueTransformerName {
    //    let className = “\<#Root#>(StringArrayTransformer.self.classForCoder())”
    //        return NSValueTransformerName(“\(className)Transformer”) //
                  
       //  }
    /// Registers the value transformer with `ValueTransformer`.
public static func register() {
    
    let transformer = StringArrayTransformer()
    ValueTransformer.setValueTransformer(transformer, forName: StringArrayTransformer.name)
    }
}
