export class SetPropertyCommand {
    target;
    path;
    after;
    name = "Set Property";
    before;
    constructor(target, path, after) {
        this.target = target;
        this.path = path;
        this.after = after;
        if (path.length === 0) {
            throw new Error("Property path cannot be empty");
        }
        this.before = this.read();
    }
    execute() {
        this.write(this.after);
    }
    undo() {
        this.write(this.before);
    }
    read() {
        let cursor = this.target;
        for (const segment of this.path) {
            if (cursor === null || typeof cursor !== "object" || !(segment in cursor)) {
                throw new Error(`Invalid property path: ${this.path.join(".")}`);
            }
            cursor = cursor[segment];
        }
        return cursor;
    }
    write(value) {
        let cursor = this.target;
        for (const segment of this.path.slice(0, -1)) {
            if (cursor === null || typeof cursor !== "object" || !(segment in cursor)) {
                throw new Error(`Invalid property path: ${this.path.join(".")}`);
            }
            cursor = cursor[segment];
        }
        cursor[this.path[this.path.length - 1]] = value;
    }
}
//# sourceMappingURL=SetPropertyCommand.js.map