export class ReparentNodeCommand {
    node;
    newParent;
    name = "Reparent Node";
    oldParent;
    oldIndex;
    newIndex;
    constructor(node, newParent, newIndex) {
        this.node = node;
        this.newParent = newParent;
        this.oldParent = node.parent;
        this.oldIndex = this.oldParent?.children.indexOf(node) ?? -1;
        this.newIndex = newIndex ?? newParent.children.length;
    }
    execute() {
        this.move(this.newParent, this.newIndex);
    }
    undo() {
        if (!this.oldParent) {
            this.node.parent?.removeChild(this.node);
            return;
        }
        this.move(this.oldParent, this.oldIndex);
    }
    move(parent, index) {
        if (this.node.parent !== parent) {
            this.node.parent?.removeChild(this.node);
            parent.addChild(this.node);
        }
        const currentIndex = parent.children.indexOf(this.node);
        const targetIndex = Math.max(0, Math.min(index, parent.children.length - 1));
        if (currentIndex >= 0 && currentIndex !== targetIndex) {
            parent.children.splice(currentIndex, 1);
            parent.children.splice(targetIndex, 0, this.node);
            this.node.markWorldDirty?.();
        }
    }
}
//# sourceMappingURL=ReparentNodeCommand.js.map