"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.cancelTransform = exports.rotate90WithTransform = exports.RotateTablePlugin = void 0;

const vtable_1 = require("@visactor/vtable"), vtable_2 = require("@visactor/vtable");

class RotateTablePlugin {
    constructor(pluginOptions) {
        var _a;
        this.id = "rotate-table", this.name = "Rotate Table", this.runTime = [ vtable_2.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() {}
}

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 = vtable_1.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;
    };
    (0, vtable_1.registerGlobalEventTransformer)(vtable_1.vglobal, this.getElement(), getMatrix, getRect, vtable_1.transformPointForCanvas), 
    (0, vtable_1.registerWindowEventTransformer)(this.scenegraph.stage.window, this.getElement(), getMatrix, getRect, vtable_1.transformPointForCanvas);
    const rotateRablePlugin = this.pluginManager.getPluginByName("Rotate Table");
    rotateRablePlugin && (rotateRablePlugin.vglobal_mapToCanvasPoint = vtable_1.vglobal.mapToCanvasPoint), 
    vtable_1.vglobal.mapToCanvasPoint = vtable_1.mapToCanvasPointForCanvas;
}

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 = vtable_1.matrixAllocate.allocate(1, 0, 0, 1, 0, 0);
        return matrix.translate(x1, y1), matrix;
    };
    (0, vtable_1.registerGlobalEventTransformer)(vtable_1.vglobal, this.getElement(), getMatrix, getRect, vtable_1.transformPointForCanvas), 
    (0, vtable_1.registerWindowEventTransformer)(this.scenegraph.stage.window, this.getElement(), getMatrix, getRect, vtable_1.transformPointForCanvas);
    const rotateRablePlugin = this.pluginManager.getPluginByName("Rotate Table");
    rotateRablePlugin && (vtable_1.vglobal.mapToCanvasPoint = rotateRablePlugin.vglobal_mapToCanvasPoint);
}

exports.RotateTablePlugin = RotateTablePlugin, exports.rotate90WithTransform = rotate90WithTransform, 
exports.cancelTransform = cancelTransform;