import { __rest } from '../_virtual/_tslib.js';
import { jsx } from 'react/jsx-runtime';
import EChartsCanvas from '../taro/ec-canvas/index.js';
import { View } from '@tarojs/components';
import classNames from '../node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.js';
import 'echarts/components';
import 'echarts/features';
import 'echarts/renderers';
import { useRef, useMemo, useEffect } from 'react';
import { getPrefixCls } from './index.js';

/**
 * @internal
 *
 * @param type - 图表类型
 * @returns
 */
function defineComponent(type, customOption) {
    return (props) => {
        const { echarts, option, title, legend = undefined, tooltip = undefined, datasetSource, onChartInit, className, children } = props, restProps = __rest(props, ["echarts", "option", "title", "legend", "tooltip", "datasetSource", "onChartInit", "className", "children"]);
        const chartRef = useRef();
        const prefixCls = type ? getPrefixCls(type) : undefined;
        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 = isFunction(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 的每一列。
                series: [
                    // @ts-ignore
                    ...((_customOption === null || _customOption === void 0 ? void 0 : _customOption.series) || []),
                    // @ts-ignore
                    ...((option === null || option === void 0 ? void 0 : option.series) || []),
                    ...Array.from({ length: seriesLength }, () => ({ type })),
                ] });
        }, [props]);
        useEffect(() => {
            var _a;
            (_a = chartRef.current) === null || _a === void 0 ? void 0 : _a.setOption(_option);
        }, [_option]);
        const handleEc = useMemo(() => ({
            onInit: (canvas, width, height, dpr) => {
                const chart = echarts.init(canvas, null, { width, height, devicePixelRatio: dpr });
                canvas.setChart(chart);
                chart.setOption(_option);
                chartRef.current = chart;
                onChartInit === null || onChartInit === void 0 ? void 0 : onChartInit(chart);
                return chart;
            },
        }), [_option, echarts]);
        return (
        // @ts-ignore
        jsx(View, Object.assign({ className: classNames(prefixCls, className) }, restProps, { children: jsx(EChartsCanvas, { echarts: echarts, ec: handleEc }) })));
    };
}
function isFunction(arg) {
    return typeof arg === 'function';
}

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