import SwiftUI

struct IconSource: Codable {
    let uri: String
}

func loadIconSources() -> [String: IconSource] {
    guard let url = Bundle.main.url(forResource: "icon", withExtension: "json") else {
        print("Error: icon.json not found")
        return [:]
    }
    
    do {
        let data = try Data(contentsOf: url)
        let decoded = try JSONDecoder().decode([String: IconSource].self, from: data)
        return decoded
    } catch {
        print("Error decoding icon.json: \(error)")
        return [:]
    }
}

// Load the JSON data into a global variable
let iconSources: [String: IconSource] = loadIconSources()

// Icon ViewModel
public struct Icon: View {
    var source: String
    var size: CGFloat
    var color: Color?
    var accessibilityLabel: String?
    
    public init(
            source: String,
            size: CGFloat = 24,
            color: Color? = nil,
            accessibilityLabel: String? = nil
        ) {
            self.source = source
            self.size = size
            self.color = color
            self.accessibilityLabel = accessibilityLabel
        }

    public var body: some View {
        if let uri = iconSources[source]?.uri {
            ImageView(uri, color: color).frame(width: size, height: size)
        }
    }
}

