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);
}
/**
 * @fileoverview 代码编辑器状态管理Hook
 * 统一管理代码编辑器的状态逻辑
 */ import { useCallback, useEffect } from "react";
import { useGetSetState } from "react-use";
import { Transforms } from "slate";
import { ReactEditor } from "slate-react";
import { useEditorStore } from "../../../MarkdownEditor/editor/store";
import { useSelStatus } from "../../../MarkdownEditor/hooks/editor";
export function useCodeEditorState(element) {
    var _useEditorStore = useEditorStore(), store = _useEditorStore.store, markdownEditorRef = _useEditorStore.markdownEditorRef;
    var _useSelStatus = _sliced_to_array(useSelStatus(element), 2), selected = _useSelStatus[0], path = _useSelStatus[1];
    // 组件内部状态
    var _useGetSetState = _sliced_to_array(useGetSetState({
        showBorder: false,
        htmlStr: '',
        hide: false,
        lang: element.language || ''
    }), 2), state = _useGetSetState[0], setState = _useGetSetState[1];
    // 更新代码节点数据
    var update = useCallback(function(data) {
        Transforms.setNodes(store.editor, data, {
            at: path
        });
    }, [
        path,
        store.editor
    ]);
    // 处理编辑器选中状态的边框显示
    useEffect(function() {
        if (selected && ReactEditor.isFocused(markdownEditorRef.current)) {
            setState({
                showBorder: true
            });
        } else if (state().showBorder) {
            setState({
                showBorder: false
            });
        }
    }, [
        selected,
        path,
        setState,
        state,
        markdownEditorRef
    ]);
    // 工具栏事件处理器
    var handleCloseClick = useCallback(function() {
        setState({
            hide: false
        });
    }, [
        setState
    ]);
    var handleRunHtml = useCallback(function() {
        try {
            setState({
                htmlStr: (element === null || element === void 0 ? void 0 : element.value) || ''
            });
        } catch (error) {
        // HTML 执行失败时静默处理
        }
    }, [
        element === null || element === void 0 ? void 0 : element.value,
        setState
    ]);
    var handleHtmlPreviewClose = useCallback(function() {
        setState({
            htmlStr: ''
        });
    }, [
        setState
    ]);
    var handleShowBorderChange = useCallback(function(show) {
        setState({
            showBorder: show
        });
    }, [
        setState
    ]);
    var handleHideChange = useCallback(function(hide) {
        setState({
            hide: hide
        });
    }, [
        setState
    ]);
    return {
        state: state(),
        setState: setState,
        update: update,
        selected: selected,
        path: path,
        handleCloseClick: handleCloseClick,
        handleRunHtml: handleRunHtml,
        handleHtmlPreviewClose: handleHtmlPreviewClose,
        handleShowBorderChange: handleShowBorderChange,
        handleHideChange: handleHideChange
    };
}
