function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { Path, Transforms } from "slate";
import { ReactEditor, useSlate } from "slate-react";
import { useRefFunction } from "../../Hooks/useRefFunction";
import { selChange$ } from "../editor/plugins/useOnchange";
import { useEditorStore } from "../editor/store";
import { useGetSetState } from "../editor/utils";
import { EditorUtils } from "../editor/utils/editorUtils";
import { useSubject } from "./subscribe";
/**
 * 自定义钩子 `useMEditor` 用于管理 Slate 编辑器中的节点更新和删除操作。
 *
 * @param el - 基础元素，用于确定操作的目标节点。
 * @returns 包含编辑器实例、更新函数和删除函数的元组。
 *
 * @example
 * ```typescript
 * const [editor, update, remove] = useMEditor(element);
 *
 * // 更新节点属性
 * update({ key: 'value' }, currentElement);
 *
 * // 删除节点
 * remove(currentElement);
 * ```
 *
 * @function
 * @name useMEditor
 * @param {BaseElement} el - 基础元素，用于确定操作的目标节点。
 * @returns {[Editor, (props: Record<string, any>, current?: BaseElement) => void, (current?: BaseElement) => void]}
 *          包含编辑器实例、更新函数和删除函数的元组。
 */ export var useMEditor = function useMEditor(el) {
    var editor = useSlate();
    var update = useRefFunction(function(props, current) {
        Transforms.setNodes(editor, props, {
            at: ReactEditor.findPath(editor, current || el)
        });
    });
    return [
        editor,
        update
    ];
};
/**
 * 自定义 Hook，用于获取编辑器中元素的选中状态和路径。
 *
 * @param element - 需要检查的元素。
 * @returns 一个包含选中状态、路径和编辑器存储的数组。
 *
 * @remarks
 * 该 Hook 使用 `useEditorStore` 获取编辑器存储，并使用 `useGetSetState` 管理组件状态。
 * 它通过 `useSubject` 订阅 `selChange$` 主题，以响应选中状态的变化。
 *
 * @example
 * ```typescript
 * const [isSelected, elementPath, editorStore] = useSelStatus(someElement);
 * ```
 */ export var useSelStatus = function useSelStatus(element) {
    var editor = useSlate();
    var _useEditorStore = useEditorStore(), store = _useEditorStore.store, markdownEditorRef = _useEditorStore.markdownEditorRef;
    var _useGetSetState = _sliced_to_array(useGetSetState({
        selected: ReactEditor.isFocused(editor),
        path: EditorUtils.findPath(editor, element)
    }), 2), state = _useGetSetState[0], setState = _useGetSetState[1];
    useSubject(selChange$, function(ctx) {
        var _ctx_node;
        var path = EditorUtils.findPath(markdownEditorRef.current, element);
        if (!ctx) {
            return setState({
                selected: false,
                path: path
            });
        }
        setState({
            path: path,
            selected: Path.equals(path, ((_ctx_node = ctx.node) === null || _ctx_node === void 0 ? void 0 : _ctx_node[1]) || [])
        });
    }, [
        element
    ]);
    return [
        state().selected,
        state().path,
        store
    ];
};
