/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { m as matrix4 } from './bundle-DfdVaPrt.js';

class TransformController {
    constructor(stream) {
        this.stream = stream;
        this.isTransforming = false;
        this.currentDelta = matrix4.makeIdentity();
    }
    async dispose() {
        if (this.isTransforming) {
            this.endTransform();
        }
    }
    async beginTransform(delta = matrix4.makeIdentity()) {
        this.clearEndInteractionTimeout();
        if (!this.isTransforming) {
            this.currentDelta = delta;
            this.isTransforming = true;
            console.debug('Beginning transform interaction');
            await this.stream.beginInteraction({
                transform: {
                    delta: this.toDeltaTransform(delta),
                },
            });
        }
    }
    async updateTransform(delta) {
        this.currentDelta = delta;
        await this.stream.updateInteraction({
            transform: {
                delta: this.toDeltaTransform(this.currentDelta, true),
            },
        });
    }
    async updateTranslation(delta) {
        this.currentDelta = matrix4.makeTranslation(delta);
        await this.stream.updateInteraction({
            transform: {
                delta: this.toDeltaTransform(this.currentDelta),
            },
        });
    }
    getCurrentDelta() {
        return this.currentDelta;
    }
    async endTransform() {
        if (this.isTransforming) {
            await this.endInteraction();
        }
    }
    async endTransformDebounced(startCallback, endCallback) {
        if (this.isTransforming) {
            this.restartEndInteractionTimeout(startCallback, endCallback);
        }
    }
    clearTransform() {
        this.currentDelta = matrix4.makeIdentity();
        this.previousDelta = undefined;
        this.endTransform();
    }
    async EXPERIMENTAL_undoTransform() {
        if (this.previousDelta != null) {
            const undoDelta = matrix4.invert(this.previousDelta);
            console.debug(`Undo of previous transform [delta-applied=${undoDelta}]`);
            await this.beginTransform();
            await this.updateTransform(matrix4.invert(this.previousDelta));
            await this.endTransform();
            this.previousDelta = undefined;
            return undoDelta;
        }
        else {
            console.debug('No previous transform to undo.');
        }
    }
    async endInteraction() {
        console.debug(`Ending transform interaction [delta=${this.currentDelta}]`);
        this.clearEndInteractionTimeout();
        this.previousDelta = this.currentDelta;
        await this.stream.endInteraction({
            transform: {
                delta: this.toDeltaTransform(this.currentDelta),
            },
        });
        this.isTransforming = false;
        this.currentDelta = matrix4.makeIdentity();
    }
    restartEndInteractionTimeout(startCallback, endCallback) {
        this.clearEndInteractionTimeout();
        this.endDebounceTimeout = setTimeout(async () => {
            startCallback === null || startCallback === void 0 ? void 0 : startCallback();
            await this.endInteraction();
            endCallback === null || endCallback === void 0 ? void 0 : endCallback();
        }, 500);
    }
    clearEndInteractionTimeout() {
        if (this.endDebounceTimeout != null) {
            clearTimeout(this.endDebounceTimeout);
            this.endDebounceTimeout = undefined;
        }
    }
    toDeltaTransform(delta, columnMajor = false) {
        const asObject = matrix4.toObject(delta);
        // TODO: update this to pass a single order for the
        // transform matrix after work in https://vertexvis.atlassian.net/browse/PLAT-1582
        const basisX = columnMajor
            ? {
                x: asObject.m11,
                y: asObject.m21,
                z: asObject.m31,
            }
            : {
                x: asObject.m11,
                y: asObject.m12,
                z: asObject.m13,
            };
        const basisY = columnMajor
            ? {
                x: asObject.m12,
                y: asObject.m22,
                z: asObject.m32,
            }
            : {
                x: asObject.m21,
                y: asObject.m22,
                z: asObject.m23,
            };
        const basisZ = columnMajor
            ? {
                x: asObject.m13,
                y: asObject.m23,
                z: asObject.m33,
            }
            : {
                x: asObject.m31,
                y: asObject.m32,
                z: asObject.m33,
            };
        return {
            basisX,
            basisY,
            basisZ,
            xlate: {
                x: asObject.m14,
                y: asObject.m24,
                z: asObject.m34,
            },
            scale: asObject.m44,
        };
    }
}

export { TransformController as T };
//# sourceMappingURL=controller-Dos03RTp.js.map

//# sourceMappingURL=controller-Dos03RTp.js.map