import AppKit
import Carbon.HIToolbox
import SwiftTerm

enum TerminalCommandArrowBehavior {
    private static let relevantModifiers: NSEvent.ModifierFlags = [.command, .control, .option, .shift]
    private static let lineKill: [UInt8] = [0x15]
    private static let clearScreen: [UInt8] = [0x0c]

    static func sequence(
        for modifierFlags: NSEvent.ModifierFlags,
        keyCode: UInt16,
        applicationCursor: Bool
    ) -> [UInt8]? {
        let flags = modifierFlags.intersection(relevantModifiers)
        guard flags == [.command] else { return nil }

        switch Int(keyCode) {
        case kVK_LeftArrow:
            return applicationCursor ? EscapeSequences.moveHomeApp : EscapeSequences.moveHomeNormal
        case kVK_RightArrow:
            return applicationCursor ? EscapeSequences.moveEndApp : EscapeSequences.moveEndNormal
        case kVK_Delete:
            return lineKill
        case kVK_ANSI_L:
            return clearScreen
        default:
            return nil
        }
    }
}
