import { AssetLoadError } from "./AssetLoader.js";
export class LoadContext {
    baseUrl;
    signal;
    dependencyChain;
    manager;
    constructor(options = {}) {
        this.baseUrl = options.baseUrl ?? "";
        this.signal = options.signal;
        this.dependencyChain = options.dependencyChain ?? [];
        this.manager = options.manager;
    }
    resolve(url) {
        if (/^(?:[a-z]+:)?\/\//i.test(url) || url.startsWith("data:") || url.startsWith("blob:")) {
            return url;
        }
        if (this.baseUrl.length === 0) {
            return url;
        }
        return new URL(url, this.baseUrl.endsWith("/") ? this.baseUrl : `${this.baseUrl}/`).toString();
    }
    throwIfAborted(url) {
        if (this.signal?.aborted) {
            throw new AssetLoadError("Asset load aborted", url, this.dependencyChain, this.signal.reason);
        }
    }
    child(url) {
        return new LoadContext({
            baseUrl: this.baseUrl,
            signal: this.signal,
            manager: this.manager,
            dependencyChain: [...this.dependencyChain, url]
        });
    }
    async loadDependency(url, type) {
        if (!this.manager) {
            throw new AssetLoadError("Cannot load dependency without an AssetManager", url, this.dependencyChain);
        }
        return this.manager.load(url, { type, signal: this.signal, dependencyChain: this.dependencyChain });
    }
}
//# sourceMappingURL=LoadContext.js.map