Source: package/index.js

// dit is niet de index, dit is gewoon de package
// de index is alleen de export


import { Reporter } from '../reporter/reporter.js';
import { Block } from './block/block.js';
import { BlockSet } from './block/block_set.js';
import { ConnectorType } from './connector/connector_type.js';
import { BlockCategory } from './block/block_category.js';
import { Preset } from './preset.js';
import { WrappedMaterial } from './material/wrapped_material.js';
import { WrappedMesh } from './mesh/wrapped_mesh.js';
import { WrappedImage } from './image/wrapped_image.js';
import { MaterialSet } from './material/material_set.js';
import { copyProps } from '../lib.js';
import { MaterialCategory } from './material/material_category.js';
import { ConfigurationSettings } from './configuration_settings.js';
import { NavigationSettings } from './navigation_settings.js';


/** Loads and manages a product package index */

class Index {

    constructor(reporter) {

        /** @type {Reporter} */

        this._reporter = reporter;


        /**
         * @param {Object} log
         * @param {string} log.msg
         * @param {SyslogLevel} [log.level]
         */

        this.report = ({ msg, level = 'debug' }) =>
            reporter.report({ msg, level, source: 'Index' })
    }

    


    /** 
     * Location of the product data package
     * @param {URL} packageURL
     */

    async loadFromURL(packageURL) {

        const indexURL = `${packageURL.href}\index.json`;
        const response = await fetch(indexURL);

        if ( ! response )
        {
            throw new Error( `Could not fetch package index from ${indexURL}` )
        }

        const data = response.json();

        this.parse( data );

        return true
    }



    



    /**
     * Parse JSON into class structure
     * @param {Object} index
     */

    parse( index ) {

        try {
            copyProps(
                index,
                this,
                [ 'name', 'version', 'author', 'license' ],
                [ 'description', 'generator', 'repository' ]
            )
        }
        catch (err) {
            throw new Error( 'Could not copy general info index props. ' + err.message )
        }

        this.configurationSettings = new ConfigurationSettings( this._reporter, index.configurationSettings );

        this.navigationSettings = new NavigationSettings( this._reporter, index.navigationSettings );



    }


    /** @returns {Object} */

    toJSON() {
        return {
            name: this.name,
            version: this.version,
            author: this.author,
            license: this.license,
            description: this.description,
            generator: this.generator,
            repository: this.repository,

            configurationOptions: this.configurationSettings.toJSON(),
        }
    }
}

export { Index };