"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HotModule = void 0;
const Hot_1 = require("./Hot");
const HotAsset_1 = require("./HotAsset");
/**
 * Load a module that contains the assets to load for the frontend.
 */
class HotModule {
    constructor(name) {
        this.name = name;
        this.import = [];
        this.html = [];
        this.css = [];
        this.js = [];
        this.componentLibrary = "";
        this.components = [];
    }
    /**
     * Output CSS.
     */
    outputCSS(echoOut = true) {
        if (this.css == null)
            return;
        let output = "";
        this.outputAsset("css", this.css, (asset) => {
            const content = asset.output();
            output += `${content}\n`;
        });
        if (echoOut === true)
            Hot_1.Hot.echoUnsafe(output);
        return (output);
    }
    /**
     * Output JS.
     */
    outputJS(echoOut = true) {
        if (this.js == null)
            return;
        let output = "";
        this.outputAsset("js", this.js, (asset) => {
            const content = asset.output();
            output += `${content}\n`;
        });
        if (echoOut === true)
            Hot_1.Hot.echoUnsafe(output);
        return (output);
    }
    /**
     * Output a loaded HTML asset.
     */
    output(assetName_1) {
        return __awaiter(this, arguments, void 0, function* (assetName, args = null) {
            yield Hot_1.Hot.include(`${this.name}/${assetName}.hott`, args);
        });
    }
    /**
     * Load HTML assets.
     */
    loadHTML() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.html == null)
                return;
            let files = {};
            this.outputAsset("html", this.html, (asset) => {
                const file = asset.output();
                if (typeof (file) === "string")
                    throw new Error(`HTML assets cannot be outputted using only a string!`);
                files[file.name] = file;
            });
            yield Hot_1.Hot.CurrentPage.processor.loadHotFiles(files);
        });
    }
    /**
     * Output components assets.
     */
    outputComponents(echoOut = true) {
        if (this.components == null)
            return ("");
        if (this.components.length < 1)
            return ("");
        let output = `<script type = "text/javascript">`;
        let componentLibrary = "";
        if (this.componentLibrary != null) {
            if (this.componentLibrary !== "")
                componentLibrary = `${this.componentLibrary}.`;
        }
        for (let iIdx = 0; iIdx < this.components.length; iIdx++) {
            let component = this.components[iIdx];
            output += `Hot.CurrentPage.processor.addComponent (${componentLibrary}${component});\n`;
        }
        output += `</script>`;
        if (echoOut === true)
            Hot_1.Hot.echoUnsafe(output);
        return (output);
    }
    /**
     * Output an asset to HTML.
     */
    outputAsset(assetType, assets = [], callback) {
        for (let iIdx = 0; iIdx < assets.length; iIdx++) {
            let asset = assets[iIdx];
            let loadAsset = null;
            loadAsset = new HotAsset_1.HotAsset(assetType);
            if (typeof (asset) === "string")
                loadAsset.path = asset;
            else {
                if (asset.name != null)
                    loadAsset.name = asset.name;
                if (asset.path != null)
                    loadAsset.path = asset.path;
                if (asset.content != null)
                    loadAsset.content = asset.content;
            }
            callback(loadAsset);
        }
    }
}
exports.HotModule = HotModule;
//# sourceMappingURL=HotModule.js.map