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 { useEffect, useMemo, useRef, useState } from "react";
import { useRefFunction } from "../../Hooks/useRefFunction";
import { isBrowser, isTest } from "../../Utils/env";
import { ATTACHMENT_EXTRA_HEIGHT_PX, BREAKPOINT_COLLAPSE_CONTAINER_PX, BREAKPOINT_COLLAPSE_VIEWPORT_PX, COLLAPSED_HEIGHT_BASE_PX, DEFAULT_EDITOR_RIGHT_PADDING_PX, ENLARGED_DEFAULT_HEIGHT_PX, ENLARGED_MIN_HEIGHT_PX, MIN_HEIGHT_MULTI_ROW_PX, MIN_HEIGHT_SINGLE_ACTION_PX, MIN_HEIGHT_WITH_ENLARGE_AND_REFINE_PX, SEND_ACTIONS_FALLBACK_RIGHT_PADDING_PX } from "../constants";
/**
 * 输入框几何信息合并 Hook。
 *
 * 由 useMarkdownInputFieldLayout（容器尺寸 / 折叠状态 / setter）与
 * useMarkdownInputFieldStyles（基于上述状态计算的派生样式）合并而来。
 * 合并的目的是：消费侧只需关心一组最终值，避免主组件被迫充当中间胶水。
 */ export var useInputFieldGeometry = function useInputFieldGeometry(param) {
    var isEnlarged = param.isEnlarged, hasTools = param.hasTools, hasEnlargeAction = param.hasEnlargeAction, hasRefineAction = param.hasRefineAction, totalActionCount = param.totalActionCount, isMultiRowLayout = param.isMultiRowLayout, maxHeight = param.maxHeight, style = param.style, attachment = param.attachment;
    // ===== Layout 部分 =====
    var _useState = _sliced_to_array(useState(function() {
        if (!isBrowser()) return false;
        if (window.innerWidth < BREAKPOINT_COLLAPSE_VIEWPORT_PX) return true;
        return false;
    }), 2), collapseSendActions = _useState[0], setCollapseSendActions = _useState[1];
    var _useState1 = _sliced_to_array(useState(DEFAULT_EDITOR_RIGHT_PADDING_PX), 2), rightPadding = _useState1[0], setRightPadding = _useState1[1];
    var _useState2 = _sliced_to_array(useState(0), 2), topRightPadding = _useState2[0], setTopRightPadding = _useState2[1];
    var _useState3 = _sliced_to_array(useState(0), 2), quickRightOffset = _useState3[0], setQuickRightOffset = _useState3[1];
    // 对外只暴露稳定的回调，避免主组件再当胶水把 setter 接到子组件 onResize 上。
    var onSendActionsResize = useRefFunction(function(width) {
        setRightPadding(width);
    });
    var onQuickActionsResize = useRefFunction(function(width, rightOffset) {
        setTopRightPadding(width);
        setQuickRightOffset(rightOffset);
    });
    var inputRef = useRef(null);
    useEffect(function() {
        if (!inputRef.current) return;
        if (isTest()) return;
        // 容器宽度小于 481 时折叠发送区按钮；observe() 会触发一次初始回调，无需手动初始化。
        // NOTE: 旧实现还维护了一份 `dimensions` state 和 `setCollapseSendActions` 对外 setter，
        // 但代码库中均无消费者（仅旧单测访问），合并到 useInputFieldGeometry 时一并移除。
        var handleResize = function handleResize() {
            if (!inputRef.current) return;
            if (isTest()) return;
            if (inputRef.current.clientWidth < BREAKPOINT_COLLAPSE_CONTAINER_PX) {
                setCollapseSendActions(true);
            } else {
                setCollapseSendActions(false);
            }
        };
        var resizeObserver = new ResizeObserver(function() {
            handleResize();
        });
        resizeObserver.observe(inputRef.current);
        return function() {
            resizeObserver.disconnect();
        };
    }, []);
    // ===== Styles 部分 =====
    var computedRightPadding = useMemo(function() {
        var bottomOverlayPadding = hasTools ? 0 : rightPadding || SEND_ACTIONS_FALLBACK_RIGHT_PADDING_PX;
        var topOverlayPadding = (topRightPadding || 0) + (quickRightOffset || 0);
        return Math.max(bottomOverlayPadding, topOverlayPadding);
    }, [
        hasTools,
        rightPadding,
        topRightPadding,
        quickRightOffset
    ]);
    var collapsedHeight = useMemo(function() {
        // 优先使用 maxHeight prop，其次使用 style.maxHeight，最后使用默认值
        var maxHeightValue = maxHeight !== null && maxHeight !== void 0 ? maxHeight : style === null || style === void 0 ? void 0 : style.maxHeight;
        var base = typeof maxHeightValue === 'number' ? maxHeightValue : maxHeightValue ? parseFloat(String(maxHeightValue)) || COLLAPSED_HEIGHT_BASE_PX : COLLAPSED_HEIGHT_BASE_PX;
        return base;
    }, [
        maxHeight,
        style === null || style === void 0 ? void 0 : style.maxHeight
    ]);
    var collapsedHeightPx = useMemo(function() {
        var extra = (attachment === null || attachment === void 0 ? void 0 : attachment.enable) ? ATTACHMENT_EXTRA_HEIGHT_PX : 0;
        return collapsedHeight + extra;
    }, [
        collapsedHeight,
        attachment === null || attachment === void 0 ? void 0 : attachment.enable
    ]);
    var computedMinHeight = useMemo(function() {
        if (isEnlarged) return 'auto';
        if ((style === null || style === void 0 ? void 0 : style.minHeight) !== undefined) return style.minHeight;
        // 同时存在放大按钮 + 提示词优化按钮
        if (hasEnlargeAction && hasRefineAction) {
            return MIN_HEIGHT_WITH_ENLARGE_AND_REFINE_PX;
        }
        if (totalActionCount === 1) return MIN_HEIGHT_SINGLE_ACTION_PX;
        // 其他多行布局
        if (isMultiRowLayout) return MIN_HEIGHT_MULTI_ROW_PX;
        return (style === null || style === void 0 ? void 0 : style.minHeight) || 0;
    }, [
        isEnlarged,
        hasEnlargeAction,
        hasRefineAction,
        isMultiRowLayout,
        totalActionCount,
        style === null || style === void 0 ? void 0 : style.minHeight
    ]);
    var enlargedStyle = useMemo(function() {
        if (!isEnlarged) return {};
        return {
            maxHeight: "".concat(ENLARGED_DEFAULT_HEIGHT_PX, "px"),
            minHeight: "".concat(ENLARGED_MIN_HEIGHT_PX, "px")
        };
    }, [
        isEnlarged
    ]);
    return {
        inputRef: inputRef,
        collapseSendActions: collapseSendActions,
        onSendActionsResize: onSendActionsResize,
        onQuickActionsResize: onQuickActionsResize,
        computedRightPadding: computedRightPadding,
        collapsedHeightPx: collapsedHeightPx,
        computedMinHeight: computedMinHeight,
        enlargedStyle: enlargedStyle
    };
};
