"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveWorkspaceOTelMetadata = resolveWorkspaceOTelMetadata;
exports.workspaceMetadataToOTelAttributes = workspaceMetadataToOTelAttributes;
const resources_1 = require("../../../util/vs/base/common/resources");
const gitService_1 = require("../../git/common/gitService");
const genAiAttributes_1 = require("./genAiAttributes");
/**
 * Synchronously resolve workspace metadata from the active repository.
 * Uses `activeRepository.get()` which is non-blocking.
 */
function resolveWorkspaceOTelMetadata(gitService, fileUri) {
    const repoContext = gitService.activeRepository?.get();
    if (!repoContext) {
        return {};
    }
    return buildWorkspaceMetadata(repoContext, fileUri);
}
function buildWorkspaceMetadata(repoContext, fileUri) {
    let remoteUrl;
    const repoInfo = Array.from((0, gitService_1.getOrderedRepoInfosFromContext)(repoContext))[0];
    if (repoInfo?.fetchUrl) {
        remoteUrl = (0, gitService_1.normalizeFetchUrl)(repoInfo.fetchUrl);
    }
    let fileRelativePath;
    if (fileUri && (0, resources_1.isEqualOrParent)(fileUri, repoContext.rootUri)) {
        fileRelativePath = (0, resources_1.relativePath)(repoContext.rootUri, fileUri);
    }
    return {
        headBranchName: repoContext.headBranchName,
        headCommitHash: repoContext.headCommitHash,
        remoteUrl,
        fileRelativePath,
    };
}
/**
 * Convert workspace metadata to OTel attributes, omitting undefined values.
 */
function workspaceMetadataToOTelAttributes(metadata) {
    if (!metadata) {
        return {};
    }
    const attrs = {};
    if (metadata.headBranchName) {
        attrs[genAiAttributes_1.CopilotChatAttr.REPO_HEAD_BRANCH_NAME] = metadata.headBranchName;
    }
    if (metadata.headCommitHash) {
        attrs[genAiAttributes_1.CopilotChatAttr.REPO_HEAD_COMMIT_HASH] = metadata.headCommitHash;
    }
    if (metadata.remoteUrl) {
        attrs[genAiAttributes_1.CopilotChatAttr.REPO_REMOTE_URL] = metadata.remoteUrl;
    }
    if (metadata.fileRelativePath) {
        attrs[genAiAttributes_1.CopilotChatAttr.FILE_RELATIVE_PATH] = metadata.fileRelativePath;
    }
    return attrs;
}
//# sourceMappingURL=workspaceOTelMetadata.js.map