//
//  LKFErrorFilterSums.swift
//  lkfc
//
//  Created by Alexander Holzer on 22.02.26.
//  Copyright © 2026 Alexander Holzer. All rights reserved.
//

import Foundation
@_implementationOnly import LibLKF

public class LKFErrorFilterSums {

    var errors : [(String,String,String)]?
    
    public init (context: LKFContext, filter: UInt32 = 0xFFFFFFFF) {
        
        guard let ptr = context.ptr else {
            return
        }
        
        var count = LibLKF.lkf_setErrorCountsFilter(context.ptr, filter)
        
        if (count == LKF_OUTOFMEMORY_ERROR) {
            return
        }
        
        let lineBufferCount = UnsafeMutablePointer<CChar>.allocate(capacity:10)
        let lineBufferCode = UnsafeMutablePointer<CChar>.allocate(capacity:6)
        let lineBufferText = UnsafeMutablePointer<CChar>.allocate(capacity:256)
        
        var errors : [(String,String,String)] = []
        
        for i in 0..<count {
            LibLKF.lkf_getSatzartErrorSum_Field(ptr, i, 2, lineBufferCount, 10)
            LibLKF.lkf_getSatzartErrorSum_Field(ptr, i, 0, lineBufferCode, 6)
            LibLKF.lkf_getSatzartErrorSum_Field(ptr, i, 1, lineBufferText, 256)
            errors.append((String(cString: lineBufferCode), String(cString: lineBufferCount), String(cString: lineBufferText)))
        }
        
        lineBufferCount.deallocate()
        lineBufferCode.deallocate()
        lineBufferText.deallocate()

        self.errors = errors
    }
    
}
