/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { h } from "@stencil/core";
import { Matrix4, Vector3 } from "@vertexvis/geometry";
import { isVertexViewerDomElement } from "../viewer-dom-element/utils";
import { isVertexViewerDomGroup } from "../viewer-dom-group/utils";
export const Renderer2d = (_, children) => {
    return h("div", { class: "root-2d" }, children);
};
export function update2d(hostEl, parentWorldMatrix, viewport, camera, depthBuffer) {
    const elements = getElementDepths(hostEl, parentWorldMatrix, camera).sort((a, b) => a.distanceToCamera - b.distanceToCamera);
    for (let i = 0; i < elements.length; i++) {
        const { element, worldMatrix, worldPosition } = elements[i];
        const depthBufferIsNull = depthBuffer == null;
        const occluded = depthBufferIsNull ||
            (!element.occlusionOff &&
                (depthBuffer === null || depthBuffer === void 0 ? void 0 : depthBuffer.isOccluded(worldPosition, viewport)));
        const detached = depthBufferIsNull ||
            (!element.detachedOff &&
                (depthBuffer === null || depthBuffer === void 0 ? void 0 : depthBuffer.isDetached(worldPosition, viewport)));
        const screenPt = getScreenPosition(worldPosition, camera.projectionViewMatrix, viewport);
        updateTransform(element, screenPt);
        updateDepth(element, i, elements.length);
        update2d(element, worldMatrix, viewport, camera, depthBuffer);
        element.occluded = occluded !== null && occluded !== void 0 ? occluded : false;
        element.detached = detached !== null && detached !== void 0 ? detached : false;
        element.classList.add('ready');
    }
}
function getElementDepths(element, parentWorldMatrix, camera) {
    const results = [];
    for (let i = 0; i < element.children.length; i++) {
        const child = element.children[i];
        if (isVertexViewerDomGroup(child)) {
            const worldMatrix = Matrix4.multiply(parentWorldMatrix, child.matrix);
            results.push(...getElementDepths(child, worldMatrix, camera));
        }
        else if (isVertexViewerDomElement(child)) {
            const worldMatrix = Matrix4.multiply(parentWorldMatrix, child.matrix);
            const worldPosition = Vector3.fromMatrixPosition(worldMatrix);
            const distanceToCamera = Vector3.distanceSquared(camera.position, worldPosition);
            results.push({
                element: child,
                worldMatrix,
                worldPosition,
                distanceToCamera,
            });
        }
        else {
            results.push(...getElementDepths(child, parentWorldMatrix, camera));
        }
    }
    return results;
}
function updateTransform(element, relativePt) {
    element.style.transform = [
        `translate(-50%, -50%)`,
        `translate(${relativePt.x}px, ${relativePt.y}px)`,
    ].join(' ');
}
function updateDepth(element, index, elementCount) {
    element.style.zIndex = `${elementCount - index}`;
}
function getScreenPosition(pt, projectionViewMatrix, viewport) {
    const ndcPt = Vector3.transformMatrix(pt, projectionViewMatrix);
    return viewport.transformVectorToViewport(ndcPt);
}
//# sourceMappingURL=renderer2d.js.map
