import sortArray from "sort-array";
import { buildGenericMarkdownTable } from "../utils/flowVisualiser/nodeFormatUtils.js";
import { DocBuilderRoot } from "./docBuilderRoot.js";
import { DocBuilderPackageXML } from "./docBuilderPackageXml.js";
import fs from "fs";
import { parsePackageXmlFile } from "../utils/xmlUtils.js";
import { makeFileNameGitCompliant } from "../utils/gitUtils.js";
export class DocBuilderPackage extends DocBuilderRoot {
    docType = "Package";
    promptKey = "PROMPT_DESCRIBE_PACKAGE";
    placeholder = "<!-- Package description -->";
    xmlRootKey = "json";
    docsSection = "packages";
    static buildIndexTable(prefix, packageDescriptions, filterObject = null) {
        const filteredPackages = filterObject ? packageDescriptions.filter(page => page.impactedObjects.includes(filterObject)) : packageDescriptions;
        if (filteredPackages.length === 0) {
            return [];
        }
        const lines = [];
        lines.push(...[
            "## Installed packages",
            "",
            "| Name  | Namespace | Version | Version Name |",
            "| :---- | :-------- | :------ | :----------: | "
        ]);
        for (const pckg of sortArray(filteredPackages, { by: ['namespace', 'name'], order: ['asc', 'asc'] })) {
            const packageNameCell = `[${pckg.name}](${prefix}${makeFileNameGitCompliant(pckg.name)}.md)`;
            lines.push(...[
                `| ${packageNameCell} | ${pckg.namespace || ""} | [${pckg.versionNumber}](https://test.salesforce.com/packaging/installPackage.apexp?p0=${pckg.versionId}) | ${pckg.versionName} |`
            ]);
        }
        lines.push("");
        return lines;
    }
    async buildInitialMarkdownLines() {
        return [
            `## ${this.metadataName}`,
            '',
            '<!-- Package description -->',
            '',
            buildGenericMarkdownTable(this.parsedXmlObject, ["SubscriberPackageName", "SubscriberPackageNamespace", "SubscriberPackageVersionNumber", "SubscriberPackageVersionId", "SubscriberPackageVersionName", "SubscriberPackageId"], "## Package attributes", []),
            '',
            '## Package Metadatas',
            '',
            '<div id="jstree-container"></div>',
            ''
        ];
    }
    // Generate json for display with jsTree npm library 
    async generateJsonTree() {
        if (this.additionalVariables.PACKAGE_FILE && fs.existsSync(this.additionalVariables.PACKAGE_FILE)) {
            const packageData = await parsePackageXmlFile(this.additionalVariables.PACKAGE_FILE);
            return DocBuilderPackageXML.generateJsonTree("all", packageData);
        }
        return null;
    }
}
//# sourceMappingURL=docBuilderPackage.js.map