function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
import { useCallback, useEffect, useImperativeHandle, useRef } from "react";
import { ReactEditor } from "slate-react";
/**
 * Refs 管理 Hook
 * 管理组件中所有的 refs 和相关逻辑
 */ export var useMarkdownInputFieldRefs = function useMarkdownInputFieldRefs(props) {
    var markdownEditorRef = useRef();
    var quickActionsRef = useRef(null);
    var actionsRef = useRef(null);
    var isSendingRef = useRef(false);
    /**
   * Tracks the last value emitted by the editor's own onChange callback.
   * When props.value matches this ref we know the update originated from the
   * editor itself and there is no need to call setMDContent — doing so would
   * replace the live Slate document while the user is actively typing, causing
   * ReactEditor.deselect() to throw "Failed to execute 'collapseToEnd' on
   * 'Selection': There is no selection", which crashes the component tree.
   */ var lastEditorValueRef = useRef(undefined);
    /** Called by MarkdownInputField's onChange handler to record what the editor just emitted. */ var onEditorChange = useCallback(function(value) {
        lastEditorValueRef.current = value;
    }, []);
    // 同步外部 value 到编辑器 — 只在 value 来自外部（非编辑器自身输入）时写回
    useEffect(function() {
        var _props_value;
        var _markdownEditorRef_current_markdownEditorRef, _markdownEditorRef_current, _markdownEditorRef_current_store, _markdownEditorRef_current1;
        if (!markdownEditorRef.current) return;
        // Primary guard: this value was just produced by the editor itself —
        // the Slate document is already correct, no write-back needed.
        if (props.value === lastEditorValueRef.current) return;
        // Secondary guard: the editor is focused, meaning the user is actively
        // typing.  A stale props.value (delayed by debounce + React batching) could
        // arrive *after* the editor has moved on to a newer character.  Calling
        // setMDContent in that window replaces the live document and triggers
        // ReactEditor.deselect() on a focused editor → InvalidStateError → white
        // screen.  Skip the write; _safeDeselect in store.ts also defends here as
        // a last resort, but not calling setMDContent at all is the clean fix.
        var slateEditor = (_markdownEditorRef_current = markdownEditorRef.current) === null || _markdownEditorRef_current === void 0 ? void 0 : (_markdownEditorRef_current_markdownEditorRef = _markdownEditorRef_current.markdownEditorRef) === null || _markdownEditorRef_current_markdownEditorRef === void 0 ? void 0 : _markdownEditorRef_current_markdownEditorRef.current;
        if (slateEditor) {
            try {
                if (ReactEditor.isFocused(slateEditor)) return;
            } catch (unused) {
            // ReactEditor.isFocused can throw if the editor is being torn down
            }
        }
        (_markdownEditorRef_current1 = markdownEditorRef.current) === null || _markdownEditorRef_current1 === void 0 ? void 0 : (_markdownEditorRef_current_store = _markdownEditorRef_current1.store) === null || _markdownEditorRef_current_store === void 0 ? void 0 : _markdownEditorRef_current_store.setMDContent((_props_value = props.value) !== null && _props_value !== void 0 ? _props_value : '');
    }, [
        props.value
    ]);
    // 通过 ref 暴露编辑器实例，包装 store.setMDContent 以同步 value 状态，确保发送按钮正确响应
    // 使用 Proxy 包装 store，仅覆盖 setMDContent，保留 getMDContent、clearContent、focus 等全部方法
    useImperativeHandle(props.inputRef, function() {
        var editor = markdownEditorRef.current;
        var syncValueAndSetMDContent = function syncValueAndSetMDContent(md, plugins, options) {
            var _editor_store;
            if (md !== undefined) {
                props.setValue(md);
            }
            return editor === null || editor === void 0 ? void 0 : (_editor_store = editor.store) === null || _editor_store === void 0 ? void 0 : _editor_store.setMDContent(md, plugins, options);
        };
        if (!editor) {
            return {
                store: {
                    setMDContent: syncValueAndSetMDContent
                }
            };
        }
        var storeProxy = new Proxy(editor.store, {
            get: function get(target, prop) {
                if (prop === 'setMDContent') {
                    return syncValueAndSetMDContent;
                }
                return Reflect.get(target, prop);
            }
        });
        return _object_spread_props(_object_spread({}, editor), {
            store: storeProxy
        });
    }, [
        props.setValue
    ]);
    return {
        markdownEditorRef: markdownEditorRef,
        quickActionsRef: quickActionsRef,
        actionsRef: actionsRef,
        isSendingRef: isSendingRef,
        onEditorChange: onEditorChange
    };
};
