"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MinimalTheme = void 0;
const FS = require("fs");
const Path = require("path");
const DefaultTheme_1 = require("./DefaultTheme");
const UrlMapping_1 = require("../models/UrlMapping");
const index_1 = require("../../models/reflections/index");
const events_1 = require("../events");
const NavigationItem_1 = require("../models/NavigationItem");
class MinimalTheme extends DefaultTheme_1.DefaultTheme {
    /**
     * Create a new DefaultTheme instance.
     *
     * @param renderer  The renderer this theme is attached to.
     * @param basePath  The base path of this theme.
     */
    constructor(renderer, basePath) {
        super(renderer, basePath);
        renderer.removeComponent("assets");
        renderer.removeComponent("javascriptIndex");
        renderer.removeComponent("navigation");
        renderer.removeComponent("toc");
        this.listenTo(renderer, events_1.PageEvent.BEGIN, this.onRendererBeginPage);
    }
    /**
     * Test whether the given path contains a documentation generated by this theme.
     *
     * @param path  The path of the directory that should be tested.
     * @returns     TRUE if the given path seems to be a previous output directory,
     *              otherwise FALSE.
     */
    isOutputDirectory(path) {
        if (!FS.existsSync(Path.join(path, "index.html"))) {
            return false;
        }
        return true;
    }
    /**
     * Map the models of the given project to the desired output files.
     *
     * @param project  The project whose urls should be generated.
     * @returns        A list of [[UrlMapping]] instances defining which models
     *                 should be rendered to which files.
     */
    getUrls(project) {
        const urls = [];
        urls.push(new UrlMapping_1.UrlMapping("index.html", project, "index.hbs"));
        project.url = "index.html";
        project.anchor = undefined;
        project.hasOwnDocument = true;
        (project.children || []).forEach((child) => {
            DefaultTheme_1.DefaultTheme.applyAnchorUrl(child, project);
        });
        return urls;
    }
    /**
     * Triggered before a document will be rendered.
     *
     * @param page  An event object describing the current render operation.
     */
    onRendererBeginPage(page) {
        const model = page.model;
        if (!(model instanceof index_1.Reflection)) {
            return;
        }
        page.toc = new NavigationItem_1.NavigationItem();
        MinimalTheme.buildToc(page.model, page.toc);
    }
    /**
     * Create a toc navigation item structure.
     *
     * @param model   The models whose children should be written to the toc.
     * @param parent  The parent [[Models.NavigationItem]] the toc should be appended to.
     */
    static buildToc(model, parent) {
        const children = model.children || [];
        children.forEach((child) => {
            const item = NavigationItem_1.NavigationItem.create(child, parent, true);
            MinimalTheme.buildToc(child, item);
        });
    }
}
exports.MinimalTheme = MinimalTheme;
