import * as _codemirror_state from '@codemirror/state';
import { Extension, Facet, EditorState, StateField } from '@codemirror/state';
import { ViewPlugin, DecorationSet, ViewUpdate, EditorView, Decoration, WidgetType } from '@codemirror/view';
import { Diagnostic } from '@codemirror/lint';
import { CompletionContext, CompletionResult } from '@codemirror/autocomplete';
import { ScriptBlock, ICodeStatement, IMetric } from '@bitcobblers/wod-wiki-core';
import { QueryOptions, QueryResult, ParsedFindQuery, FindOptions, FindQueryResult, ParsedRowsQuery, RowsQueryResult, ParsedQuery } from '@bitcobblers/wod-wiki-wql';
import * as react from 'react';

declare function editorTheme(isDark: boolean): Extension;

/**
 * Extension that adds ID attributes to line elements in the editor.
 */
declare const lineIdsExtension: ViewPlugin<{
    decorations: DecorationSet;
    update(update: ViewUpdate): void;
    getDecorations(view: EditorView): _codemirror_state.RangeSet<Decoration>;
}, undefined>;

type NavigationHook = (url: string, view: EditorView) => void;
declare const navigationFacet: Facet<NavigationHook, NavigationHook | null>;
declare function urlAtPos(view: EditorView, pos: number): string | null;
declare const linkOpen: Extension;

declare function findWorkoutErrors(content: string, offsetInDoc: number): Diagnostic[];
declare function workoutLintSource(view: EditorView): Diagnostic[];
declare const wodLinter: _codemirror_state.Extension;

declare function fenceCompletion(context: CompletionContext): CompletionResult | null;
declare function wrapInTimeFence(view: EditorView): boolean;
declare function handleFenceAutoWrap(event: InputEvent, view: EditorView): boolean;
declare const wodAutoWrap: Extension;
declare const wodEditorKeymap: Extension;
declare const wodAutocompletion: Extension;

type EditorDialect = "time" | "log";
type EditorSectionType = "markdown" | "time" | "log" | "frontmatter" | "code" | "widget" | "embed" | "query";
type EditorSectionSubtype = "heading" | "paragraph" | "list" | "blockquote" | "table" | "unknown";
type EmbedType = "image" | "link" | "youtube";
interface EditorSection {
    id: string;
    type: EditorSectionType;
    subtype?: EditorSectionSubtype;
    from: number;
    to: number;
    startLine: number;
    endLine: number;
    contentFrom?: number;
    contentTo?: number;
    sport?: string;
    widgetName?: string;
    contentId?: string;
    queryType?: string;
    queryError?: string;
    embed?: {
        type: EmbedType;
        url: string;
        label: string;
        videoId?: string;
        height?: number;
    };
}
interface SectionState {
    sections: EditorSection[];
}
declare const forceSectionParse: _codemirror_state.StateEffectType<null>;
declare function blockContentId(content: string): string;
declare const sectionField: StateField<SectionState>;
declare function sectionAtPos(state: EditorState, pos: number): EditorSection | null;
declare function activeCursorSection(state: EditorState): EditorSection | null;

interface SectionRect {
    sectionId: string;
    top: number;
    height: number;
    type: EditorSection["type"];
    subtype?: EditorSection["subtype"];
    widgetName?: string;
}
type GeometryListener = (rects: SectionRect[], docVersion: number) => void;
declare class SectionGeometryPlugin {
    private view;
    rects: SectionRect[];
    docVersion: number;
    private listeners;
    constructor(view: EditorView);
    update(update: ViewUpdate): void;
    addListener(fn: GeometryListener): () => void;
    private measure;
    private notify;
    destroy(): void;
}
declare const sectionGeometry: ViewPlugin<SectionGeometryPlugin, undefined>;

declare const previewDecorations: Extension;

declare const embedPreviewDecorations: Extension;

declare class DefaultFrontmatterWidget extends WidgetType {
    readonly props: Record<string, string>;
    constructor(props: Record<string, string>);
    eq(other: DefaultFrontmatterWidget): boolean;
    toDOM(): HTMLElement;
}
declare const frontmatterPreviewField: StateField<DecorationSet>;
declare const frontmatterPreview: Extension;

declare function markdownSyntaxHiding(): Extension;

declare const markdownTablePreview: Extension;

declare const setGutterHighlights: _codemirror_state.StateEffectType<Set<number>>;
declare const gutterHighlightsField: StateField<Set<number>>;
declare const gutterUnified: (_codemirror_state.Extension | StateField<Set<number>>)[];
declare function dispatchGutterHighlights(view: EditorView, lineNumbers: number[]): void;

type ButtonAction = (action: string, params: Record<string, string>) => void;
declare function inlineButtonDecoration(onAction: ButtonAction): Extension;

interface RuntimePanelEntry {
    sectionId: string;
    block: ScriptBlock;
    afterPos: number;
    expanded?: boolean;
}
declare const addRuntimePanel: _codemirror_state.StateEffectType<RuntimePanelEntry>;
declare const removeRuntimePanel: _codemirror_state.StateEffectType<{
    sectionId: string;
}>;
declare const expandRuntimePanel: _codemirror_state.StateEffectType<{
    sectionId: string;
    expanded: boolean;
}>;
declare const runtimePanelField: StateField<Map<string, RuntimePanelEntry>>;
declare function dispatchAddRuntimePanel(view: EditorView, sectionId: string, block: ScriptBlock, afterPos: number, expanded?: boolean): void;
declare function dispatchRemoveRuntimePanel(view: EditorView, sectionId: string): void;
declare function dispatchExpandRuntimePanel(view: EditorView, sectionId: string, expanded: boolean): void;

