function cloneTransform(transform) {
    return {
        position: { ...transform.position },
        rotation: transform.rotation ? { ...transform.rotation } : undefined,
        scale: transform.scale ? { ...transform.scale } : undefined
    };
}
function readTransform(target) {
    if ("transform" in target) {
        return {
            position: {
                x: target.transform.position[0],
                y: target.transform.position[1],
                z: target.transform.position[2]
            },
            rotation: {
                x: target.transform.rotation[0],
                y: target.transform.rotation[1],
                z: target.transform.rotation[2],
                w: target.transform.rotation[3]
            },
            scale: {
                x: target.transform.scale[0],
                y: target.transform.scale[1],
                z: target.transform.scale[2]
            }
        };
    }
    return cloneTransform(target);
}
function applyTransform(target, transform) {
    if ("transform" in target) {
        target.transform.setPosition(transform.position.x, transform.position.y, transform.position.z);
        if (transform.rotation) {
            target.transform.setRotation(transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w ?? 1);
        }
        if (transform.scale) {
            target.transform.setScale(transform.scale.x, transform.scale.y, transform.scale.z);
        }
        return;
    }
    target.position = { ...transform.position };
    target.rotation = transform.rotation ? { ...transform.rotation } : undefined;
    target.scale = transform.scale ? { ...transform.scale } : undefined;
}
export class TransformCommand {
    target;
    name = "Transform";
    before;
    after;
    constructor(target, next, before) {
        this.target = target;
        this.before = cloneTransform(before ?? readTransform(target));
        this.after = cloneTransform(next);
    }
    execute() {
        applyTransform(this.target, this.after);
    }
    undo() {
        applyTransform(this.target, this.before);
    }
    canMerge(next) {
        return next instanceof TransformCommand && next.target === this.target;
    }
    merge(next) {
        if (!(next instanceof TransformCommand) || next.target !== this.target) {
            throw new Error("TransformCommand can only merge commands for the same target.");
        }
        return new TransformCommand(this.target, next.after, this.before);
    }
}
//# sourceMappingURL=TransformCommand.js.map