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 { ConfigProvider } from "antd";
import classNames from "clsx";
import React, { useContext, useEffect, useRef, useState } from "react";
import ReactDOM from "react-dom";
import { Editor, Range, Transforms } from "slate";
import { useRefFunction } from "../../../../Hooks/useRefFunction";
import { MARKDOWN_EDITOR_EVENTS } from "../../../BaseMarkdownEditor";
import { useEditorStore } from "../../store";
import { getSelRect } from "../../utils/dom";
import { BaseToolBar } from "./BaseBar";
import { useStyle } from "./floatBarStyle";
import { ReadonlyBaseBar } from "./ReadonlyBaseBar";
var fileMap = new Map();
/**
 * 浮动工具栏,用于设置文本样式
 */ export var FloatBar = function FloatBar(props) {
    var floatBarRef = useRef(null);
    var _useEditorStore = useEditorStore(), domRect = _useEditorStore.domRect, setDomRect = _useEditorStore.setDomRect, markdownContainerRef = _useEditorStore.markdownContainerRef, markdownEditorRef = _useEditorStore.markdownEditorRef;
    var _useState = _sliced_to_array(useState(false), 2), isOpen = _useState[0], setIsOpen = _useState[1];
    var sel = React.useRef();
    var resize = useRefFunction(function() {
        var force = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
        if (domRect && floatBarRef.current) {
            var left = domRect.x;
            left = left - ((props.readonly ? 65 : 178) - domRect.width) / 2;
            var container = markdownContainerRef.current;
            if (left < 4) left = 4;
            var barWidth = props.readonly ? 65 : 232;
            if (left > container.clientWidth - barWidth) left = container.clientWidth - barWidth / 2;
            var top = isOpen && !force ? parseFloat(floatBarRef.current.style.top || '0') : domRect.top - 32;
            var finalLeft = Math.max(left, 4);
            var finalTop = Math.max(top, 4);
            floatBarRef.current.style.left = finalLeft + 'px';
            floatBarRef.current.style.top = finalTop + 'px';
            setIsOpen(true);
        } else {
            setIsOpen(false);
        }
    });
    useEffect(function() {
        if (domRect && markdownEditorRef.current) {
            resize(true);
            sel.current = markdownEditorRef.current.selection;
        } else {
            setIsOpen(false);
            fileMap.clear();
        }
    }, [
        domRect
    ]);
    useEffect(function() {
        var resizeFn = function resizeFn(e) {
            if (markdownEditorRef.current && floatBarRef.current) {
                floatBarRef.current.style.top = e.clientY + 20 + 'px';
                floatBarRef.current.style.left = e.clientX + 'px';
                resize(true);
            }
        };
        if (markdownContainerRef.current) {
            var _markdownContainerRef_current;
            (_markdownContainerRef_current = markdownContainerRef.current) === null || _markdownContainerRef_current === void 0 ? void 0 : _markdownContainerRef_current.addEventListener(MARKDOWN_EDITOR_EVENTS.SELECTIONCHANGE, resizeFn);
        }
        return function() {
            if (markdownContainerRef.current) {
                var _markdownContainerRef_current;
                (_markdownContainerRef_current = markdownContainerRef.current) === null || _markdownContainerRef_current === void 0 ? void 0 : _markdownContainerRef_current.removeEventListener(MARKDOWN_EDITOR_EVENTS.SELECTIONCHANGE, resizeFn);
            }
        };
    }, [
        isOpen
    ]);
    useEffect(function() {
        var _markdownContainerRef_current;
        if (typeof window === 'undefined') return;
        var close = function close(e) {
            if (e.key === 'Escape') {
                e.preventDefault();
                setIsOpen(false);
                fileMap.clear();
                if (!sel.current) return;
                //@ts-ignore
                var end = Range.end(sel.current).path;
                if (Editor.hasPath(markdownEditorRef.current, end)) {
                    Transforms.select(markdownEditorRef.current, Editor.end(markdownEditorRef.current, end));
                }
            }
        };
        markdownContainerRef === null || markdownContainerRef === void 0 ? void 0 : (_markdownContainerRef_current = markdownContainerRef.current) === null || _markdownContainerRef_current === void 0 ? void 0 : _markdownContainerRef_current.addEventListener('keydown', close);
        return function() {
            var _markdownContainerRef_current;
            markdownContainerRef === null || markdownContainerRef === void 0 ? void 0 : (_markdownContainerRef_current = markdownContainerRef.current) === null || _markdownContainerRef_current === void 0 ? void 0 : _markdownContainerRef_current.removeEventListener('keydown', close);
        };
    }, [
        isOpen
    ]);
    useEffect(function() {
        var change = function change() {
            if (isOpen) {
                var rect = getSelRect();
                if (rect) {
                    setDomRect === null || setDomRect === void 0 ? void 0 : setDomRect(rect);
                }
                resize(true);
            }
        };
        window.addEventListener('resize', change);
        return function() {
            return window.removeEventListener('resize', change);
        };
    }, [
        isOpen
    ]);
    var context = useContext(ConfigProvider.ConfigContext);
    var baseClassName = context === null || context === void 0 ? void 0 : context.getPrefixCls("agentic-md-editor-float-bar");
    var _useStyle = useStyle(baseClassName), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    if (!markdownContainerRef.current) return null;
    return /*#__PURE__*/ ReactDOM.createPortal(wrapSSR(/*#__PURE__*/ React.createElement("div", {
        style: {
            position: 'fixed',
            opacity: isOpen ? 1 : 0,
            transition: 'all 0.3s ease-out',
            userSelect: 'none'
        },
        ref: floatBarRef,
        onMouseDown: function onMouseDown(e) {
            e.preventDefault();
            e.stopPropagation();
        },
        className: classNames(baseClassName, hashId)
    }, props.readonly ? /*#__PURE__*/ React.createElement(ReadonlyBaseBar, {
        prefix: baseClassName
    }) : /*#__PURE__*/ React.createElement(BaseToolBar, {
        prefix: baseClassName,
        hashId: hashId
    }))), document.body);
};