declare function sessionQueryWql(resultId: string): string;
declare function sessionQueryInsert(state: EditorState, blockId: string, resultId: string, runBlock?: {
    id?: string;
    contentId?: string;
}): {
    from: number;
    insert: string;
} | null;

declare const smartIncrement: Extension;

interface OverlayAction {
    label: string;
    icon: string;
    action: (view: EditorView, section: EditorSection) => void;
}
declare function configureOverlayActions(actions: OverlayAction[]): void;
declare function getOverlayActions(): OverlayAction[];
declare const wodOverlayPanel: Extension;

interface CursorFocusState {
    sectionId: string;
    section: EditorSection;
    lineFrom: number;
    docLine: number;
    statementIndex: number;
    statement: ICodeStatement;
    focusedMetric: IMetric | null;
    focusedMetricIndex: number;
    allStatements: ICodeStatement[];
}
declare function getCursorFocusState(state: EditorState): CursorFocusState | null;
declare const cursorFocusExtension: Extension;

interface QueryExecutor {
    runQuery(query: string, options?: QueryOptions): Promise<QueryResult>;
    runFind(parsed: ParsedFindQuery, options?: FindOptions): Promise<FindQueryResult>;
    runRows(parsed: ParsedRowsQuery, options?: {
        anchorNow?: number;
    }): Promise<RowsQueryResult>;
    run?(parsed: ParsedQuery, options?: QueryOptions): Promise<QueryResult>;
}

interface QueryBlockPreviewOptions {
    executor?: QueryExecutor;
    onResultSaved?: (callback: () => void) => (() => void) | void;
    readOnly?: boolean;
    onSaveQuery?: (sectionId: string, nextQuery: string) => void;
}
declare function saveBlockQuerySource(view: EditorView, sectionId: string, newQuery: string, queryIndex?: number): {
    ok: boolean;
    patchedContent?: string;
    message?: string;
};
declare function findQueryBlockSection(view: EditorView, sectionId: string): EditorSection | null;
declare function queryBlockPreview(options?: QueryBlockPreviewOptions): Extension;

/** Parsed JSON object passed into a widget component. */
type WidgetConfig = Record<string, unknown>;
/** Props passed to every registered widget component. */
interface WidgetProps {
    /** Parsed JSON object from the fenced widget body. */
    config: WidgetConfig;
    /** Exact source text between the opening and closing fences. */
    rawContent: string;
    /** Stable section identifier generated by section-state.ts. */
    sectionId: string;
}
/** Registry of widget names to React components. */
type WidgetRegistry = Map<string, react.ComponentType<WidgetProps>>;
/** Effect to toggle edit mode for a specific widget block */
declare const toggleWidgetEdit: _codemirror_state.StateEffectType<{
    sectionId: string;
    editing: boolean;
}>;
/** StateField to track which widgets are currently being edited as raw text */
declare const editingWidgetsField: StateField<Set<string>>;
/**
 * Create a CM6 Extension that replaces widget fenced blocks with React components.
 * Block decorations MUST come from a StateField (not a ViewPlugin) per CM6 constraints.
 */
declare function widgetBlockPreview(registry: WidgetRegistry): Extension;

interface EditorPresetOptions {
    dialect?: 'wod' | 'whiteboard' | 'wql' | 'markdown' | string;
    isDark?: boolean;
    readOnly?: boolean;
    lineNumbers?: boolean;
    lineWrapping?: boolean;
    executor?: QueryExecutor;
    onResultSaved?: (callback: () => void) => (() => void) | void;
    extensions?: Extension[];
}
declare function editorPreset(optionsOrDialect?: string | EditorPresetOptions): Extension[];

export { removeRuntimePanel as $, forceSectionParse as A, type ButtonAction as B, type CursorFocusState as C, DefaultFrontmatterWidget as D, type EditorDialect as E, frontmatterPreview as F, type GeometryListener as G, frontmatterPreviewField as H, getCursorFocusState as I, getOverlayActions as J, gutterHighlightsField as K, gutterUnified as L, handleFenceAutoWrap as M, type NavigationHook as N, type OverlayAction as O, inlineButtonDecoration as P, type QueryExecutor as Q, type RuntimePanelEntry as R, type SectionRect as S, lineIdsExtension as T, linkOpen as U, markdownSyntaxHiding as V, type WidgetConfig as W, markdownTablePreview as X, navigationFacet as Y, previewDecorations as Z, queryBlockPreview as _, type EditorPresetOptions as a, runtimePanelField as a0, saveBlockQuerySource as a1, sectionAtPos as a2, sectionField as a3, sectionGeometry as a4, sessionQueryInsert as a5, sessionQueryWql as a6, setGutterHighlights as a7, smartIncrement as a8, toggleWidgetEdit as a9, urlAtPos as aa, widgetBlockPreview as ab, wodAutoWrap as ac, wodAutocompletion as ad, wodEditorKeymap as ae, wodLinter as af, wodOverlayPanel as ag, workoutLintSource as ah, wrapInTimeFence as ai, type EditorSection as b, type EditorSectionSubtype as c, type EditorSectionType as d, type EmbedType as e, type QueryBlockPreviewOptions as f, type SectionState as g, type WidgetProps as h, type WidgetRegistry as i, activeCursorSection as j, addRuntimePanel as k, blockContentId as l, configureOverlayActions as m, cursorFocusExtension as n, dispatchAddRuntimePanel as o, dispatchExpandRuntimePanel as p, dispatchGutterHighlights as q, dispatchRemoveRuntimePanel as r, editingWidgetsField as s, editorPreset as t, editorTheme as u, embedPreviewDecorations as v, expandRuntimePanel as w, fenceCompletion as x, findQueryBlockSection as y, findWorkoutErrors as z };
