import { Output } from './output.js';
import { join } from '../util/path.js';
export class Resource {
    constructor() {
        this.default = '';
        this.categories = {};
        this.all = [];
        this.allCategorised = [];
    }
    addCategory(id, label) {
        if (this.categories[id]) {
            console.warn('Category already exists');
            return;
        }
        this.categories[id] = { id, label, resources: {} };
    }
    add(category, id, label, path) {
        if (!this.categories[category]) {
            console.warn('Category does not exist');
            return;
        }
        if (this.categories[category].resources[id]) {
            console.warn('A sticker with this name has already been created');
            return;
        }
        this.categories[category].resources[id] = { id, label, path: path };
    }
    setDefault(id) {
        this.default = id;
    }
    resolve(path) {
        const ASSET_URL_PREFIX_KEY = 'sticker:base-url';
        const prefix = Output.config.get(ASSET_URL_PREFIX_KEY, '/assets/part/stickers');
        return encodeURI(join(prefix, path));
    }
    get resourceSet() {
        if (this.all.length > 0) {
            return this.all;
        }
        Object.keys(this.categories).forEach(category => {
            Object.keys(this.categories[category].resources).forEach(sticker => {
                this.all.push(this.categories[category].resources[sticker]);
            });
        });
        return this.all;
    }
    get categorisedResource() {
        if (this.allCategorised.length > 0) {
            return this.allCategorised;
        }
        Object.keys(this.categories).forEach(category => {
            let categoryObject = {
                id: this.categories[category].id,
                label: this.categories[category].label,
                resources: []
            };
            Object.keys(this.categories[category].resources).forEach(sticker => {
                const stickerObject = { id: sticker, src: this.getUrl(sticker) };
                categoryObject.resources.push(stickerObject);
            });
            this.allCategorised.push(categoryObject);
        });
        return this.allCategorised;
    }
    get categoryEnum() {
        return Object.keys(this.categories).map(category => [this.categories[category].label, this.categories[category].id]);
    }
    getRandom() {
        return this.resourceSet[Math.floor(Math.random() * this.resourceSet.length)].id;
    }
    getRandomFrom(id) {
        if (!this.categories[id]) {
            console.warn('Category does not exist');
            return this.default;
        }
        else {
            const randomKey = Object.keys(this.categories[id].resources)[Math.floor(Math.random() * Object.keys(this.categories[id].resources).length)];
            return this.categories[id].resources[randomKey].id;
        }
    }
    getUrl(id) {
        if (!id) {
            console.warn(`No sticker provided`);
            return '';
        }
        let selectedSticker = this.resourceSet.find(sticker => sticker.id === id);
        if (!selectedSticker) {
            selectedSticker = this.resourceSet.find(sticker => sticker.id === this.default);
        }
        if (!selectedSticker) {
            console.warn(`Sticker ${id} does not exist`);
            return '';
        }
        return this.resolve(selectedSticker.path);
    }
    getLabel(id) {
        const selectedSticker = this.resourceSet.find(sticker => sticker.id === id);
        if (!selectedSticker) {
            console.warn('Sticker does not exist');
            return;
        }
        return this.resolve(selectedSticker.label);
    }
}
export class Resources {
    constructor() {
        this.resources = new Map();
        this.resources.set('stickers', new Resource);
    }
    get(id) {
        return this.resources.get(id);
    }
}
//# sourceMappingURL=resources.js.map