import { Store } from "./_store.js";
import { HotkeyConflict, HotkeyConflictOptions } from "./conflicts.js";
import { Hotkey, HotkeyChord, HotkeySequence, MaybeGetter, ParsedHotkeyChord } from "./hotkey.types.js";
import { HotkeyPlatform } from "./platform.js";
/**
 * How the recorder writes the pressed key:
 *
 * - `'auto'` (default): by what the press typed, written as by hand, such as
 *   `Mod+S`, `Shift+Enter`, `?` for Shift+/ and `@` for German AltGr+Q. The
 *   result compares equal to a hotkey written by hand, and its label follows
 *   the user's keyboard layout. With Control, Command or Alt, a digit-row key
 *   records its digit (AZERTY Ctrl+& is `Mod+1`). Recorded by code: macOS
 *   Option with a symbol, such as `Alt+[Slash]`, dead keys, the numpad digits
 *   and decimal key, and Shift with a numpad operator.
 * - `'code'`: always by physical code, such as `Mod+[KeyS]`. The hotkey names
 *   a position on the keyboard, whatever that key types.
 * - `'key'`: always by the character or key name the press produced, such as
 *   `Alt+ß` for macOS Option+S.
 */
export type RecorderKeyMode = 'auto' | 'code' | 'key';
/**
 * Why the recorder rejected a key press or a recording:
 *
 * - `'alt-graph'`: AltGr was held and typed nothing the mode can record:
 *   recording by code rejects every AltGr press, `'auto'` rejects a dead key,
 *   and both `'auto'` and `'key'` reject AltGr that typed nothing new, such
 *   as AltGr+Home on Linux, because no hotkey matches that press. On
 *   Windows, AltGr with a key it doesn't change counts as Control+Alt, which
 *   no mode rejects: AltGr+Home records `Mod+Alt+Home`, or `Mod+Alt+[Home]` by code.
 * - `'missing-code'`: the event has no usable physical key code. Recording by
 *   code always needs one. `recordBy: 'auto'` records what the press typed
 *   instead, and rejects only a press that typed nothing it can record.
 * - `'whitespace'`: the key is a whitespace character, which a hotkey can't
 *   hold.
 * - `'validation'`: your `validate` callback rejected the recording.
 * - `'conflict'`: the recording conflicts with registered hotkeys.
 */
export type RecorderRejectionReason = 'alt-graph' | 'missing-code' | 'whitespace' | 'validation' | 'conflict';
export interface RecorderRejection {
  reason: RecorderRejectionReason;
  /** The message your `validate` callback returned, when it returned one. */
  message?: string;
  /** The rejected chord or sequence, when one was recorded. */
  hotkey?: Hotkey;
  conflicts?: Array<HotkeyConflict>;
}
export interface RecorderOptions {
  /** How the pressed key is written. Default: `'auto'`. */
  recordBy?: RecorderKeyMode;
  /** Platform used for Mod and AltGraph handling. Defaults to platform detection. */
  platform?: HotkeyPlatform;
  /** Opt in to live-registry conflicts, including sequence prefixes. Default: false. */
  detectConflicts?: boolean | HotkeyConflictOptions;
  /** Rejected candidates leave recording active. */
  onReject?: (rejection: RecorderRejection) => void;
}
/** Validation context for a recorded chord. */
export interface HotkeyChordRecorderValidationContext {
  kind: 'chord';
  /** The event that recorded the chord. */
  event: KeyboardEvent;
  /** The chord with `Mod` resolved for the recorder's platform. */
  parsedChord: ParsedHotkeyChord;
  /**
   * The platform the chord was recorded on. `Control` recorded on macOS and
   * `Meta` recorded elsewhere mean other keys on the other platforms, so
   * store it with the hotkey if users sync bindings across machines.
   */
  platform: HotkeyPlatform;
}
/** Validation context for a recorded sequence. */
export interface HotkeySequenceRecorderValidationContext {
  kind: 'sequence';
  /** The event that recorded each step. */
  events: ReadonlyArray<KeyboardEvent>;
  /** Each step with `Mod` resolved for the recorder's platform. */
  parsedSequence: Array<ParsedHotkeyChord>;
  /** The platform the sequence was recorded on. */
  platform: HotkeyPlatform;
}
/**
 * Validation context for a recorded chord or sequence. Narrow with `kind` to
 * read `parsedChord` or `parsedSequence`.
 */
