import { BuildableComponent } from '../component/buildable_component.js';
import { Reporter } from '../../reporter/reporter.js';
import { checkPropTypes } from '../../lib.js';
import { Component } from '../../component/component.js';
import { Theme } from '../theme/theme.js';
import { WrappedImage } from '../image/wrapped_image.js';
import { Project } from '../../project.js';
/** Theme Group */
class ThemeGroup extends BuildableComponent {
/**
* @param {Reporter} reporter
* @param {Object} settings
* @param {UUID} [settings.id]
* @param {string} settings.name
* @param {WrappedImage} [settings.thumbnail]
*/
constructor(reporter, settings) {
// this component should not be auto linked in parent class
super(reporter, settings, { autoLinkDependencies: false });
checkPropTypes(
settings,
{
name: 'string',
},
{}
);
if ( settings.thumbnail ) {
this._dependencies.UI = { thumbnail: settings.thumbnail };
}
}
static _exportName = {
singular: 'themeGroup',
plural: 'themeGroups'
};
_onTreeSet() {
const component = this;
this._tree.on(
'change',
({ cls = [] }) => {
if ( cls.includes( Theme ) ) {
component.report({ msg: 'Theme(s) changed in package. Rebuilding.' });
component._build('main');
}
},
this.id
);
this._build('main');
}
_onTreeUnset() {
if (this._tree.hasListener('change', this.id)) {
this._tree.removeListener('change', this.id);
}
}
/**
* Collect material categories of this type from the package
* @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 themes = this._tree ? this._tree.themes.filter(con => con.settings.group === this) : [];
for (let setQuality of Component.qualities) {
this._setContent('main', setQuality, themes);
}
this.report({ msg: `Built theme group of ${themes.length} themes.` });
break;
default:
this._setContent(part, quality, null);
break;
}
return this;
}
}
export { ThemeGroup };