export class AudioContextManager {
    options;
    contextRef;
    unlocked = false;
    constructor(options = {}) {
        this.options = options;
        this.contextRef = options.context;
    }
    get context() {
        if (!this.contextRef) {
            const createContext = this.options.createContext ?? (() => new AudioContext());
            this.contextRef = createContext();
        }
        return this.contextRef;
    }
    get state() {
        if (!this.contextRef) {
            return "locked";
        }
        if (this.contextRef.state === "running") {
            return "running";
        }
        if (this.contextRef.state === "closed") {
            return "closed";
        }
        return this.unlocked ? "suspended" : "locked";
    }
    async unlock() {
        if (this.state === "closed") {
            throw new Error("Cannot unlock a closed audio context");
        }
        await this.context.resume();
        this.unlocked = true;
    }
    async suspend() {
        if (this.state !== "closed") {
            await this.context.suspend();
        }
    }
    async resume() {
        await this.unlock();
    }
    async dispose() {
        if (this.contextRef && this.contextRef.state !== "closed") {
            await this.contextRef.close();
        }
    }
}
//# sourceMappingURL=AudioContextManager.js.map