export type HotkeyRecorderValidationContext = HotkeyChordRecorderValidationContext | HotkeySequenceRecorderValidationContext;
/**
 * State interface for the HotkeyRecorder. Adapters narrow `THotkey` to the
 * kind their recorder records.
 */
export interface HotkeyRecorderState<THotkey extends Hotkey = Hotkey> {
  /** Whether recording is currently active */
  isRecording: boolean;
  /** Chords recorded so far in a multi-step recording (empty for `maxSteps: 1`) */
  steps: HotkeySequence;
  /** The last committed hotkey: a chord, or a sequence of two or more steps */
  recordedHotkey: THotkey | null;
}
/** The recorder controls that framework adapters return. */
export interface HotkeyRecorderActions {
  /** Start recording a new hotkey */
  startRecording: () => void;
  /** Stop recording without calling onCancel */
  stopRecording: () => void;
  /** Cancel recording and call onCancel */
  cancelRecording: () => void;
  /** Commit the recorded steps now (multi-step recordings) */
  commitRecording: () => void;
}
/** Which keys commit a multi-step recording before it reaches `maxSteps`. */
export type HotkeyRecorderCommitKeys = 'enter' | 'none';
interface HotkeyRecorderBaseOptions extends RecorderOptions {
  /**
   * Optional callback when recording is cancelled: Escape, Tab without
   * `recordTab`, or the window losing focus with `cancelOnBlur`
   */
  onCancel?: () => void;
  /**
   * Whether to cancel recording when the window loses focus, as when the user
   * switches apps or tabs, opens browser devtools, or clicks into an iframe.
   * Keys pressed elsewhere never reach the page. Focus moving to another
   * element on the page doesn't cancel. Default: true
   */
  cancelOnBlur?: boolean;
  /**
   * Optional callback when the recording is cleared: Backspace/Delete with no
   * modifiers and no recorded steps
   */
  onClear?: () => void;
  /**
   * Whether to ignore keyboard events from input-like elements (text inputs,
   * textarea, select, contenteditable) while recording. When true, typing in
   * inputs passes through normally (Escape still cancels). Defaults to true.
   */
  ignoreInputs?: boolean;
  /**
   * Whether to record Tab and Shift+Tab. By default they cancel recording and
   * move focus, so keyboard users can leave the recorder. Tab with Control,
   * Alt, or Meta isn't affected. When true, tell users that Escape cancels.
   * Default: false
   */
  recordTab?: boolean;
}
/** Options for recording a single chord. This is the default (`maxSteps: 1`). */
export interface HotkeyChordRecorderOptions extends HotkeyRecorderBaseOptions {
  /** Maximum number of chords to record. Default: 1 */
  maxSteps?: 1;
  /** Return true to accept, or false or a message to reject while staying in recording mode. The message is passed to `onReject`. */
  validate?: (hotkey: HotkeyChord, context: HotkeyChordRecorderValidationContext) => boolean | string;
  /**
   * Callback when a chord is successfully recorded. `context` is the one
   * `validate` received, with the key press that recorded the chord.
   */
  onRecord: (hotkey: HotkeyChord, context: HotkeyChordRecorderValidationContext) => void;
}
/**
 * Options for recording up to `maxSteps` chords. A one-step recording commits
 * as a chord string; two or more steps commit as a sequence.
 */
