import { vglobal } from "@visactor/vrender-core";

import { SeriesNumberCellStateValue, SeriesNumberEvent } from "./type";

export class TableSeriesNumberEventManager {
    constructor(tableSeriesNumber) {
        this.isPointerDownStartSelect = !1, this._onRightDown = e => {
            const target = e.target;
            this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellRightClick, {
                seriesNumberCell: target,
                event: e
            });
        }, this._onPointermove = e => {
            const target = e.target;
            if (this.isPointerDownStartSelect) {
                if (this._tableSeriesNumber.interactionState.selectIndexs.has(target.name)) {
                    if (this._tableSeriesNumber.interactionState._lastClickItem && this._tableSeriesNumber.interactionState._lastClickItem.id === target.id) return;
                    this._tableSeriesNumber.renderSelectedIndexsState(), this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellClick, {
                        seriesNumberCell: target,
                        event: e,
                        isDragSelect: !0
                    });
                } else this._tableSeriesNumber.interactionState.selectIndexs.add(target.name), this._tableSeriesNumber.renderSelectedIndexsState(), 
                this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellClick, {
                    seriesNumberCell: target,
                    event: e,
                    isDragSelect: !0
                });
                this._tableSeriesNumber.interactionState._lastClickItem = target;
            } else {
                if (target.name.startsWith("col")) {
                    Number(target.name.split("-")[1]);
                    const canvasPointXY = target.stage.window.pointTransform(e.canvasX, e.canvasY), XYtoTarget = {
                        x: 0,
                        y: 0
                    };
                    target.globalTransMatrix.transformPoint(canvasPointXY, XYtoTarget), XYtoTarget.x <= 4 || XYtoTarget.x <= target.getAttributes().width && XYtoTarget.x >= target.getAttributes().width - 4 ? target.setAttribute("cursor", "col-resize") : target.setAttribute("cursor", "default");
                } else if (target.name.startsWith("row")) {
                    Number(target.name.split("-")[1]);
                    const canvasPointXY = target.stage.window.pointTransform(e.canvasX, e.canvasY), XYtoTarget = {
                        x: 0,
                        y: 0
                    };
                    target.globalTransMatrix.transformPoint(canvasPointXY, XYtoTarget), XYtoTarget.y <= 4 || XYtoTarget.y <= target.getAttributes().height && XYtoTarget.y >= target.getAttributes().height - 4 ? target.setAttribute("cursor", "row-resize") : target.setAttribute("cursor", "default");
                }
                if (this._tableSeriesNumber.interactionState._lastHoverItem) {
                    if (this._tableSeriesNumber.interactionState._lastHoverItem.id === target.id) return;
                    this._unHoverHandler(this._tableSeriesNumber.interactionState._lastHoverItem, e);
                }
                this._tableSeriesNumber.getAttributes().hover && this._hoverHandler(target, e);
            }
        }, this._onPointerleave = e => {
            this._tableSeriesNumber.interactionState._lastHoverItem && (this._unHoverHandler(this._tableSeriesNumber.interactionState._lastHoverItem, e), 
            this._tableSeriesNumber.interactionState._lastHoverItem = null);
        }, this._onPointerdown = e => {
            var _a;
            if (2 === e.button) return;
            const target = e.target;
            if (target.name.startsWith("col")) {
                const colIndex = Number(target.name.split("-")[1]), canvasPointXY = target.stage.window.pointTransform(e.canvasX, e.canvasY), XYtoTarget = {
                    x: 0,
                    y: 0
                };
                if (target.globalTransMatrix.transformPoint(canvasPointXY, XYtoTarget), XYtoTarget.x <= 4 || XYtoTarget.x <= target.getAttributes().width && XYtoTarget.x >= target.getAttributes().width - 4) {
                    let resizeTargetColIndex = colIndex;
                    return XYtoTarget.x <= 4 && (resizeTargetColIndex = colIndex - 1), void this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.resizeColWidthStart, {
                        colIndex: resizeTargetColIndex,
                        event: e
                    });
                }
            } else if (target.name.startsWith("row")) {
                const rowIndex = Number(target.name.split("-")[1]), canvasPointXY = target.stage.window.pointTransform(e.canvasX, e.canvasY), XYtoTarget = {
                    x: 0,
                    y: 0
                };
                if (target.globalTransMatrix.transformPoint(canvasPointXY, XYtoTarget), XYtoTarget.y <= 4 || XYtoTarget.y <= target.getAttributes().height && XYtoTarget.y >= target.getAttributes().height - 4) {
                    let resizeTargetRowIndex = rowIndex;
                    return XYtoTarget.y <= 4 && (resizeTargetRowIndex = rowIndex - 1), void this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.resizeRowHeightStart, {
                        rowIndex: resizeTargetRowIndex,
                        event: e
                    });
                }
            }
            if (this.isPointerDownStartSelect = !0, null === (_a = this._tableSeriesNumber.interactionState.selectIndexs) || void 0 === _a ? void 0 : _a.size) if (this._tableSeriesNumber.interactionState.selectIndexs.has(target.name)) if (e.nativeEvent.ctrlKey || e.nativeEvent.metaKey) this._tableSeriesNumber.removeOneGroupSelected(target), 
            this._unClickHandler(target.name, e); else {
                this._tableSeriesNumber.removeAllSelectedIndexs();
                for (const name of this._tableSeriesNumber.interactionState.selectIndexs) this._unClickHandler(name, e);
            } else if (e.nativeEvent.ctrlKey || e.nativeEvent.metaKey) ; else {
                this._tableSeriesNumber.removeAllSelectedIndexs();
                for (const name of this._tableSeriesNumber.interactionState.selectIndexs) (target.name.startsWith("row") && name.startsWith("row") || target.name.startsWith("col") && name.startsWith("col")) && this._unClickHandler(name, e);
            }
            this._clickHandler(target, e);
        }, this._onPointerup = e => {
            const target = e.target;
            this.isPointerDownStartSelect && (this.isPointerDownStartSelect = !1, this._tableSeriesNumber.interactionState._lastClickItem = null, 
            this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellClickUp, {
                seriesNumberCell: target,
                event: e
            }));
        }, this._tableSeriesNumber = tableSeriesNumber;
    }
    bindEvents() {
        const {hover: hover = !0, select: select = !0} = this._tableSeriesNumber.attribute;
        hover && (this._tableSeriesNumber._rowSeriesNumberGroup.addEventListener("pointermove", this._onPointermove), 
        this._tableSeriesNumber._rowSeriesNumberGroup.addEventListener("pointerleave", this._onPointerleave), 
        this._tableSeriesNumber._colSeriesNumberGroup.addEventListener("pointermove", this._onPointermove), 
        this._tableSeriesNumber._colSeriesNumberGroup.addEventListener("pointerleave", this._onPointerleave), 
        this._tableSeriesNumber._cornerGroup.addEventListener("pointermove", this._onPointermove), 
        this._tableSeriesNumber._cornerGroup.addEventListener("pointerleave", this._onPointerleave), 
        this._tableSeriesNumber._frozenTopRowSeriesNumberGroup.addEventListener("pointermove", this._onPointermove), 
        this._tableSeriesNumber._frozenTopRowSeriesNumberGroup.addEventListener("pointerleave", this._onPointerleave), 
        this._tableSeriesNumber._frozenLeftColSeriesNumberGroup.addEventListener("pointermove", this._onPointermove), 
        this._tableSeriesNumber._frozenLeftColSeriesNumberGroup.addEventListener("pointerleave", this._onPointerleave)), 
        select && (this._tableSeriesNumber._rowSeriesNumberGroup.addEventListener("pointerdown", this._onPointerdown), 
        this._tableSeriesNumber._colSeriesNumberGroup.addEventListener("pointerdown", this._onPointerdown), 
        this._tableSeriesNumber._cornerGroup.addEventListener("pointerdown", this._onPointerdown), 
        this._tableSeriesNumber._frozenTopRowSeriesNumberGroup.addEventListener("pointerdown", this._onPointerdown), 
        this._tableSeriesNumber._frozenLeftColSeriesNumberGroup.addEventListener("pointerdown", this._onPointerdown), 
        vglobal.addEventListener("pointerup", this._onPointerup)), this._tableSeriesNumber._rowSeriesNumberGroup.addEventListener("rightdown", this._onRightDown), 
        this._tableSeriesNumber._colSeriesNumberGroup.addEventListener("rightdown", this._onRightDown), 
        this._tableSeriesNumber._cornerGroup.addEventListener("rightdown", this._onRightDown), 
        this._tableSeriesNumber._frozenTopRowSeriesNumberGroup.addEventListener("rightdown", this._onRightDown), 
        this._tableSeriesNumber._frozenLeftColSeriesNumberGroup.addEventListener("rightdown", this._onRightDown);
    }
    _hoverHandler(seriesNumberCell, e) {
        this._tableSeriesNumber.interactionState._lastHoverItem = seriesNumberCell, seriesNumberCell.hasState(SeriesNumberCellStateValue.select) ? seriesNumberCell.useStates([ SeriesNumberCellStateValue.select, SeriesNumberCellStateValue.hover ]) : seriesNumberCell.useStates([ SeriesNumberCellStateValue.hover ]), 
        this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellHover, {
            seriesNumberCell: seriesNumberCell,
            event: e
        });
    }
    _unHoverHandler(seriesNumberCell, e) {
        seriesNumberCell.removeState(SeriesNumberCellStateValue.hover), this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellUnHover, {
            seriesNumberCell: seriesNumberCell,
            event: e
        });
    }
    _clickHandler(seriesNumberCell, e) {
        this._tableSeriesNumber.addOneGroupSelected(seriesNumberCell), this._tableSeriesNumber.interactionState._lastClickItem = seriesNumberCell, 
        this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellClick, {
            seriesNumberCell: seriesNumberCell,
            event: e
        });
    }
    _unClickHandler(seriesNumberIndex, e) {
        const seriesNumberCell = seriesNumberIndex.startsWith("row") ? this._tableSeriesNumber.getRowSeriesNumberCellGroup(Number(seriesNumberIndex.split("-")[1])) : this._tableSeriesNumber.getColSeriesNumberCellGroup(Number(seriesNumberIndex.split("-")[1]));
        null == seriesNumberCell || seriesNumberCell.removeState(SeriesNumberCellStateValue.select), 
        this._tableSeriesNumber.dispatchTableSeriesNumberEvent(SeriesNumberEvent.seriesNumberCellCancelClick, {
            index: seriesNumberIndex,
            event: e
        });
    }
}
//# sourceMappingURL=event-manager.js.map
