//
//  TSBaseModel.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/22.
//

import ObjectMapper

class TSBaseModel: NSObject, Mappable, NSCopying {
    static func model(data: Data) -> TSBaseModel? {
        do {
            let object = try JSONSerialization.jsonObject(with: data)
            if let dict = object as? [String: AnyHashable] {
                return Self.init(json: dict)
            }
        } catch _ {
            return nil
        }
        return nil
    }
    
    var data: Data? {
        let json = toJSON()
        do {
            let data = try JSONSerialization.data(withJSONObject: json)
            return data
        } catch _ {
            return nil
        }
    }
    
    required convenience init?(map: ObjectMapper.Map) {
        self.init()
        mapping(map: map)
    }
    
    required convenience init?(json: [String: Any]) {
        self.init(JSON: json)
    }
    
    func mapping(map: ObjectMapper.Map) {}
    
    func copy(with zone: NSZone? = nil) -> Any {
        let json = toJSON()
        return Self.init(JSON: json) ?? Self.init(JSON: [:])!
    }
    
//    deinit {
//        debugPrint("♻️♻️♻️ TGRootViewController -> \(type(of: self)) deinit ♻️♻️♻️")
//    }
}