export interface HotkeySequenceRecorderOptions extends HotkeyRecorderBaseOptions {
  /** Maximum number of chords to record. Reaching it commits automatically. Use `Infinity` for no limit. */
  maxSteps: number;
  /** Return true to accept, or false or a message to reject while staying in recording mode. The message is passed to `onReject`. */
  validate?: (hotkey: Hotkey, context: HotkeyRecorderValidationContext) => boolean | string;
  /**
   * Callback when a hotkey is successfully recorded. `context` is the one
   * `validate` received, with the key press that recorded each step.
   */
  onRecord: (hotkey: Hotkey, context: HotkeyRecorderValidationContext) => void;
  /** Whether plain Enter commits the recorded steps. Default: 'enter' */
  commitKeys?: HotkeyRecorderCommitKeys;
  /** Commit automatically after this many milliseconds without a new step. Disabled when unset or 0. */
  idleTimeoutMs?: number;
}
/**
 * Options for HotkeyRecorder. `maxSteps` (default 1) selects between recording
 * a single chord and recording a sequence.
 */
export type HotkeyRecorderOptions = HotkeyChordRecorderOptions | HotkeySequenceRecorderOptions;
/**
 * Framework-agnostic class for recording keyboard shortcuts.
 *
 * With the default `maxSteps: 1`, the first valid chord commits. With a larger
 * `maxSteps`, each chord adds a step; the recording commits on Enter (unless
 * `commitKeys: 'none'`), after `idleTimeoutMs`, or when it reaches `maxSteps`.
 * Backspace/Delete removes the last step, and on an empty recording calls
 * `onClear`. Escape cancels. Tab and Shift+Tab cancel and move focus unless
 * `recordTab` is true. The window losing focus cancels unless `cancelOnBlur`
 * is false. A one-step result is a chord string.
 *
 * State Management:
 * - Uses TanStack Store for reactive state management
 * - State can be accessed via `recorder.store.state` when using the class directly
 * - When using framework adapters, use the `useHotkeyRecorder` hook for reactive state
 *
 * @example
 * ```ts
 * const recorder = new HotkeyRecorder({
 *   onRecord: (hotkey) => {
 *     console.log('Recorded:', hotkey) // 'Mod+S'
 *   },
 * })
 *
 * const sequenceRecorder = new HotkeyRecorder({
 *   maxSteps: Infinity,
 *   onRecord: (hotkey) => {
 *     console.log('Recorded:', hotkey) // 'Mod+S' or ['G', 'H']
 *   },
 * })
 *
 * recorder.start()
 * // ... later
 * recorder.destroy()
 * ```
 */
export declare class HotkeyRecorder {
  #private;
  readonly store: Store<HotkeyRecorderState>;
  constructor(options: MaybeGetter<HotkeyChordRecorderOptions>);
  constructor(options: MaybeGetter<HotkeySequenceRecorderOptions>);
  /**
   * Updates the recorder options, including callbacks.
   * This allows framework adapters to sync callback changes without recreating the recorder.
   */
  setOptions(options: Partial<HotkeyRecorderOptions>): void;
  /**
   * Start recording a new hotkey.
   *
   * Sets up a keydown event listener that captures keyboard events
   * and records chords according to `maxSteps`.
   */
  start(): void;
  /**
   * Commits the recorded steps after validation and conflict checks.
   * Called automatically at `maxSteps`, on Enter, or after `idleTimeoutMs`.
   * A rejected commit leaves recording active with its steps intact.
   */
  commit(): void;
  /**
   * Stop recording (same as cancel, but doesn't call onCancel).
   *
   * Removes the event listener and resets the recording state.
   */
  stop(): void;
  /**
   * Cancel recording without saving.
   *
   * Removes the event listener, resets the recording state, and calls
   * the onCancel callback if provided.
   */
  cancel(): void;
  /**
   * Clean up event listeners and reset state.
   *
   * Call this when you're done with the recorder to ensure
   * all event listeners are properly removed.
   */
  destroy(): void;
}
export {};