import { BuildableComponent } from '../component/buildable_component.js';
import { Reporter } from '../../reporter/reporter.js';
import { UUIDRegex, checkPropTypes } from '../../lib.js';
import { BlockCategoryType } from './block_category_type.js';
import { Block } from './block.js';
import { WrappedImage } from '../image/wrapped_image.js';
import { Project } from '../../project.js';
/** Block category */
class BlockCategory extends BuildableComponent {
/**
* @param {Reporter} reporter
* @param {Object} settings
* @param {UUID} [settings.id]
* @param {string} settings.name
* @param {BlockCategoryType} settings.type
* @param {WrappedImage} [settings.thumbnail]
*/
constructor(reporter, settings) {
super(reporter, settings );
checkPropTypes(
settings,
{
name: 'string',
type: BlockCategoryType
},
{
id: UUIDRegex,
}
);
}
static _exportName = {
singular: 'blockCategory',
plural: 'blockCategories'
};
_onTreeSet() {
const component = this;
this._tree.on(
'change',
({ cls = [] }) => {
if ( cls.includes( Block ) ) {
component.report({ msg: 'Blocks 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 blocks 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 blocks = this._tree
? this._tree.blocks.filter(mat => (mat.settings.categories || []).find(cat => cat === this))
: []
this._setContent('main', quality, blocks);
this.report({ msg: `Built category of ${blocks.length} blocks.` });
break;
default:
this._setContent(part, quality, null);
break;
}
return this;
}
}
export { BlockCategory };