import { ScriptContext } from "./ScriptContext.js";
export class BehaviorHost {
    target;
    behaviors = new Set();
    destroyed = false;
    constructor(options = {}) {
        this.target = options.target;
    }
    get isDestroyed() {
        return this.destroyed;
    }
    attach(behavior) {
        this.assertAlive();
        this.behaviors.add(behavior);
    }
    detach(behavior) {
        return this.behaviors.delete(behavior);
    }
    list() {
        return [...this.behaviors];
    }
    async destroy(services) {
        if (this.destroyed) {
            return;
        }
        this.destroyed = true;
        const context = new ScriptContext({ phase: "destroy", target: this.target, services });
        for (const behavior of [...this.behaviors]) {
            await behavior.onDestroy?.(context);
        }
        this.behaviors.clear();
    }
    assertAlive() {
        if (this.destroyed) {
            throw new Error("Cannot attach behavior to a destroyed host");
        }
    }
}
//# sourceMappingURL=BehaviorHost.js.map