import { Notifier, Option, Terminable, UUID } from "@opendaw/lib-std";
export class DefaultSampleLoader {
    #uuid;
    #notifier;
    #meta = Option.None;
    #data = Option.None;
    #peaks = Option.None;
    #state = { type: "progress", progress: 0.0 };
    constructor(uuid) {
        this.#uuid = uuid;
        this.#notifier = new Notifier();
    }
    subscribe(observer) {
        if (this.#state.type === "loaded" || this.#state.type === "error") {
            observer(this.#state);
            return Terminable.Empty;
        }
        return this.#notifier.subscribe(observer);
    }
    get uuid() { return this.#uuid; }
    get data() { return this.#data; }
    get meta() { return this.#meta; }
    get peaks() { return this.#peaks; }
    get state() { return this.#state; }
    setLoaded(data, peaks, meta) {
        this.#data = Option.wrap(data);
        this.#peaks = Option.wrap(peaks);
        this.#meta = Option.wrap(meta);
        this.#state = { type: "loaded" };
        this.#notifier.notify(this.#state);
    }
    setProgress(progress) {
        this.#state = { type: "progress", progress };
        this.#notifier.notify(this.#state);
    }
    setError(reason) {
        this.#state = { type: "error", reason };
        this.#notifier.notify(this.#state);
    }
    invalidate() {
        this.#state = { type: "progress", progress: 0.0 };
        this.#meta = Option.None;
        this.#data = Option.None;
        this.#peaks = Option.None;
        this.#notifier.notify(this.#state);
    }
    toString() { return `{DefaultSampleLoader ${UUID.toString(this.#uuid)}}`; }
}
