import { parse as parseHtml } from 'node-html-parser';
import { decode } from 'html-entities';
import { relative } from 'node:path';
export const htmlReportToObj = (htmlContent) => {
    const root = parseHtml(htmlContent);
    // eslint-disable-next-line unicorn/prefer-dom-node-text-content
    const read = (id) => { var _a; return (_a = root.querySelector(`#${id}`)) === null || _a === void 0 ? void 0 : _a.innerText.trim(); };
    const buildScript = read('buildScript');
    const centralTemplate = Object.fromEntries([...root.querySelectorAll('#central_template [data-key]')]
        // eslint-disable-next-line unicorn/prefer-dom-node-dataset, unicorn/prefer-dom-node-text-content
        .map(el => [decode(el.getAttribute('data-key')), decode(el.innerText)])
        .filter(([key, value]) => (key === null || key === void 0 ? void 0 : key.trim()) !== '' && (value === null || value === void 0 ? void 0 : value.trim()) !== ''));
    const specmaticCoverageString = read('specmaticCoverage');
    const specmaticStubUsageString = read('specmaticStubUsage');
    const specmaticCentralRepoReportString = read('specmaticCentralRepoReport');
    const agentGitRoot = read('BUILD_SOURCESDIRECTORY');
    const specmaticCoverage = specmaticCoverageString
        ? JSON.parse(decode(specmaticCoverageString))
        : undefined;
    const specmaticStubUsage = specmaticStubUsageString
        ? JSON.parse(decode(specmaticStubUsageString))
        : undefined;
    const specmaticCentralRepoReport = specmaticCentralRepoReportString
        ? JSON.parse(decode(specmaticCentralRepoReportString))
        : undefined;
    const specmaticConfigPath = () => {
        var _a;
        if (!agentGitRoot)
            return;
        if (!specmaticCoverage && !specmaticStubUsage)
            return;
        return relative(agentGitRoot, (_a = (specmaticCoverage || specmaticStubUsage)) === null || _a === void 0 ? void 0 : _a.specmaticConfigPath);
    };
    /* eslint-disable @typescript-eslint/no-non-null-assertion */
    return {
        sonarHost: read('sonarHost'),
        sonarProjectKey: read('sonarProjectKey'),
        collection: read('SYSTEM_COLLECTIONURI').split('/')[3],
        collectionId: read('SYSTEM_COLLECTIONID'),
        project: read('SYSTEM_TEAMPROJECT'),
        repoName: read('BUILD_REPOSITORY_NAME'),
        repoId: read('BUILD_REPOSITORY_ID'),
        repoUrl: read('BUILD_REPOSITORY_URI'),
        branch: read('BUILD_SOURCEBRANCH'),
        branchName: read('BUILD_SOURCEBRANCHNAME'),
        buildId: read('BUILD_BUILDID'),
        buildDefinitionId: read('SYSTEM_DEFINITIONID'),
        buildReason: read('BUILD_REASON'),
        agentName: read('AGENT_NAME'),
        buildScript: buildScript ? decode(buildScript) : undefined,
        centralTemplate: Object.keys(centralTemplate).length ? centralTemplate : undefined,
        specmaticConfigPath: specmaticConfigPath(),
        specmaticCoverage: specmaticCoverage === null || specmaticCoverage === void 0 ? void 0 : specmaticCoverage.apiCoverage,
        specmaticStubUsage: specmaticStubUsage === null || specmaticStubUsage === void 0 ? void 0 : specmaticStubUsage.stubUsage,
        specmaticCentralRepoReport: specmaticCentralRepoReport === null || specmaticCentralRepoReport === void 0 ? void 0 : specmaticCentralRepoReport.specifications,
    };
    /* eslint-enable */
};
