import Serializable from "../../../util/Serializable.js";
import UnitHelper from "../../util/UnitHelper.js";

export default class AssetDataCollectionConfig extends Serializable{
    /**
     * @type {number[]}
     */
    #timePeriods = [];

    /**
     * @param {string[]|number[]|null} timePeriods 
     */
    constructor(timePeriods = null) {
        super();
        this.setTimePeriods(timePeriods);
    }

    /**
     * @param {string[]|number[]|null} timePeriods 
     */
    setTimePeriods(timePeriods){
        if(timePeriods != null){
            for (const timePeriod of timePeriods) {
                this.#timePeriods.push(UnitHelper.formatedDurationToMs(timePeriod));
            }
        }
    }

    getTimePeriodsInMs(){
        return [...this.#timePeriods.sort((a, b) => a - b)];
    }


    /**
         * @returns {string} string representation of this object
         */
    toJson() {
        const obj = JSON.parse(super.toJson());
        obj.timePeriods = this.#timePeriods;

        return JSON.stringify(obj);
    }

    /**
     * @param {string} objString 
     * @returns {AssetDataCollectionConfig}
     */
    static fromJson(objString) {
        const obj = JSON.parse(objString);
        const timePeriodConfig = new AssetDataCollectionConfig(obj.timePeriods);

        return timePeriodConfig;
    }
}