"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SiteConfig = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const HEADING_INDEXING_LEVEL_DEFAULT = 3;
/**
 * Represents a read only site config read from the site configuration file,
 * with default values for unspecified properties.
 */
class SiteConfig {
    /**
     * @param siteConfigJson The raw json read from the site configuration file
     * @param cliBaseUrl As read from the --baseUrl option
     */
    constructor(siteConfigJson, cliBaseUrl) {
        this.baseUrl = cliBaseUrl !== undefined
            ? cliBaseUrl
            : (siteConfigJson.baseUrl || '');
        this.enableSearch = siteConfigJson.enableSearch === undefined || siteConfigJson.enableSearch;
        this.faviconPath = siteConfigJson.faviconPath;
        this.headingIndexingLevel = siteConfigJson.headingIndexingLevel || HEADING_INDEXING_LEVEL_DEFAULT;
        this.style = siteConfigJson.style || {};
        this.style.codeTheme = (this.style.codeTheme === 'dark' || this.style.codeTheme === 'light')
            ? this.style.codeTheme
            : 'dark';
        this.style.codeLineNumbers = this.style.codeLineNumbers !== undefined
            ? this.style.codeLineNumbers : false;
        this.pages = siteConfigJson.pages || [];
        this.pagesExclude = siteConfigJson.pagesExclude || [];
        this.ignore = siteConfigJson.ignore || [];
        this.externalScripts = siteConfigJson.externalScripts || [];
        this.titlePrefix = siteConfigJson.titlePrefix || '';
        this.titleSuffix = siteConfigJson.titleSuffix || '';
        this.globalOverride = siteConfigJson.globalOverride || {};
        this.timeZone = siteConfigJson.timeZone || 'UTC';
        this.locale = siteConfigJson.locale || 'en-GB';
        this.plugins = siteConfigJson.plugins || [];
        this.pluginsContext = siteConfigJson.pluginsContext || {};
        this.deploy = siteConfigJson.deploy || {};
        this.intrasiteLinkValidation = siteConfigJson.intrasiteLinkValidation || {};
        this.intrasiteLinkValidation.enabled = this.intrasiteLinkValidation.enabled !== false;
        // TODO this should probably be in pluginsContext
        this.plantumlCheck = siteConfigJson.plantumlCheck !== undefined
            ? siteConfigJson.plantumlCheck : true; // check PlantUML's prerequisite by default
    }
    /**
     * Read and returns the site config from site.json, overwrites the default base URL
     * if it's specified by the user.
     *
     * @param rootPath The absolute path to the site folder
     * @param siteConfigPath The relative path to the siteConfig
     * @param baseUrl user defined base URL (if exists)
     */
    static readSiteConfig(rootPath, siteConfigPath, baseUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const absoluteSiteConfigPath = path_1.default.join(rootPath, siteConfigPath);
                const siteConfigJson = fs_extra_1.default.readJsonSync(absoluteSiteConfigPath);
                const siteConfig = new SiteConfig(siteConfigJson, baseUrl);
                return siteConfig;
            }
            catch (err) {
                throw (new Error(`Failed to read the site config file '${siteConfigPath}' at`
                    + `${rootPath}:\n${err.message}\nPlease ensure the file exist or is valid`));
            }
        });
    }
}
exports.SiteConfig = SiteConfig;
//# sourceMappingURL=SiteConfig.js.map