import Foundation

public enum EditorActiveLineTracker {
    public static func lines(from source: String, activeLineIndex: Int) -> [EditorLine] {
        DocumentLineIndex(source: source).editorLines(activeLineIndex: activeLineIndex)
    }

    public static func lines(from lineIndex: DocumentLineIndex, activeLineIndex: Int) -> [EditorLine] {
        lineIndex.editorLines(activeLineIndex: activeLineIndex)
    }

    public static func lineIndex(containing location: Int, in source: String) -> Int {
        DocumentLineIndex(source: source).lineIndex(containing: location)
    }

    public static func lineIndex(containing location: Int, in lineIndex: DocumentLineIndex) -> Int {
        lineIndex.lineIndex(containing: location)
    }

    public static func clampedSelection(_ selection: EditorSelection, in source: String) -> EditorSelection {
        let sourceLength = source.utf16.count
        let location = max(0, min(selection.location, sourceLength))
        let length = max(0, min(selection.length, sourceLength - location))
        return EditorSelection(location: location, length: length)
    }
}
