import { __rest } from '../_virtual/_tslib.js';
import { jsx } from 'react/jsx-runtime';
import EChartsReactCore from 'echarts-for-react/lib/core';
import { DatasetComponent, LegendComponent, TitleComponent, TooltipComponent } from 'echarts/components';
import { LabelLayout, UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import React, { useRef, useImperativeHandle, useMemo } from 'react';

/**
 * @internal
 */
const commonEchartsComponents = [
    DatasetComponent,
    LegendComponent,
    TitleComponent,
    TooltipComponent,
    LabelLayout,
    UniversalTransition,
    CanvasRenderer,
];
/**
 * @internal
 *
 * @param echarts - echarts instance
 * @param type - 图表类型
 * @returns
 */
function defineComponent(echarts, type, customOption) {
    return React.forwardRef((props, ref) => {
        const { option, title, legend = {}, tooltip = {}, datasetSource } = props, restProps = __rest(props, ["option", "title", "legend", "tooltip", "datasetSource"]);
        const echartsReactCoreRef = useRef(null);
        useImperativeHandle(ref, () => { var _a; return (_a = echartsReactCoreRef.current) === null || _a === void 0 ? void 0 : _a.getEchartsInstance(); });
        const _option = useMemo(() => {
            let seriesLength = 0;
            if (datasetSource) {
                if (Array.isArray(datasetSource === null || datasetSource === void 0 ? void 0 : datasetSource[0])) {
                    seriesLength = (datasetSource === null || datasetSource === void 0 ? void 0 : datasetSource[0].length) - 1 || 1;
                }
                else {
                    seriesLength = Object.keys(datasetSource === null || datasetSource === void 0 ? void 0 : datasetSource[0]).length - 1 || 1;
                }
            }
            const _customOption = isFunctionType(customOption) ? customOption(props) : customOption;
            return Object.assign(Object.assign(Object.assign({ title,
                legend,
                tooltip }, _customOption), option), { dataset: Object.assign(Object.assign({}, ((option === null || option === void 0 ? void 0 : option.dataset) || {})), { source: datasetSource }), 
                // 声明多个 图表 系列，默认情况下，每个系列会自动对应到 dataset 的每一列。
                // @ts-ignore
                series: (option === null || option === void 0 ? void 0 : option.series)
                    ? // @ts-ignore
                        option === null || option === void 0 ? void 0 : option.series
                    : [
                        // @ts-ignore
                        ...((_customOption === null || _customOption === void 0 ? void 0 : _customOption.series) || []),
                        ...Array.from({ length: seriesLength }, () => ({ type })),
                    ] });
        }, [props]);
        return jsx(EChartsReactCore, Object.assign({ ref: echartsReactCoreRef, echarts: echarts, option: _option }, restProps));
    });
}
function isFunctionType(arg) {
    return typeof arg === 'function';
}

export { commonEchartsComponents, defineComponent };
//# sourceMappingURL=comp-factory.js.map
