//
//  UITableView+Extension.swift
//  MyTatvaCare
//
//  Created by Nirav Ramani on 05/05/25.
//

import Foundation
import UIKit

extension UITableView {

    /// Register multiple UITableViewCell classes using their class names as reuse identifiers
    func register(_ cellClasses: [UITableViewCell.Type]) {
        for cellClass in cellClasses {
            self.register(cellClass, forCellReuseIdentifier: String(describing: cellClass))
        }
    }

    /// Register multiple nib-based UITableViewCell classes
    func registerNibs(_ cellClasses: [UITableViewCell.Type]) {
        for cellClass in cellClasses {
            let className = String(describing: cellClass)
            let nib = UINib(nibName: className, bundle: nil)
            self.register(nib, forCellReuseIdentifier: className)
        }
    }

    /// Dequeue a reusable cell of the expected type
    func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T {
        let identifier = String(describing: T.self)
        guard let cell = dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Error: Cell with identifier \(identifier) is not of type \(T.self)")
        }
        return cell
    }
}
