/**
 * 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 { useCallback } from 'react';
import { getColumnLabel, getNumberFormatter, getTimeFormatter, } from '@superset-ui/core';
import Echart from '../components/Echart';
import { formatSeriesName } from '../utils/series';
export const extractTreePathInfo = (treePathInfo) => (treePathInfo ?? [])
    .map(pathInfo => pathInfo?.name || '')
    .filter(path => path !== '');
export default function EchartsSunburst(props) {
    const { height, width, echartOptions, setDataMask, labelMap, selectedValues, formData, onContextMenu, refs, emitCrossFilters, coltypeMapping, } = props;
    const { columns } = formData;
    const getCrossFilterDataMask = useCallback((treePathInfo) => {
        const treePath = extractTreePathInfo(treePathInfo);
        const name = treePath.join(',');
        const selected = Object.values(selectedValues);
        let values;
        if (selected.includes(name)) {
            values = selected.filter(v => v !== name);
        }
        else {
            values = [name];
        }
        const labels = values.map(value => labelMap[value]);
        return {
            dataMask: {
                extraFormData: {
                    filters: values.length === 0 || !columns
                        ? []
                        : columns.slice(0, treePath.length).map((col, idx) => {
                            const val = labels.map(v => v[idx]);
                            if (val === null || val === undefined)
                                return {
                                    col,
                                    op: 'IS NULL',
                                };
                            return {
                                col,
                                op: 'IN',
                                val: val,
                            };
                        }),
                },
                filterState: {
                    value: labels.length ? labels : null,
                    selectedValues: values.length ? values : null,
                },
            },
            isCurrentValueSelected: selected.includes(name),
        };
    }, [columns, labelMap, selectedValues]);
    const handleChange = useCallback((treePathInfo) => {
        if (!emitCrossFilters || !columns?.length) {
            return;
        }
        setDataMask(getCrossFilterDataMask(treePathInfo).dataMask);
    }, [emitCrossFilters, setDataMask, getCrossFilterDataMask]);
    const eventHandlers = {
        click: props => {
            const { treePathInfo } = props;
            handleChange(treePathInfo);
        },
        contextmenu: async (eventParams) => {
            if (onContextMenu) {
                eventParams.event.stop();
                const { data, treePathInfo } = eventParams;
                const { records } = data;
                const treePath = extractTreePathInfo(eventParams.treePathInfo);
                const pointerEvent = eventParams.event.event;
                const drillToDetailFilters = [];
                const drillByFilters = [];
                if (columns?.length) {
                    treePath.forEach((path, i) => drillToDetailFilters.push({
                        col: columns[i],
                        op: '==',
                        val: records[i],
                        formattedVal: path,
                    }));
                    const val = treePath[treePath.length - 1];
                    drillByFilters.push({
                        col: columns[treePath.length - 1],
                        op: '==',
                        val,
                        formattedVal: formatSeriesName(val, {
                            timeFormatter: getTimeFormatter(formData.dateFormat),
                            numberFormatter: getNumberFormatter(formData.numberFormat),
                            coltype: coltypeMapping?.[getColumnLabel(columns[treePath.length - 1])],
                        }),
                    });
                }
                onContextMenu(pointerEvent.clientX, pointerEvent.clientY, {
                    drillToDetail: drillToDetailFilters,
                    crossFilter: columns?.length
                        ? getCrossFilterDataMask(treePathInfo)
                        : undefined,
                    drillBy: { filters: drillByFilters, groupbyFieldName: 'columns' },
                });
            }
        },
    };
    return (<Echart refs={refs} height={height} width={width} echartOptions={echartOptions} eventHandlers={eventHandlers} selectedValues={selectedValues}/>);
}
//# sourceMappingURL=EchartsSunburst.jsx.map