//
//  JWTDecoder.swift
//  BindID Example
//
//  Created by Transmit Security on 12/07/2021.
//

import Foundation

class JWTDecoder {
    
    public static let shared = JWTDecoder()
    
    private init() { } // Prevent creating new instances of this singleton
    
    public func decodePayload(_ jwt: String) throws -> [String: Any] {

        enum DecodeErrors: Error {
            case badToken
            case other
        }

        func base64Decode(_ base64: String) throws -> Data {
            let padded = base64.padding(toLength: ((base64.count + 3) / 4) * 4, withPad: "=", startingAt: 0)
            guard let decoded = Data(base64Encoded: padded) else {
                throw DecodeErrors.badToken
            }
            return decoded
        }

        func decodeJWTPart(_ value: String) throws -> [String: Any] {
            let bodyData = try base64Decode(value)
            let json = try JSONSerialization.jsonObject(with: bodyData, options: [])
            guard let payload = json as? [String: Any] else {
                throw DecodeErrors.other
            }
            return payload
        }

        let segments = jwt.components(separatedBy: ".")
        
        return try decodeJWTPart(segments[1])
    }
}

extension Double {
    var date: Date? {
        return Date(timeIntervalSince1970: self)
    }
}
