let id_counter = 0;

export class AssetRequestScope {
    id = id_counter++;

    priority = 1

    /**
     *
     * @type {AssetRequestScope[]}
     */
    sources = []


    get final_priority() {
        const sources = this.sources;
        const n = sources.length;

        if (n === 0) {
            return this.priority;
        }


        let max = sources[0].final_priority;

        for (let i = 0; i < n; i++) {
            const source = sources[i];

            const source_priority = source.final_priority;

            if (source_priority > max) {
                max = source_priority;
            }
        }

        return this.priority * max;
    }

    /**
     *
     * @param {AssetRequestScope[]} sources
     * @return {AssetRequestScope}
     */
    static from(sources) {
        const r = new AssetRequestScope();

        r.sources = sources;

        return r;
    }

    toString() {
        const sources = this.sources.length > 0 ? `[ ${this.sources.join(', ')} ]` : 'none';
        return `Scope{ id:${this.id}, sources:${sources} }`;
    }

}


/**
 * @readonly
 * @type {Readonly<AssetRequestScope>}
 */
AssetRequestScope.GLOBAL = Object.freeze(new AssetRequestScope());
