/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
import { getColumnLabel, getNumberFormatter, getTimeFormatter, } from '@superset-ui/core';
import Echart from '../components/Echart';
import { formatSeriesName } from '../utils/series';
export default function EchartsGraph({ height, width, echartOptions, formData, onContextMenu, setDataMask, filterState, emitCrossFilters, refs, coltypeMapping, }) {
    const getCrossFilterDataMask = (node) => {
        if (!node?.name || !node?.col) {
            return undefined;
        }
        const { name, col } = node;
        const selected = Object.values(filterState?.selectedValues || {});
        let values;
        if (selected.includes(name)) {
            values = selected.filter(v => v !== name);
        }
        else {
            values = [name];
        }
        return {
            dataMask: {
                extraFormData: {
                    filters: values.length
                        ? [
                            {
                                col,
                                op: 'IN',
                                val: values,
                            },
                        ]
                        : [],
                },
                filterState: {
                    value: values.length ? values : null,
                    selectedValues: values.length ? values : null,
                },
            },
            isCurrentValueSelected: selected.includes(name),
        };
    };
    const eventHandlers = {
        click: (e) => {
            if (!emitCrossFilters || !setDataMask) {
                return;
            }
            e.event.stop();
            const data = echartOptions.series[0].data;
            const node = data.find(item => item.id === e.data.id);
            const dataMask = getCrossFilterDataMask(node)?.dataMask;
            if (dataMask) {
                setDataMask(dataMask);
            }
        },
        contextmenu: (e) => {
            const handleNodeClick = (data) => {
                const node = data.find(item => item.id === e.data.id);
                if (node?.name) {
                    return [
                        {
                            col: node.col,
                            op: '==',
                            val: node.name,
                            formattedVal: node.name,
                        },
                    ];
                }
                return undefined;
            };
            const handleEdgeClick = (data) => {
                const sourceValue = data.find(item => item.id === e.data.source)?.name;
                const targetValue = data.find(item => item.id === e.data.target)?.name;
                if (sourceValue && targetValue) {
                    return [
                        {
                            col: formData.source,
                            op: '==',
                            val: sourceValue,
                            formattedVal: sourceValue,
                        },
                        {
                            col: formData.target,
                            op: '==',
                            val: targetValue,
                            formattedVal: targetValue,
                        },
                    ];
                }
                return undefined;
            };
            if (onContextMenu) {
                e.event.stop();
                const pointerEvent = e.event.event;
                const data = echartOptions.series[0].data;
                const drillToDetailFilters = e.dataType === 'node' ? handleNodeClick(data) : handleEdgeClick(data);
                const node = data.find(item => item.id === e.data.id);
                onContextMenu(pointerEvent.clientX, pointerEvent.clientY, {
                    drillToDetail: drillToDetailFilters,
                    crossFilter: getCrossFilterDataMask(node),
                    drillBy: node && {
                        filters: [
                            {
                                col: node.col,
                                op: '==',
                                val: node.name,
                                formattedVal: formatSeriesName(node.name, {
                                    timeFormatter: getTimeFormatter(formData.dateFormat),
                                    numberFormatter: getNumberFormatter(formData.numberFormat),
                                    coltype: coltypeMapping?.[getColumnLabel(node.col)],
                                }),
                            },
                        ],
                        groupbyFieldName: node.col === formData.source ? 'source' : 'target',
                    },
                });
            }
        },
    };
    return (<Echart refs={refs} height={height} width={width} echartOptions={echartOptions} eventHandlers={eventHandlers}/>);
}
//# sourceMappingURL=EchartsGraph.jsx.map