export class DeleteNodeCommand {
    container;
    node;
    name = "Delete Node";
    sceneParent = null;
    sceneIndex = -1;
    constructor(container, node) {
        this.container = container;
        this.node = node;
    }
    execute() {
        const sceneNode = asSceneNode(this.node);
        if (sceneNode) {
            this.sceneParent = sceneNode.parent;
            this.sceneIndex = this.sceneParent?.children.indexOf(sceneNode) ?? -1;
            if (this.sceneParent && sceneNode.removeFromParent()) {
                return;
            }
            this.sceneParent = null;
            this.sceneIndex = -1;
        }
        this.container.remove(this.node);
    }
    undo() {
        const sceneNode = asSceneNode(this.node);
        if (sceneNode && this.sceneParent) {
            this.sceneParent.addChild(sceneNode);
            if (this.sceneIndex >= 0) {
                const currentIndex = this.sceneParent.children.indexOf(sceneNode);
                if (currentIndex >= 0 && currentIndex !== this.sceneIndex) {
                    this.sceneParent.children.splice(currentIndex, 1);
                    this.sceneParent.children.splice(Math.min(this.sceneIndex, this.sceneParent.children.length), 0, sceneNode);
                    sceneNode.markWorldDirty?.();
                }
            }
            return;
        }
        this.container.add(this.node);
    }
}
function asSceneNode(value) {
    if (!value || typeof value !== "object") {
        return undefined;
    }
    const candidate = value;
    if (Array.isArray(candidate.children) &&
        "parent" in candidate &&
        typeof candidate.addChild === "function" &&
        typeof candidate.removeChild === "function" &&
        typeof candidate.removeFromParent === "function") {
        return candidate;
    }
    return undefined;
}
//# sourceMappingURL=DeleteNodeCommand.js.map