"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../../../utils");
var edge_1 = require("./common/edge");
var edge_rectangle_vert_glsl_1 = __importDefault(require("../shaders/edge.rectangle.vert.glsl.js"));
var edge_rectangle_frag_glsl_1 = __importDefault(require("../shaders/edge.rectangle.frag.glsl.js"));
var UNSIGNED_BYTE = WebGLRenderingContext.UNSIGNED_BYTE, FLOAT = WebGLRenderingContext.FLOAT;
var UNIFORMS = ["u_matrix", "u_zoomRatio", "u_sizeRatio", "u_correctionRatio"];
var EdgeRectangleProgram = /** @class */ (function (_super) {
    __extends(EdgeRectangleProgram, _super);
    function EdgeRectangleProgram() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    EdgeRectangleProgram.prototype.getDefinition = function () {
        return {
            VERTICES: 6,
            VERTEX_SHADER_SOURCE: edge_rectangle_vert_glsl_1.default,
            FRAGMENT_SHADER_SOURCE: edge_rectangle_frag_glsl_1.default,
            METHOD: WebGLRenderingContext.TRIANGLES,
            UNIFORMS: UNIFORMS,
            ATTRIBUTES: [
                { name: "a_positionStart", size: 2, type: FLOAT },
                { name: "a_positionEnd", size: 2, type: FLOAT },
                { name: "a_normal", size: 2, type: FLOAT },
                { name: "a_color", size: 4, type: UNSIGNED_BYTE, normalized: true },
                { name: "a_id", size: 4, type: UNSIGNED_BYTE, normalized: true },
            ],
            CONSTANT_ATTRIBUTES: [
                // If 0, then position will be a_positionStart
                // If 2, then position will be a_positionEnd
                { name: "a_positionCoef", size: 1, type: FLOAT },
                { name: "a_normalCoef", size: 1, type: FLOAT },
            ],
            CONSTANT_DATA: [
                [0, 1],
                [0, -1],
                [1, 1],
                [1, 1],
                [0, -1],
                [1, -1],
            ],
        };
    };
    EdgeRectangleProgram.prototype.processVisibleItem = function (edgeIndex, startIndex, sourceData, targetData, data) {
        var thickness = data.size || 1;
        var x1 = sourceData.x;
        var y1 = sourceData.y;
        var x2 = targetData.x;
        var y2 = targetData.y;
        var color = (0, utils_1.floatColor)(data.color);
        // Computing normals
        var dx = x2 - x1;
        var dy = y2 - y1;
        var len = dx * dx + dy * dy;
        var n1 = 0;
        var n2 = 0;
        if (len) {
            len = 1 / Math.sqrt(len);
            n1 = -dy * len * thickness;
            n2 = dx * len * thickness;
        }
        var array = this.array;
        array[startIndex++] = x1;
        array[startIndex++] = y1;
        array[startIndex++] = x2;
        array[startIndex++] = y2;
        array[startIndex++] = n1;
        array[startIndex++] = n2;
        array[startIndex++] = color;
        array[startIndex++] = edgeIndex;
    };
    EdgeRectangleProgram.prototype.setUniforms = function (params, _a) {
        var gl = _a.gl, uniformLocations = _a.uniformLocations, isPicking = _a.isPicking;
        var u_matrix = uniformLocations.u_matrix, u_zoomRatio = uniformLocations.u_zoomRatio, u_correctionRatio = uniformLocations.u_correctionRatio, u_sizeRatio = uniformLocations.u_sizeRatio;
        gl.uniformMatrix3fv(u_matrix, false, params.matrix);
        gl.uniform1f(u_zoomRatio, params.zoomRatio);
        gl.uniform1f(u_sizeRatio, isPicking ? params.downSizedSizeRatio : params.sizeRatio);
        gl.uniform1f(u_correctionRatio, params.correctionRatio);
    };
    return EdgeRectangleProgram;
}(edge_1.EdgeProgram));
exports.default = EdgeRectangleProgram;
