'use strict';

var _tslib = require('../_virtual/_tslib.js');
var jsxRuntime = require('react/jsx-runtime');
var index$1 = require('../taro/ec-canvas/index.js');
var components = require('@tarojs/components');
var index = require('../node_modules/.pnpm/classnames@2.5.1/node_modules/classnames/index.js');
require('echarts/components');
require('echarts/features');
require('echarts/renderers');
var React = require('react');
var index$2 = require('./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 = _tslib.__rest(props, ["echarts", "option", "title", "legend", "tooltip", "datasetSource", "onChartInit", "className", "children"]);
        const chartRef = React.useRef();
        const prefixCls = type ? index$2.getPrefixCls(type) : undefined;
        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 = 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]);
        React.useEffect(() => {
            var _a;
            (_a = chartRef.current) === null || _a === void 0 ? void 0 : _a.setOption(_option);
        }, [_option]);
        const handleEc = React.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
        jsxRuntime.jsx(components.View, Object.assign({ className: index.default(prefixCls, className) }, restProps, { children: jsxRuntime.jsx(index$1.default, { echarts: echarts, ec: handleEc }) })));
    };
}
function isFunction(arg) {
    return typeof arg === 'function';
}

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