import BoundedValue from "../../core/model/BoundedValue.js";
import { AssetLoadState } from "./AssetLoadState.js";

export class PendingAsset {
    /**
     *
     * @param {AssetDescription} description
     * @constructor
     */
    constructor(description) {
        /**
         *
         * @type {AssetDescription}
         */
        this.description = description;

        /**
         *
         * @type {AssetRequest[]}
         */
        this.requests = [];

        /**
         *
         * @type {BoundedValue}
         */
        this.progress = new BoundedValue(0, 0);

        /**
         *
         * @type {AssetLoadState|number}
         */
        this.state = AssetLoadState.Initial;
    }

    /**
     * Returns aggregated priority of the pending asset based on highest priority of associated requests
     * @return {number}
     */
    get priority() {
        const requests = this.requests;
        const request_count = requests.length;

        if (request_count === 0) {
            return 0;
        }

        let max_priority = requests[0].priority;

        for (let i = 1; i < request_count; i++) {
            const request = requests[i];
            const priority = request.scoped_priority;

            if (priority > max_priority) {
                max_priority = request;
            }
        }

        return max_priority;
    }
}
