import { Gesture } from "./../../vrender";

import { IconFuncTypeEnum, InteractionState } from "../../ts-types";

import { getCellEventArgsSet, regIndexReg } from "../util";

import { TABLE_EVENT_TYPE } from "../../core/TABLE_EVENT_TYPE";

import { isValid } from "@visactor/vutils";

import { getIconAndPositionFromTarget } from "../../scenegraph/utils/icon";

import { cellInRanges, getPromiseValue } from "../../tools/helper";

import { Rect } from "../../tools/Rect";

import { getCellMergeInfo } from "../../scenegraph/utils/get-cell-merge";

import { handleWhell } from "../scroll";

import { fireMoveColEventListeners } from "../helper";

import { clearPageSelection } from "../../tools/env";

export function bindTableGroupListener(eventManager) {
    const table = eventManager.table, stateManager = table.stateManager;
    table.scenegraph.tableGroup.addEventListener("pointermove", (e => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
        const lastX = null !== (_b = null === (_a = table.eventManager.LastPointerXY) || void 0 === _a ? void 0 : _a.x) && void 0 !== _b ? _b : e.x, lastY = null !== (_d = null === (_c = table.eventManager.LastPointerXY) || void 0 === _c ? void 0 : _c.y) && void 0 !== _d ? _d : e.y;
        table.eventManager.LastPointerXY = {
            x: e.x,
            y: e.y
        }, eventManager.touchSetTimeout && (clearTimeout(eventManager.touchSetTimeout), 
        eventManager.touchSetTimeout = void 0);
        const eventArgsSet = getCellEventArgsSet(e);
        if (eventArgsSet.eventArgs && table.hasListeners(TABLE_EVENT_TYPE.MOUSEMOVE_TABLE) && table.fireListeners(TABLE_EVENT_TYPE.MOUSEMOVE_TABLE, {
            col: eventArgsSet.eventArgs.col,
            row: eventArgsSet.eventArgs.row,
            x: eventArgsSet.abstractPos.x,
            y: eventArgsSet.abstractPos.y,
            event: e.nativeEvent,
            target: null === (_e = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _e ? void 0 : _e.target,
            mergeCellInfo: null === (_f = eventArgsSet.eventArgs) || void 0 === _f ? void 0 : _f.mergeInfo
        }), stateManager.interactionState === InteractionState.grabing) return void (Math.abs(lastX - e.x) + Math.abs(lastY - e.y) >= 1 && (stateManager.isResizeCol() || stateManager.isResizeRow() || stateManager.isMoveCol() || (stateManager.isFillHandle() ? eventManager.dealFillSelect(eventArgsSet, !0) : (null === (_g = table.options.select) || void 0 === _g ? void 0 : _g.disableDragSelect) || eventManager.dealTableSelect(eventArgsSet, !0))));
        !(null === (_h = table.options.select) || void 0 === _h ? void 0 : _h.disableDragSelect) && table.eventManager.isDraging && stateManager.isSelecting() && !(null === (_j = table.editorManager) || void 0 === _j ? void 0 : _j.editingEditor) && eventManager.dealTableSelect(eventArgsSet, !0);
        const cellGoup = e.path.find((node => "cell" === node.role));
        if (table.hasListeners(TABLE_EVENT_TYPE.MOUSELEAVE_CELL) && (-1 === table.stateManager.hover.cellPos.col || -1 === table.stateManager.hover.cellPos.row || (null == cellGoup ? void 0 : cellGoup.col) === table.stateManager.hover.cellPos.col && (null == cellGoup ? void 0 : cellGoup.row) === table.stateManager.hover.cellPos.row || table.fireListeners(TABLE_EVENT_TYPE.MOUSELEAVE_CELL, {
            col: table.stateManager.hover.cellPos.col,
            row: table.stateManager.hover.cellPos.row,
            cellRange: table.getCellRangeRelativeRect({
                col: table.stateManager.hover.cellPos.col,
                row: table.stateManager.hover.cellPos.row
            }),
            scaleRatio: table.canvas.getBoundingClientRect().width / table.canvas.offsetWidth,
            event: e.nativeEvent,
            target: null === (_k = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _k ? void 0 : _k.target
        })), table.hasListeners(TABLE_EVENT_TYPE.MOUSEENTER_CELL) && ("cell" !== (null == cellGoup ? void 0 : cellGoup.role) || !isValid(cellGoup.col) || !isValid(cellGoup.row) || cellGoup.col === table.stateManager.hover.cellPos.col && cellGoup.row === table.stateManager.hover.cellPos.row || cellGoup.col === (null === (_l = table.stateManager.hover.cellPosContainHeader) || void 0 === _l ? void 0 : _l.col) && cellGoup.row === (null === (_m = table.stateManager.hover.cellPosContainHeader) || void 0 === _m ? void 0 : _m.row) || table.fireListeners(TABLE_EVENT_TYPE.MOUSEENTER_CELL, {
            col: cellGoup.col,
            row: cellGoup.row,
            cellRange: table.getCellRangeRelativeRect({
                col: cellGoup.col,
                row: cellGoup.row
            }),
            scaleRatio: table.canvas.getBoundingClientRect().width / table.canvas.offsetWidth,
            event: e.nativeEvent,
            target: null === (_o = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _o ? void 0 : _o.target,
            mergeCellInfo: null === (_p = eventArgsSet.eventArgs) || void 0 === _p ? void 0 : _p.mergeInfo
        })), eventManager.dealIconHover(eventArgsSet), eventManager.dealTableHover(eventArgsSet), 
        table.theme.columnResize.visibleOnHover && eventManager.checkColumnResize(eventArgsSet, !0), 
        eventArgsSet.eventArgs && table.hasListeners(TABLE_EVENT_TYPE.MOUSEMOVE_CELL)) {
            let icon, position;
            if (null === (_q = eventArgsSet.eventArgs) || void 0 === _q ? void 0 : _q.target) {
                const iconInfo = getIconAndPositionFromTarget(null === (_r = eventArgsSet.eventArgs) || void 0 === _r ? void 0 : _r.target);
                iconInfo && (icon = iconInfo.icon, position = iconInfo.position);
            }
            table.fireListeners(TABLE_EVENT_TYPE.MOUSEMOVE_CELL, {
                col: eventArgsSet.eventArgs.col,
                row: eventArgsSet.eventArgs.row,
                x: eventArgsSet.abstractPos.x,
                y: eventArgsSet.abstractPos.y,
                event: e.nativeEvent,
                targetIcon: icon ? {
                    name: icon.name,
                    position: position,
                    funcType: icon.attribute.funcType
                } : void 0,
                target: null === (_s = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _s ? void 0 : _s.target,
                mergeCellInfo: null === (_t = eventArgsSet.eventArgs) || void 0 === _t ? void 0 : _t.mergeInfo
            });
        }
    })), table.scenegraph.tableGroup.addEventListener("pointerout", (e => {
        var _a;
        const eventArgsSet = getCellEventArgsSet(e), cellGoup = null === (_a = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.target;
        "table" === (null == cellGoup ? void 0 : cellGoup.role) && eventManager.dealTableHover();
    })), table.scenegraph.tableGroup.addEventListener("pointerover", (e => {
        var _a, _b;
        const eventArgsSet = getCellEventArgsSet(e), cellGoup = null === (_a = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.target;
        if (cellGoup && table.hasListeners(TABLE_EVENT_TYPE.MOUSEOVER_CHART_SYMBOL) && "symbol" === cellGoup.type) {
            const cellGroup = e.composedPath().find((p => "cell" === p.roll));
            if (cellGroup) {
                const {col: col, row: row} = cellGroup, eventInfo = {
                    col: col,
                    row: row,
                    field: table.getHeaderField(col, row),
                    value: table.getCellValue(col, row),
                    dataValue: table.getCellOriginValue(col, row),
                    cellHeaderPaths: table.internalProps.layoutMap.getCellHeaderPaths(col, row),
                    title: table.getBodyColumnDefine(col, row).title,
                    cellRange: table.getCellRelativeRect(col, row),
                    event: e.nativeEvent,
                    sparkline: {
                        pointData: void 0
                    },
                    scaleRatio: table.canvas.getBoundingClientRect().width / table.canvas.offsetWidth,
                    target: null === (_b = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.target
                };
                table.fireListeners(TABLE_EVENT_TYPE.MOUSEOVER_CHART_SYMBOL, eventInfo);
            }
        }
    })), table.scenegraph.tableGroup.addEventListener("pointerenter", (e => {
        (table.theme.scrollStyle.horizontalVisible && "focus" === table.theme.scrollStyle.horizontalVisible || !table.theme.scrollStyle.horizontalVisible && "focus" === table.theme.scrollStyle.visible) && stateManager.showHorizontalScrollBar(), 
        (table.theme.scrollStyle.verticalVisible && "focus" === table.theme.scrollStyle.verticalVisible || !table.theme.scrollStyle.verticalVisible && "focus" === table.theme.scrollStyle.visible) && stateManager.showVerticalScrollBar(), 
        table.hasListeners(TABLE_EVENT_TYPE.MOUSEENTER_TABLE) && table.fireListeners(TABLE_EVENT_TYPE.MOUSEENTER_TABLE, {
            event: e.nativeEvent
        });
    })), table.scenegraph.tableGroup.addEventListener("pointerleave", (e => {
        stateManager.isResizeCol() || stateManager.isResizeRow() || stateManager.isMoveCol() || stateManager.isSelecting() || (stateManager.updateInteractionState(InteractionState.default), 
        stateManager.updateCursor()), (table.theme.scrollStyle.horizontalVisible && "focus" === table.theme.scrollStyle.horizontalVisible || !table.theme.scrollStyle.horizontalVisible && "focus" === table.theme.scrollStyle.visible) && stateManager.hideHorizontalScrollBar(), 
        (table.theme.scrollStyle.verticalVisible && "focus" === table.theme.scrollStyle.verticalVisible || !table.theme.scrollStyle.verticalVisible && "focus" === table.theme.scrollStyle.visible) && stateManager.hideVerticalScrollBar(), 
        table.hasListeners(TABLE_EVENT_TYPE.MOUSELEAVE_CELL) && -1 !== table.stateManager.hover.cellPos.col && -1 !== table.stateManager.hover.cellPos.row && table.fireListeners(TABLE_EVENT_TYPE.MOUSELEAVE_CELL, {
            col: table.stateManager.hover.cellPos.col,
            row: table.stateManager.hover.cellPos.row,
            cellRange: table.getCellRangeRelativeRect({
                col: table.stateManager.hover.cellPos.col,
                row: table.stateManager.hover.cellPos.row
            }),
            scaleRatio: table.canvas.getBoundingClientRect().width / table.canvas.offsetWidth,
            event: e.nativeEvent,
            target: void 0
        }), eventManager.dealTableHover();
        const target = e.target;
        target && !target.isDescendantsOf(table.scenegraph.tableGroup) && table.fireListeners(TABLE_EVENT_TYPE.MOUSELEAVE_TABLE, {
            event: e.nativeEvent
        });
    })), table.scenegraph.tableGroup.addEventListener("pointerdown", (e => {
        var _a, _b, _c, _d, _e;
        if (table.hasListeners(TABLE_EVENT_TYPE.MOUSEDOWN_TABLE) && table.fireListeners(TABLE_EVENT_TYPE.MOUSEDOWN_TABLE, {
            event: e.nativeEvent
        }), clearPageSelection(), table.eventManager.isDown = !0, table.eventManager.LastBodyPointerXY = {
            x: e.x,
            y: e.y
        }, table.eventManager.LastPointerXY = {
            x: e.x,
            y: e.y
        }, 0 !== e.button) return;
        const eventArgsSet = getCellEventArgsSet(e);
        if (eventManager.downIcon = void 0, stateManager.interactionState !== InteractionState.default) return;
        if (table.isPivotChart() && "chart" !== (null === (_a = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.target.type) && table.scenegraph.updateChartState(null), 
        (null === (_b = eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.target) !== (null === (_c = stateManager.residentHoverIcon) || void 0 === _c ? void 0 : _c.icon) && stateManager.hideMenu(), 
        "chart" === (null === (_d = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _d ? void 0 : _d.target.type)) return;
        const isCompleteEdit = null === (_e = table.editorManager) || void 0 === _e ? void 0 : _e.completeEdit(e.nativeEvent);
        getPromiseValue(isCompleteEdit, (isCompleteEdit => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
            if (!(null === (_a = table.options.customConfig) || void 0 === _a ? void 0 : _a.selectCellWhenCellEditorNotExists) && !1 === isCompleteEdit) return;
            const hitIcon = (null === (_d = null === (_c = null === (_b = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.target) || void 0 === _c ? void 0 : _c.role) || void 0 === _d ? void 0 : _d.startsWith("icon")) ? eventArgsSet.eventArgs.target : (null === (_e = e.target.role) || void 0 === _e ? void 0 : _e.startsWith("icon")) ? e.target : void 0;
            if (eventManager.downIcon = hitIcon, hitIcon && !1 !== hitIcon.attribute.interactive) hitIcon.attribute.funcType === IconFuncTypeEnum.dragReorder && (stateManager.startMoveCol(eventArgsSet.eventArgs.col, eventArgsSet.eventArgs.row, eventArgsSet.abstractPos.x, eventArgsSet.abstractPos.y, null === (_l = null === (_k = eventArgsSet.eventArgs) || void 0 === _k ? void 0 : _k.event) || void 0 === _l ? void 0 : _l.nativeEvent), 
            stateManager.updateInteractionState(InteractionState.grabing)); else {
                if (eventManager.cellIsHeaderCheck(eventArgsSet)) return;
                if ("touch" === e.pointerType) {
                    if (eventManager.touchEnd = !1, eventManager.touchSetTimeout = setTimeout((() => {
                        eventManager.isTouchdown = !1, eventManager.isLongTouch = !0, (eventManager.touchEnd || !eventManager.checkColumnResize(eventArgsSet, !0) && !eventManager.checkRowResize(eventArgsSet, !0)) && (eventManager.touchEnd || !eventManager.checkColumnMover(eventArgsSet)) ? eventManager.dealTableSelect(eventArgsSet) && !eventManager.touchEnd && stateManager.updateInteractionState(InteractionState.grabing) : stateManager.updateInteractionState(InteractionState.grabing);
                    }), 500), table.hasListeners(TABLE_EVENT_TYPE.MOUSEENTER_CELL)) {
                        const cellGoup = e.path.find((node => "cell" === node.role));
                        "cell" !== (null == cellGoup ? void 0 : cellGoup.role) || !isValid(cellGoup.col) || !isValid(cellGoup.row) || cellGoup.col === table.stateManager.hover.cellPos.col && cellGoup.row === table.stateManager.hover.cellPos.row || cellGoup.col === (null === (_f = table.stateManager.hover.cellPosContainHeader) || void 0 === _f ? void 0 : _f.col) && cellGoup.row === (null === (_g = table.stateManager.hover.cellPosContainHeader) || void 0 === _g ? void 0 : _g.row) || table.fireListeners(TABLE_EVENT_TYPE.MOUSEENTER_CELL, {
                            col: cellGoup.col,
                            row: cellGoup.row,
                            cellRange: table.getCellRangeRelativeRect({
                                col: cellGoup.col,
                                row: cellGoup.row
                            }),
                            scaleRatio: table.canvas.getBoundingClientRect().width / table.canvas.offsetWidth,
                            event: e.nativeEvent,
                            target: null === (_h = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _h ? void 0 : _h.target,
                            mergeCellInfo: null === (_j = eventArgsSet.eventArgs) || void 0 === _j ? void 0 : _j.mergeInfo
                        });
                    }
                    eventManager.dealTableHover(eventArgsSet);
                } else {
                    if (!eventManager.checkCellFillhandle(eventArgsSet) && (eventManager.checkColumnResize(eventArgsSet, !0) || eventManager.checkRowResize(eventArgsSet, !0))) return table.scenegraph.updateChartState(null), 
                    void stateManager.updateInteractionState(InteractionState.grabing);
                    if (eventManager.checkColumnMover(eventArgsSet)) return void stateManager.updateInteractionState(InteractionState.grabing);
                    if (eventManager.checkCellFillhandle(eventArgsSet, !0) && eventManager.dealFillSelect(eventArgsSet)) return void stateManager.updateInteractionState(InteractionState.grabing);
                    eventManager.dealTableSelect(eventArgsSet) && stateManager.updateInteractionState(InteractionState.grabing);
                }
            }
            if (table.hasListeners(TABLE_EVENT_TYPE.MOUSEDOWN_CELL)) {
                const eventArgsSet = getCellEventArgsSet(e);
                eventArgsSet.eventArgs && table.fireListeners(TABLE_EVENT_TYPE.MOUSEDOWN_CELL, {
                    col: eventArgsSet.eventArgs.col,
                    row: eventArgsSet.eventArgs.row,
                    event: e.nativeEvent,
                    target: null === (_m = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _m ? void 0 : _m.target,
                    mergeCellInfo: eventArgsSet.eventArgs.mergeInfo
                });
            }
        }));
    })), table.scenegraph.tableGroup.addEventListener("pointerup", (e => {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        if (0 === e.button) {
            if ("grabing" === stateManager.interactionState) {
                if (stateManager.updateInteractionState(InteractionState.default), stateManager.isResizeCol()) endResizeCol(table); else if (stateManager.isResizeRow()) endResizeRow(table); else if (stateManager.isMoveCol()) {
                    const endMoveColSuccess = table.stateManager.endMoveCol();
                    fireMoveColEventListeners(table, endMoveColSuccess, e.nativeEvent);
                } else if (stateManager.isSelecting()) {
                    table.stateManager.endSelectCells(), table.stateManager.isFillHandle() && table.stateManager.endFillSelect();
                    const eventArgsSet = getCellEventArgsSet(e);
                    if (table.eventManager.isDraging && eventArgsSet.eventArgs && table.hasListeners(TABLE_EVENT_TYPE.DRAG_SELECT_END)) {
                        const cellsEvent = {
                            event: e.nativeEvent,
                            cells: [],
                            col: eventArgsSet.eventArgs.target.col,
                            row: eventArgsSet.eventArgs.target.row,
                            scaleRatio: table.canvas.getBoundingClientRect().width / table.canvas.offsetWidth,
                            target: null === (_a = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.target,
                            mergeCellInfo: null === (_b = eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.mergeInfo
                        };
                        cellsEvent.cells = table.getSelectedCellInfos(), table.fireListeners(TABLE_EVENT_TYPE.DRAG_SELECT_END, cellsEvent);
                    }
                }
            } else stateManager.interactionState === InteractionState.scrolling && stateManager.updateInteractionState(InteractionState.default);
            if (!table.eventManager.isDraging) {
                const eventArgsSet = getCellEventArgsSet(e);
                if (!eventManager.isTouchMove && 0 === e.button && eventArgsSet.eventArgs && table.hasListeners(TABLE_EVENT_TYPE.CLICK_CELL)) {
                    const {col: col, row: row} = eventArgsSet.eventArgs, cellInfo = table.getCellInfo(col, row);
                    let icon, position;
                    if (null === (_c = eventArgsSet.eventArgs) || void 0 === _c ? void 0 : _c.target) {
                        const iconInfo = getIconAndPositionFromTarget(null === (_d = eventArgsSet.eventArgs) || void 0 === _d ? void 0 : _d.target);
                        iconInfo && (icon = iconInfo.icon, position = iconInfo.position);
                    }
                    const cellsEvent = Object.assign(Object.assign({}, cellInfo), {
                        event: e.nativeEvent,
                        federatedEvent: e,
                        cells: [],
                        targetIcon: icon ? {
                            name: icon.name,
                            position: position,
                            funcType: icon.attribute.funcType
                        } : void 0,
                        target: null === (_e = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _e ? void 0 : _e.target,
                        mergeCellInfo: null === (_f = eventArgsSet.eventArgs) || void 0 === _f ? void 0 : _f.mergeInfo
                    });
                    table.fireListeners(TABLE_EVENT_TYPE.CLICK_CELL, cellsEvent);
                }
            }
            if (table.hasListeners(TABLE_EVENT_TYPE.MOUSEUP_CELL)) {
                const eventArgsSet = getCellEventArgsSet(e);
                eventArgsSet.eventArgs && table.fireListeners(TABLE_EVENT_TYPE.MOUSEUP_CELL, {
                    col: eventArgsSet.eventArgs.col,
                    row: eventArgsSet.eventArgs.row,
                    event: e.nativeEvent,
                    target: null === (_g = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _g ? void 0 : _g.target,
                    mergeCellInfo: null === (_h = eventArgsSet.eventArgs) || void 0 === _h ? void 0 : _h.mergeInfo
                });
            }
            setTimeout((() => {
                eventManager.isDown = !1, eventManager.isTouchdown = !1, eventManager.isTouchMove = !1, 
                eventManager.isDraging = !1, eventManager.touchMovePoints = [];
            }), 0);
        }
    })), table.scenegraph.tableGroup.addEventListener("rightdown", (e => {
        var _a, _b, _c, _d, _e, _f, _g;
        const eventArgsSet = getCellEventArgsSet(e);
        if (eventArgsSet.eventArgs) {
            stateManager.triggerContextMenu(eventArgsSet.eventArgs.col, eventArgsSet.eventArgs.row, eventArgsSet.abstractPos.x, eventArgsSet.abstractPos.y);
            const {col: col, row: row} = eventArgsSet.eventArgs;
            if (col >= 0 && row >= 0) {
                const ranges = table.getSelectedCellRanges();
                let cellInRange = !1;
                if (ranges.length > 0) for (let i = 0; i < ranges.length; i++) {
                    const range = ranges[i], startCol = range.start.col, endCol = range.end.col, startRow = range.start.row, endRow = range.end.row;
                    if (col >= startCol && col <= endCol && row >= startRow && row <= endRow || col >= endCol && col <= startCol && row >= endRow && row <= startRow || col >= startCol && col <= endCol && row >= endRow && row <= startRow || col >= endCol && col <= startCol && row >= startRow && row <= endRow) {
                        cellInRange = !0;
                        break;
                    }
                }
                const disableSelectOnContextMenu = null === (_a = table.options.select) || void 0 === _a ? void 0 : _a.disableSelectOnContextMenu;
                if (cellInRange || disableSelectOnContextMenu || table.selectCell(col, row), table.hasListeners(TABLE_EVENT_TYPE.CONTEXTMENU_CELL)) {
                    const cellInfo = table.getCellInfo(col, row);
                    let icon, position;
                    if (null === (_b = eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.target) {
                        const iconInfo = getIconAndPositionFromTarget(null === (_c = eventArgsSet.eventArgs) || void 0 === _c ? void 0 : _c.target);
                        iconInfo && (icon = iconInfo.icon, position = iconInfo.position);
                    }
                    const cellsEvent = Object.assign(Object.assign({}, cellInfo), {
                        event: e.nativeEvent,
                        cells: [],
                        targetIcon: icon ? {
                            name: icon.name,
                            position: position,
                            funcType: icon.attribute.funcType
                        } : void 0,
                        target: null === (_d = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _d ? void 0 : _d.target,
                        mergeCellInfo: null === (_e = eventArgsSet.eventArgs) || void 0 === _e ? void 0 : _e.mergeInfo
                    });
                    (null === (_g = null === (_f = table.options.eventOptions) || void 0 === _f ? void 0 : _f.contextmenuReturnAllSelectedCells) || void 0 === _g || _g) && (cellInRanges(table.stateManager.select.ranges, col, row) ? cellsEvent.cells = table.getSelectedCellInfos() : cellsEvent.cells = [ [ cellInfo ] ]), 
                    table.fireListeners(TABLE_EVENT_TYPE.CONTEXTMENU_CELL, cellsEvent);
                }
            }
        }
    })), table.scenegraph.tableGroup.addEventListener("pointertap", (e => {
        var _a;
        if (table.stateManager.columnResize.resizing) return;
        const eventArgsSet = getCellEventArgsSet(e);
        if (!table.stateManager.columnResize.resizing && !table.stateManager.columnMove.moving && (eventManager.dealIconClick(e, eventArgsSet), 
        (null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) && eventManager.touchSetTimeout && "touch" === e.pointerType)) {
            const eventArgsSet = getCellEventArgsSet(e);
            if (eventManager.touchSetTimeout) {
                clearTimeout(eventManager.touchSetTimeout);
                const isHasSelected = !!(null === (_a = stateManager.select.ranges) || void 0 === _a ? void 0 : _a.length);
                eventManager.dealTableSelect(eventArgsSet), stateManager.endSelectCells(!0, isHasSelected), 
                eventManager.touchSetTimeout = void 0;
            }
        }
    })), table.scenegraph.stage.addEventListener("pointerdown", (e => {
        var _a, _b, _c, _d;
        const eventArgsSet = getCellEventArgsSet(e);
        (null === (_a = eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.target) && (null === (_b = eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.target) === (null === (_c = stateManager.residentHoverIcon) || void 0 === _c ? void 0 : _c.icon) || stateManager.hideMenu();
        let isCompleteEdit = !1;
        const target = e.target;
        target === table.scenegraph.tableGroup || target.isDescendantsOf(table.scenegraph.tableGroup) || (isCompleteEdit = null === (_d = table.editorManager) || void 0 === _d ? void 0 : _d.completeEdit(e.nativeEvent)), 
        getPromiseValue(isCompleteEdit, (isCompleteEdit => {
            var _a, _b;
            if (!(null === (_a = table.options.customConfig) || void 0 === _a ? void 0 : _a.selectCellWhenCellEditorNotExists) && !1 === isCompleteEdit) return;
            const hitIcon = (null === (_b = e.target.role) || void 0 === _b ? void 0 : _b.startsWith("icon")) ? e.target : void 0;
            if (eventManager.downIcon = hitIcon, "touch" === e.pointerType || hitIcon || eventManager.checkCellFillhandle(eventArgsSet) || stateManager.columnResize.resizing || !eventManager.checkColumnResize(eventArgsSet, !0)) ; else {
                table.scenegraph.updateChartState(null), stateManager.updateInteractionState(InteractionState.grabing);
                const {eventArgs: eventArgs} = eventArgsSet;
                if (!(null == eventArgs ? void 0 : eventArgs.targetCell)) {
                    table.getCellAt(eventArgsSet.abstractPos.x - table.theme.columnResize.resizeHotSpotSize / 2, eventArgsSet.abstractPos.y) && table.hasListeners(TABLE_EVENT_TYPE.MOUSEDOWN_TABLE) && table.fireListeners(TABLE_EVENT_TYPE.MOUSEDOWN_TABLE, {
                        event: e.nativeEvent
                    });
                }
            }
        }));
    })), table.scenegraph.stage.addEventListener("pointerup", (e => {
        "grabing" === stateManager.interactionState && (stateManager.updateInteractionState(InteractionState.default), 
        stateManager.isResizeCol() ? endResizeCol(table) : stateManager.isResizeRow() && endResizeRow(table));
    })), table.scenegraph.stage.addEventListener("pointertap", (e => {
        var _a, _b, _c, _d, _e;
        const target = e.target;
        if (table.eventManager.isDraging || !target || !target.isDescendantsOf(table.scenegraph.stage) && target.stage !== target || target.isDescendantsOf(table.scenegraph.tableGroup)) table.eventManager.isDraging && stateManager.isSelecting() && stateManager.endSelectCells(); else {
            stateManager.updateInteractionState(InteractionState.default), eventManager.dealTableHover();
            const isHasSelected = !!(null === (_a = stateManager.select.ranges) || void 0 === _a ? void 0 : _a.length);
            (null === (_b = table.options.customConfig) || void 0 === _b ? void 0 : _b.cancelSelectCellHook) ? (null === (_c = table.options.customConfig) || void 0 === _c ? void 0 : _c.cancelSelectCellHook(e)) && eventManager.dealTableSelect() : (null === (_e = null === (_d = table.options.select) || void 0 === _d ? void 0 : _d.blankAreaClickDeselect) || void 0 === _e || _e) && eventManager.dealTableSelect(), 
            stateManager.endSelectCells(!0, isHasSelected), stateManager.updateCursor(), table.scenegraph.updateChartState(null);
        }
    })), table.scenegraph.stage.addEventListener("pointermove", (e => {
        var _a, _b, _c;
        const eventArgsSet = getCellEventArgsSet(e);
        null !== (_c = null === (_b = null === (_a = e.target) || void 0 === _a ? void 0 : _a.isDescendantsOf) || void 0 === _b ? void 0 : _b.call(_a, table.scenegraph.tableGroup)) && void 0 !== _c && _c && (stateManager.isResizeCol() || eventManager.checkColumnResize(eventArgsSet) ? table.stateManager.select && eventManager.checkCellFillhandle(eventArgsSet) ? stateManager.updateCursor("crosshair") : stateManager.updateCursor("col-resize") : stateManager.isResizeRow() || eventManager.checkRowResize(eventArgsSet) ? table.stateManager.select && eventManager.checkCellFillhandle(eventArgsSet) ? stateManager.updateCursor("crosshair") : stateManager.updateCursor("row-resize") : stateManager.isMoveCol() || stateManager.updateCursor());
    })), table.scenegraph.tableGroup.addEventListener("checkbox_state_change", (e => {
        var _a, _b;
        const eventArgsSet = getCellEventArgsSet(e), {col: col, row: row} = eventArgsSet.eventArgs, cellInfo = table.getCellInfo(col, row), mergeRange = getCellMergeInfo(table, col, row);
        if (mergeRange) for (let col = mergeRange.start.col; col <= mergeRange.end.col; col++) for (let row = mergeRange.start.row; row <= mergeRange.end.row; row++) {
            table.scenegraph.getCell(col, row).forEachChildren((checkbox => {
                "checkbox" === checkbox.name && checkbox.setAttributes({
                    checked: e.target.attribute.checked,
                    indeterminate: e.target.attribute.indeterminate
                });
            }));
        }
        const cellsEvent = Object.assign(Object.assign({}, cellInfo), {
            event: e.nativeEvent,
            target: null === (_a = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.target,
            mergeCellInfo: null === (_b = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.mergeInfo,
            checked: e.detail.checked
        });
        if (table.isHeader(col, row)) {
            if (table.stateManager.setHeaderCheckedState(cellInfo.field, e.detail.checked), 
            table.internalProps.enableHeaderCheckboxCascade) {
                "checkbox" === table.getCellType(col, row) && table.scenegraph.updateCheckboxCellState(col, row, e.detail.checked);
            }
        } else if (table.stateManager.setCheckedState(col, row, cellInfo.field, e.detail.checked), 
        table.internalProps.enableCheckboxCascade) {
            if ("checkbox" === table.getCellType(col, row)) {
                const oldHeaderCheckedState = table.stateManager.headerCheckedState[cellInfo.field], newHeaderCheckedState = table.stateManager.updateHeaderCheckedState(cellInfo.field, col, row);
                oldHeaderCheckedState !== newHeaderCheckedState && table.scenegraph.updateHeaderCheckboxCellState(col, row, newHeaderCheckedState);
            }
        }
        table.fireListeners(TABLE_EVENT_TYPE.CHECKBOX_STATE_CHANGE, cellsEvent), table.scenegraph.updateNextFrame();
    })), table.scenegraph.tableGroup.addEventListener("radio_checked", (e => {
        var _a, _b, _c;
        const eventArgsSet = getCellEventArgsSet(e), {col: col, row: row, target: target} = eventArgsSet.eventArgs, cellInfo = table.getCellInfo(col, row), indexInCell = null === (_a = regIndexReg.exec(target.id)) || void 0 === _a ? void 0 : _a[1], mergeRange = getCellMergeInfo(table, col, row);
        if (mergeRange) for (let col = mergeRange.start.col; col <= mergeRange.end.col; col++) for (let row = mergeRange.start.row; row <= mergeRange.end.row; row++) {
            table.scenegraph.getCell(col, row).forEachChildren((radio => {
                "radio" === radio.name && radio.id === target.id && radio.setAttributes({
                    checked: !0
                });
            }));
        }
        const radioCheckType = table.getBodyColumnDefine(col, row).radioCheckType || "column";
        if ("cell" === radioCheckType) if (mergeRange) for (let col = mergeRange.start.col; col <= mergeRange.end.col; col++) for (let row = mergeRange.start.row; row <= mergeRange.end.row; row++) {
            table.scenegraph.getCell(col, row).forEachChildren((radio => {
                "radio" === radio.name && radio.id !== target.id && radio.setAttributes({
                    checked: !1
                });
            }));
        } else {
            table.scenegraph.getCell(col, row).forEachChildren((radio => {
                "radio" === radio.name && radio.id !== target.id && radio.setAttributes({
                    checked: !1
                });
            }));
        } else if ("column" === radioCheckType) {
            table.scenegraph.getColGroup(col).forEachChildren((cellGroup => {
                cellGroup.forEachChildren((radio => {
                    "radio" === radio.name && radio.id !== target.id && radio.setAttributes({
                        checked: !1
                    });
                }));
            }));
        }
        const radioIndexInCell = indexInCell ? Number(indexInCell) : void 0;
        table.stateManager.setRadioState(col, row, cellInfo.field, radioCheckType, radioIndexInCell);
        const cellsEvent = Object.assign(Object.assign({}, cellInfo), {
            event: e.nativeEvent,
            target: null === (_b = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.target,
            mergeCellInfo: null === (_c = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _c ? void 0 : _c.mergeInfo,
            radioIndexInCell: radioIndexInCell
        });
        table.fireListeners(TABLE_EVENT_TYPE.RADIO_STATE_CHANGE, cellsEvent), table.scenegraph.updateNextFrame();
    })), table.scenegraph.tableGroup.addEventListener("switch_state_change", (e => {
        var _a, _b;
        const eventArgsSet = getCellEventArgsSet(e), {col: col, row: row, target: target} = eventArgsSet.eventArgs, cellInfo = table.getCellInfo(col, row), mergeRange = getCellMergeInfo(table, col, row);
        if (mergeRange) for (let col = mergeRange.start.col; col <= mergeRange.end.col; col++) for (let row = mergeRange.start.row; row <= mergeRange.end.row; row++) {
            table.scenegraph.getCell(col, row).forEachChildren((switchComponent => {
                "switch" === switchComponent.name && switchComponent.setAttributes({
                    checked: e.target.attribute.checked
                });
            }));
        }
        const cellsEvent = Object.assign(Object.assign({}, cellInfo), {
            event: e.nativeEvent,
            target: null === (_a = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.target,
            mergeCellInfo: null === (_b = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _b ? void 0 : _b.mergeInfo,
            checked: e.detail.checked
        });
        table.stateManager.setCheckedState(col, row, cellInfo.field, e.detail.checked), 
        table.fireListeners(TABLE_EVENT_TYPE.SWITCH_STATE_CHANGE, cellsEvent), table.scenegraph.updateNextFrame();
    })), table.scenegraph.stage.addEventListener("wheel", (e => {
        var _a;
        e.path.find((node => "legend" === node.name)) || (null === (_a = table.editorManager) || void 0 === _a || _a.completeEdit(), 
        table.eventManager._enableTableScroll && handleWhell(e, stateManager));
    }));
}

export function bindGesture(eventManager) {
    const table = eventManager.table;
    eventManager.gesture = new Gesture(table.scenegraph.tableGroup, {
        tap: {
            interval: 300
        }
    }), eventManager.gesture.on("doubletap", (e => {
        dblclickHandler(e, table);
    }));
}

export function endResizeCol(table) {
    table.stateManager.endResizeCol();
    const columns = [];
    for (let col = 0; col < table.colCount; col++) columns.push(table.getColWidth(col));
    table.fireListeners(TABLE_EVENT_TYPE.RESIZE_COLUMN_END, {
        col: table.stateManager.columnResize.col,
        colWidths: columns
    });
}

export function endResizeRow(table) {
    table.stateManager.endResizeRow(), table.fireListeners(TABLE_EVENT_TYPE.RESIZE_ROW_END, {
        row: table.stateManager.rowResize.row,
        rowHeight: table.getRowHeight(table.stateManager.rowResize.row)
    });
}

function dblclickHandler(e, table) {
    var _a, _b, _c, _d, _e, _f;
    const eventArgsSet = getCellEventArgsSet(e);
    let col = -1, row = -1;
    eventArgsSet.eventArgs && (col = eventArgsSet.eventArgs.col, row = eventArgsSet.eventArgs.row);
    const value = table.getCellValue(col, row), bounds = null === (_b = null === (_a = eventArgsSet.eventArgs) || void 0 === _a ? void 0 : _a.targetCell) || void 0 === _b ? void 0 : _b.globalAABBBounds;
    if (bounds && table.internalProps.focusControl.setFocusRect(new Rect(bounds.x1 + table.scrollLeft, bounds.y1 + table.scrollTop, bounds.x2 - bounds.x1, bounds.y2 - bounds.y1), value), 
    table.hasListeners(TABLE_EVENT_TYPE.DBLCLICK_CELL)) {
        const cellInfo = table.getCellInfo(col, row);
        let icon, position;
        if (null === (_c = eventArgsSet.eventArgs) || void 0 === _c ? void 0 : _c.target) {
            const iconInfo = getIconAndPositionFromTarget(null === (_d = eventArgsSet.eventArgs) || void 0 === _d ? void 0 : _d.target);
            iconInfo && (icon = iconInfo.icon, position = iconInfo.position);
        }
        const cellsEvent = Object.assign(Object.assign({}, cellInfo), {
            event: e.nativeEvent,
            federatedEvent: e,
            cells: [],
            targetIcon: icon ? {
                name: icon.name,
                position: position,
                funcType: icon.attribute.funcType
            } : void 0,
            target: null === (_e = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _e ? void 0 : _e.target,
            mergeCellInfo: null === (_f = null == eventArgsSet ? void 0 : eventArgsSet.eventArgs) || void 0 === _f ? void 0 : _f.mergeInfo
        });
        table.fireListeners(TABLE_EVENT_TYPE.DBLCLICK_CELL, cellsEvent);
    }
}
//# sourceMappingURL=table-group.js.map
