/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { readRepoConfig } from '../Config/ConfigManager.js';
import { DataverseSystemDataService, getEnvironmentByName } from '../services/index.js';
const ENV_VAR_PREFIX = '@envvar:';
export function isEnvironmentVariable(parameter) {
    return !!parameter && parameter.startsWith(ENV_VAR_PREFIX);
}
export function getEnvironmentVariableName(parameter) {
    if (!parameter) {
        return parameter;
    }
    if (!isEnvironmentVariable(parameter)) {
        throw new Error(`Parameter '${parameter}' is not an environment variable.`);
    }
    return parameter.slice(ENV_VAR_PREFIX.length);
}
export async function resolveEnvironmentVariable(parameter, httpClient, orgUrl, logger) {
    if (!parameter || !orgUrl) {
        return parameter;
    }
    if (!isEnvironmentVariable(parameter)) {
        throw new Error(`Parameter '${parameter}' is not an environment variable.`);
    }
    const dataverseClient = new DataverseSystemDataService(httpClient);
    const variableDefinition = await dataverseClient.getEnvironmentVariable(orgUrl, parameter.slice(ENV_VAR_PREFIX.length), logger);
    if (!variableDefinition) {
        throw new Error(`Environment variable ${parameter} not found in current env.`);
    }
    return (variableDefinition.environmentvariabledefinition_environmentvariablevalue?.[0]?.value || '');
}
/**
 * Retrieves the Dataverse organization URL for the current environment based on the linked environment metadata.
 * @param powerConfigPath The path to the power configuration file.
 * @returns The Dataverse organization URL or undefined if not found.
 */
export async function getDataverseOrgUrl(powerConfigPath) {
    const config = await readRepoConfig(powerConfigPath);
    const environmentId = config.environmentId;
    if (environmentId) {
        const metadata = await getEnvironmentByName(environmentId);
        const dataverseOrgUrl = metadata?.properties?.linkedEnvironmentMetadata?.instanceApiUrl;
        if (!dataverseOrgUrl) {
            throw new Error('Unable to determine the dataverse org associated with the current environment.');
        }
        return dataverseOrgUrl;
    }
    else {
        throw new Error('Environment ID is not set in the configuration file.');
    }
}
/**
 * Retrieves the Dataverse organization URL for the given environment name (environment ID GUID).
 * @param environmentName The environment ID GUID (from BaseActionContext.environmentName).
 * @returns The Dataverse organization URL.
 * @throws If the environment does not have a linked Dataverse instance.
 */
export async function getDataverseOrgUrlByEnvironmentName(environmentName) {
    const metadata = await getEnvironmentByName(environmentName);
    const orgUrl = metadata?.properties?.linkedEnvironmentMetadata?.instanceApiUrl;
    if (!orgUrl) {
        throw new Error(`Unable to determine the Dataverse organization URL for environment '${environmentName}'. Ensure the environment has a linked Dataverse instance.`);
    }
    return orgUrl;
}
/**
 * Helper function to normalize lazy parameters.
 * Handles both direct string values and deferred functions that return Promise<string>.
 * @param value - The value to normalize (string, function, or undefined/null)
 * @returns The resolved value if it's not an empty string, otherwise undefined
 */
export async function normalizeParam(value) {
    if (value === undefined || value === null) {
        return undefined;
    }
    // If it's a function, call it to get the promise and await it
    const resolvedValue = typeof value === 'function' ? await value() : value;
    return resolvedValue && resolvedValue.trim() !== '' ? resolvedValue.trim() : undefined;
}
//# sourceMappingURL=DataUtils.js.map