//
//  ApiErrorModel.swift
//  CheckoutSDK-iOS
//
//  Created by Osama Rabie on 6/14/21.
//  Copyright © 2021 Tap Payments. All rights reserved.
//

import Foundation
import CommonDataModelsKit_iOS
/// Model to parse errors coming from the backend
internal struct ApiErrorModel {
    
    // MARK: - Internal -
    // MARK: Properties
    /// Determines if there are errors from the backend if any
    internal let errors:[ErrorDetail]
    
    // MARK: - Private -
    
    private enum CodingKeys: String, CodingKey {
        case errors = "errors"
    }
}

// MARK: - Decodable
extension ApiErrorModel: Decodable {
    
    internal init(from decoder: Decoder) throws {
        
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let errors    = try container.decodeIfPresent([ErrorDetail].self, forKey: .errors) ?? []
        
        self.init(errors: errors)
    }
}
