'use strict';

var _tslib = require('../_virtual/_tslib.js');
var jsxRuntime = require('react/jsx-runtime');
var EChartsReactCore = require('echarts-for-react/lib/core');
var components = require('echarts/components');
var features = require('echarts/features');
var renderers = require('echarts/renderers');
var React = require('react');

/**
 * @internal
 */
const commonEchartsComponents = [
    components.DatasetComponent,
    components.LegendComponent,
    components.TitleComponent,
    components.TooltipComponent,
    features.LabelLayout,
    features.UniversalTransition,
    renderers.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 = _tslib.__rest(props, ["option", "title", "legend", "tooltip", "datasetSource"]);
        const echartsReactCoreRef = React.useRef(null);
        React.useImperativeHandle(ref, () => { var _a; return (_a = echartsReactCoreRef.current) === null || _a === void 0 ? void 0 : _a.getEchartsInstance(); });
        const _option = React.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 jsxRuntime.jsx(EChartsReactCore, Object.assign({ ref: echartsReactCoreRef, echarts: echarts, option: _option }, restProps));
    });
}
function isFunctionType(arg) {
    return typeof arg === 'function';
}

exports.commonEchartsComponents = commonEchartsComponents;
exports.defineComponent = defineComponent;
//# sourceMappingURL=comp-factory.js.map
