import { BuildableComponent } from '../component/buildable_component.js';
import { Reporter } from '../../reporter/reporter.js';
import { UUIDRegex, checkPropTypes } from '../../lib.js';
import { MaterialCategoryType } from './material_category_type.js';
import { WrappedMaterial } from './wrapped_material.js';
import { WrappedImage } from '../image/wrapped_image.js';
import { Project } from '../../project.js';
/** Material category */
class MaterialCategory extends BuildableComponent {
/**
* @param {Reporter} reporter
* @param {Object} settings
* @param {UUID} [settings.id]
* @param {string} settings.name
* @param {MaterialCategoryType} settings.type
* @param {WrappedImage} [settings.thumbnail]
*/
// * @param {Package} settings.pkg
constructor(reporter, settings) {
// this component should not be auto linked in parent class
super(reporter, settings );//, { autoLinkDependencies: false });
checkPropTypes(
settings,
{
name: 'string',
// pkg: Package,
type: MaterialCategoryType
},
{
id: UUIDRegex,
}
);
// this.exportName = 'materialCategories';
// this._dependencies.main = { type: settings.type };
// if ( settings.thumbnail ) {
// this._dependencies.UI = { thumbnail: settings.thumbnail };
// }
// settings.pkg.on('change', ({ cls = null }) => {
// if ( cls === WrappedMaterial ) {
// this.report({ msg: 'Materials changed in package. Rebuilding.' });
// this._build('main');
// }
// });
}
static _exportName = {
singular: 'materialCategory',
plural: 'materialCategories'
};
_onTreeSet() {
const component = this;
this._tree.on(
'change',
({ cls = [] }) => {
if ( cls.includes( WrappedMaterial ) ) {
component.report({ msg: 'Materials changed in package. Rebuilding.' });
component._rebuild();
}
},
this.id
);
this._rebuild();
}
_onTreeUnset() {
if (this._tree.hasListener('change', this.id)) {
this._tree.removeListener('change', this.id);
}
}
/**
* Collect materials in this category (that have this category as a dependency)
* @protected
*/
async _build(part,quality) {
switch (part) {
case 'UI':
if (this._settings.thumbnail) {
this._setContent('UI', quality, this._settings.thumbnail.content.main[quality]);
}
else {
this._setContent('UI', quality, Project.defaultImages.missing.cloneNode(true));
}
break;
case 'main':
// const materials = this._settings.pkg.materials.filter(mat => (mat.settings.categories || []).find(cat => cat === this))
const materials = this._tree
? this._tree.materials.filter(mat => (mat.settings.categories || []).find(cat => cat === this))
: [];
// for (let setQuality of Component.qualities) {
// this._setContent('main', setQuality, materials);
// }
this._setContent('main', quality, materials);
this.report({ msg: `Built category of ${materials.length} materials.` });
break;
default:
this._setContent(part, quality, null);
break;
}
return this;
}
}
export { MaterialCategory };