"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 });
const path = require("path");
const Adapter = require("@frctl/fractal").Adapter;
const tempseal_1 = require("@sealcode/tempseal");
const compile_pipeline_1 = require("@sealcode/tempseal/compile-pipeline");
const operators_1 = require("@sealcode/tempseal/compile-pipeline/operators");
class TempsealAdapter extends Adapter {
    constructor(source, app) {
        super({}, source);
        this._app = app;
    }
    render(input_file_path, _str, context, _meta) {
        return __awaiter(this, void 0, void 0, function* () {
            if (Array.isArray(context)) {
                if (context[context.length - 1] === null) {
                    context = context.slice(0, context.length - 1);
                }
            }
            for (let module_path of [
                input_file_path,
                require.resolve("./config.js"),
            ]) {
                if (require.cache[module_path])
                    delete require.cache[module_path];
            }
            const { config } = require("./config.js");
            try {
                const start = Date.now();
                if (!input_file_path) {
                    return "";
                }
                const component = require(input_file_path).default;
                const output_path = path.resolve(__dirname, "public");
                let resolve_html_body;
                let resolve_write;
                let reject;
                const promises = [
                    new Promise(resolve => {
                        resolve_html_body = resolve;
                    }),
                    new Promise((resolve, _reject) => {
                        resolve_write = resolve;
                        reject = _reject;
                    }),
                ];
                compile_pipeline_1.emitEffects(new tempseal_1.ComponentMap([component]), config, [
                    {
                        component_name: component.identifier,
                        props: context,
                    },
                ])
                    .pipe(operators_1.replaceUrlPlaceholders("/"), operators_1.downloadFonts(output_path), operators_1.combineHtml(content => resolve_html_body(content)), operators_1.write(output_path))
                    .subscribe(e => console.log(e.message), er => {
                    var _a;
                    return reject(`Error: <code><pre>${er.formatted ? er.formatted : er}\n${(_a = er.stack) === null || _a === void 0 ? void 0 : _a.replace(/</g, "&lt;")}</pre></code>`);
                }, () => {
                    console.log("RESOLVING WRITE");
                    resolve_write();
                });
                const [html_body] = yield Promise.all(promises);
                console.log(`Done in ${Date.now() - start}ms`);
                return html_body;
            }
            catch (e) {
                console.error(e);
                throw e;
            }
        });
    }
}
exports.default = {
    register: (source, app) => new TempsealAdapter(source, app),
};
//# sourceMappingURL=tempseal-adapter.js.map