import { matrixAllocate, transformPointForCanvas, mapToCanvasPointForCanvas, registerGlobalEventTransformer, registerWindowEventTransformer, vglobal } from "@visactor/vtable/es/vrender";

import { TABLE_EVENT_TYPE } from "@visactor/vtable";

export class RotateTablePlugin {
    constructor(pluginOptions) {
        var _a;
        this.id = "rotate-table", this.name = "Rotate Table", this.runTime = [ TABLE_EVENT_TYPE.INITIALIZED ], 
        this.id = null !== (_a = null == pluginOptions ? void 0 : pluginOptions.id) && void 0 !== _a ? _a : this.id;
    }
    run(...args) {
        const table = args[2];
        this.table = table, this.table.rotate90WithTransform = rotate90WithTransform.bind(this.table), 
        this.table.cancelTransform = cancelTransform.bind(this.table);
    }
    release() {}
}

export function rotate90WithTransform(rotateDom) {
    this.rotateDegree = 90;
    const rotateCenter = rotateDom.clientWidth < rotateDom.clientHeight ? Math.max(rotateDom.clientWidth, rotateDom.clientHeight) / 2 : Math.min(rotateDom.clientWidth, rotateDom.clientHeight) / 2, domRect = this.getElement().getBoundingClientRect(), x1 = domRect.left, y1 = domRect.top, x2 = domRect.right, y2 = domRect.bottom;
    rotateDom.style.transform = "rotate(90deg)", rotateDom.style.transformOrigin = `${rotateCenter}px ${rotateCenter}px`;
    const getRect = () => ({
        x1: x1,
        y1: y1,
        x2: x2,
        y2: y2
    }), getMatrix = () => {
        const viewPortWidth = ("undefined" != typeof window ? {
            width: window.innerWidth || document.documentElement.clientWidth,
            height: window.innerHeight || document.documentElement.clientHeight
        } : rotateDom.getBoundingClientRect()).width, domRect = this.getElement().getBoundingClientRect(), x1 = domRect.top, y1 = viewPortWidth - domRect.right, matrix = matrixAllocate.allocate(1, 0, 0, 1, 0, 0);
        matrix.translate(x1, y1);
        const centerX = rotateCenter - x1, centerY = rotateCenter - y1;
        matrix.translate(centerX, centerY), matrix.rotate(Math.PI / 2), matrix.translate(-centerX, -centerY);
        const rotateRablePlugin = this.pluginManager.getPluginByName("Rotate Table");
        return rotateRablePlugin && (rotateRablePlugin.matrix = matrix), matrix;
    };
    registerGlobalEventTransformer(vglobal, this.getElement(), getMatrix, getRect, transformPointForCanvas), 
    registerWindowEventTransformer(this.scenegraph.stage.window, this.getElement(), getMatrix, getRect, transformPointForCanvas);
    const rotateRablePlugin = this.pluginManager.getPluginByName("Rotate Table");
    rotateRablePlugin && (rotateRablePlugin.vglobal_mapToCanvasPoint = vglobal.mapToCanvasPoint), 
    vglobal.mapToCanvasPoint = mapToCanvasPointForCanvas;
}

export function cancelTransform(rotateDom) {
    this.rotateDegree = 0, rotateDom.style.transform = "none", rotateDom.style.transformOrigin = "none";
    const domRect = this.getElement().getBoundingClientRect(), x1 = domRect.left, y1 = domRect.top, x2 = domRect.right, y2 = domRect.bottom, getRect = () => ({
        x1: x1,
        y1: y1,
        x2: x2,
        y2: y2
    }), getMatrix = () => {
        const matrix = matrixAllocate.allocate(1, 0, 0, 1, 0, 0);
        return matrix.translate(x1, y1), matrix;
    };
    registerGlobalEventTransformer(vglobal, this.getElement(), getMatrix, getRect, transformPointForCanvas), 
    registerWindowEventTransformer(this.scenegraph.stage.window, this.getElement(), getMatrix, getRect, transformPointForCanvas);
    const rotateRablePlugin = this.pluginManager.getPluginByName("Rotate Table");
    rotateRablePlugin && (vglobal.mapToCanvasPoint = rotateRablePlugin.vglobal_mapToCanvasPoint);
}
//# sourceMappingURL=rotate-table.js.map