import { transfer } from 'comlink';
import { HoverDieOpacity } from '../types';
import { createMatrixRenderer } from '../modules/create-matrix-renderer';
/**
 * Responsible for drawing the dies inside the wafer map, adding dieText and scaling the canvas
 */
export class WorkerRenderer {
    constructor(wafermap) {
        this.wafermap = wafermap;
        this.minDieDim = 100;
    }
    async setupWafer(snapshot) {
        if (this.matrixRenderer === undefined) {
            const { matrixRenderer } = await createMatrixRenderer();
            this.matrixRenderer = matrixRenderer;
            const offscreenCanvas = this.wafermap.workerCanvas.transferControlToOffscreen();
            await this.matrixRenderer.setCanvas(transfer(offscreenCanvas, [offscreenCanvas]));
        }
        await this.matrixRenderer.setCanvasDimensions(snapshot.canvasDimensions);
        await this.matrixRenderer.setRenderConfig(snapshot.renderConfig);
        await this.matrixRenderer.setMatrixData(snapshot.columnIndices, snapshot.rowIndices, snapshot.values);
    }
    async drawWafer(snapshot) {
        const topLeftCanvasCorner = snapshot.transform.invert([0, 0]);
        const bottomRightCanvasCorner = snapshot.transform.invert([
            snapshot.canvasDimensions.width,
            snapshot.canvasDimensions.height
        ]);
        await this.matrixRenderer.setTransformConfig({
            transform: snapshot.transform,
            topLeftCanvasCorner: {
                x: topLeftCanvasCorner[0] - snapshot.dieDimensions.width,
                y: topLeftCanvasCorner[1] - snapshot.dieDimensions.height
            },
            bottomRightCanvasCorner: {
                x: bottomRightCanvasCorner[0],
                y: bottomRightCanvasCorner[1]
            }
        });
        await this.matrixRenderer.drawWafer();
        if (!snapshot.dieLabelsHidden
            && snapshot.dieDimensions.width
                * snapshot.dieDimensions.height
                * (snapshot.transform.k !== 0 ? snapshot.transform.k : 1)
                >= this.minDieDim) {
            await this.matrixRenderer.drawText();
        }
    }
    renderHover() {
        if (this.wafermap.computations.dieDimensions === undefined
            || this.wafermap.transform === undefined) {
            return;
        }
        this.wafermap.hoverWidth = this.wafermap.computations.dieDimensions.width
            * this.wafermap.transform.k;
        this.wafermap.hoverHeight = this.wafermap.computations.dieDimensions.height
            * this.wafermap.transform.k;
        this.wafermap.hoverOpacity = this.wafermap.hoverDie === undefined
            ? HoverDieOpacity.hide
            : HoverDieOpacity.show;
        this.wafermap.hoverTransform = this.calculateHoverTransform();
    }
    calculateHoverTransform() {
        if (this.wafermap.hoverDie !== undefined) {
            const scaledX = this.wafermap.computations.horizontalScale(this.wafermap.hoverDie.x);
            if (scaledX === undefined) {
                return '';
            }
            const scaledY = this.wafermap.computations.verticalScale(this.wafermap.hoverDie.y);
            if (scaledY === undefined) {
                return '';
            }
            const transformedPoint = this.wafermap.transform.apply([
                scaledX + this.wafermap.computations.margin.left,
                scaledY + this.wafermap.computations.margin.top
            ]);
            return `translate(${transformedPoint[0]}, ${transformedPoint[1]})`;
        }
        return '';
    }
}
//# sourceMappingURL=worker-renderer.js.map