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 ResizeObserver from "rc-resize-observer";
import React, { useEffect, useRef, useState } from "react";
import { debounce } from "../utils";
export var Container = function Container(props) {
    var _htmlRef_current;
    var chartRef = props.chartRef, htmlRef = props.htmlRef, children = props.children;
    var sizeRef = useRef({
        width: 0,
        height: 0
    });
    var _useState = _sliced_to_array(useState(false), 2), inView = _useState[0], setInView = _useState[1];
    var onSize = debounce(function() {
        var _htmlRef_current;
        if (!inView) return;
        var chart = chartRef.current;
        if (!chart) return;
        var preSize = sizeRef.current;
        var containerWidth = ((_htmlRef_current = htmlRef.current) === null || _htmlRef_current === void 0 ? void 0 : _htmlRef_current.clientWidth) || 0;
        // 让图表根据容器自然确定高度，保持最小高度
        var minHeight = 300;
        var naturalHeight = Math.max(containerWidth * 0.6, minHeight); // 宽高比约为 5:3
        var newSize = {
            width: containerWidth,
            height: naturalHeight
        };
        // 增加阈值，减少频繁调整导致的抖动
        if (Math.abs(preSize.width - newSize.width) > 30 || // 从 20 增加到 30
        Math.abs(preSize.height - newSize.height) > 30 // 从 20 增加到 30
        ) {
            chart.resize(newSize.width, newSize.height);
            sizeRef.current = newSize;
            return;
        }
    }, 300 + props.index * 20);
    useEffect(function() {
        if (inView) {
            onSize();
        }
        setInView(true);
    }, [
        inView
    ]);
    useEffect(function() {
        var _htmlRef_current;
        if (!inView) return;
        if (!htmlRef.current) return;
        var containerWidth = ((_htmlRef_current = htmlRef.current) === null || _htmlRef_current === void 0 ? void 0 : _htmlRef_current.clientWidth) || 0;
        var minHeight = 300;
        var naturalHeight = Math.max(containerWidth * 0.6, minHeight);
        var newSize = {
            width: containerWidth,
            height: naturalHeight
        };
        sizeRef.current = newSize;
    }, [
        htmlRef.current
    ]);
    if (typeof window === 'undefined' || typeof document === 'undefined') {
        return /*#__PURE__*/ React.createElement("div", {
            ref: htmlRef
        }, "chart");
    }
    // 计算容器高度，使用更自然的宽高比
    var containerWidth = ((_htmlRef_current = htmlRef.current) === null || _htmlRef_current === void 0 ? void 0 : _htmlRef_current.clientWidth) || 400;
    var minHeight = 300;
    var containerHeight = Math.max(containerWidth * 0.6, minHeight);
    return /*#__PURE__*/ React.createElement(ResizeObserver, {
        onResize: onSize
    }, /*#__PURE__*/ React.createElement("div", {
        ref: htmlRef,
        onClick: onSize,
        style: {
            height: "".concat(containerHeight, "px"),
            minHeight: "".concat(minHeight, "px"),
            width: '100%',
            position: 'relative'
        }
    }, children));
};
