import { WaferMapOriginLocation } from '../types';
/**
 * HoverHandler deals with user interactions and events like hovering
 */
export class HoverHandler {
    constructor(wafermap) {
        this.wafermap = wafermap;
        this.onMouseMove = (event) => {
            if (this.wafermap.isExperimentalUpdate()) {
                return;
            }
            const mousePosition = {
                x: event.offsetX,
                y: event.offsetY
            };
            if (!this.hoversOverDie(mousePosition)) {
                this.wafermap.hoverDie = undefined;
                return;
            }
            // get original mouse position in case we are in zoom.
            const invertedPoint = this.wafermap.transform.invert([
                mousePosition.x,
                mousePosition.y
            ]);
            const dieCoordinates = this.calculateDieCoordinates({
                x: invertedPoint[0],
                y: invertedPoint[1]
            });
            if (dieCoordinates === undefined) {
                this.wafermap.hoverDie = undefined;
                return;
            }
            this.wafermap.hoverDie = this.wafermap.dataManager.getWaferMapDie(dieCoordinates);
        };
        this.onMouseOut = (_event) => {
            this.wafermap.hoverDie = undefined;
        };
    }
    /**
     * @internal
     */
    connect() {
        this.wafermap.addEventListener('mousemove', this.onMouseMove);
        this.wafermap.addEventListener('mouseout', this.onMouseOut);
    }
    /**
     * @internal
     */
    disconnect() {
        this.wafermap.removeEventListener('mousemove', this.onMouseMove);
        this.wafermap.removeEventListener('mouseout', this.onMouseOut);
    }
    calculateDieCoordinates(mousePosition) {
        if (!this.wafermap.isExperimentalUpdate()) {
            const originLocation = this.wafermap.originLocation;
            const xRoundFunction = originLocation === WaferMapOriginLocation.bottomLeft
                || originLocation === WaferMapOriginLocation.topLeft
                ? Math.floor
                : Math.ceil;
            const yRoundFunction = originLocation === WaferMapOriginLocation.bottomLeft
                || originLocation === WaferMapOriginLocation.bottomRight
                ? Math.floor
                : Math.ceil;
            // go to x and y scale to get the x,y values of the die.
            const x = xRoundFunction(this.wafermap.dataManager.invertedHorizontalScale(mousePosition.x - this.wafermap.dataManager.margin.left));
            const y = yRoundFunction(this.wafermap.dataManager.invertedVerticalScale(mousePosition.y - this.wafermap.dataManager.margin.top));
            return { x, y };
        }
        return undefined;
    }
    hoversOverDie(mousePosition) {
        const rgba = this.wafermap.canvasContext.getImageData(mousePosition.x, mousePosition.y, 1, 1).data;
        let rgbaSum = 0;
        for (const color of rgba) {
            rgbaSum += color;
        }
        return rgbaSum > 0;
    }
}
//# sourceMappingURL=hover-handler.js.map