import { getPopulatedFields } from '../mapping.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Mapping of SFCC_* environment variable names to NormalizedConfig field names.
 */
const ENV_VAR_MAP = {
    // sfcc-ci legacy aliases — listed first so canonical names below take precedence
    SFCC_OAUTH_CLIENT_ID: 'clientId',
    SFCC_OAUTH_CLIENT_SECRET: 'clientSecret',
    SFCC_LOGIN_URL: 'accountManagerHost',
    SFCC_SERVER: 'hostname',
    SFCC_WEBDAV_SERVER: 'webdavHostname',
    SFCC_CODE_VERSION: 'codeVersion',
    SFCC_USERNAME: 'username',
    SFCC_PASSWORD: 'password',
    SFCC_CERTIFICATE: 'certificate',
    SFCC_CERTIFICATE_PASSPHRASE: 'certificatePassphrase',
    SFCC_SELFSIGNED: 'selfSigned',
    SFCC_CLIENT_ID: 'clientId',
    SFCC_CLIENT_SECRET: 'clientSecret',
    SFCC_OAUTH_SCOPES: 'scopes',
    SFCC_SHORTCODE: 'shortCode',
    SFCC_TENANT_ID: 'tenantId',
    SFCC_CARTRIDGES: 'cartridges',
    SFCC_CATALOGS: 'catalogs',
    SFCC_LIBRARIES: 'libraries',
    SFCC_ASSET_QUERY: 'assetQuery',
    SFCC_AUTH_METHODS: 'authMethods',
    SFCC_ACCOUNT_MANAGER_HOST: 'accountManagerHost',
    SFCC_SANDBOX_API_HOST: 'sandboxApiHost',
    // JWT Bearer auth env vars
    SFCC_JWT_CERT: 'jwtCertPath',
    SFCC_JWT_KEY: 'jwtKeyPath',
    SFCC_JWT_PASSPHRASE: 'jwtPassphrase',
    // MRT env vars — MRT_* listed first as fallback, SFCC_MRT_* listed second to take precedence
    MRT_API_KEY: 'mrtApiKey',
    SFCC_MRT_API_KEY: 'mrtApiKey',
    MRT_PROJECT: 'mrtProject',
    SFCC_MRT_PROJECT: 'mrtProject',
    MRT_ENVIRONMENT: 'mrtEnvironment',
    SFCC_MRT_ENVIRONMENT: 'mrtEnvironment',
    MRT_CLOUD_ORIGIN: 'mrtOrigin',
    SFCC_MRT_CLOUD_ORIGIN: 'mrtOrigin',
};
/** Fields that should be parsed as comma-separated arrays. */
const ARRAY_FIELDS = new Set([
    'scopes',
    'authMethods',
    'cartridges',
    'catalogs',
    'libraries',
    'assetQuery',
]);
/** Fields that should be parsed as booleans. */
const BOOLEAN_FIELDS = new Set(['selfSigned']);
/**
 * Configuration source that reads SFCC_* environment variables.
 *
 * Priority -10 (higher than dw.json at 0), matching CLI behavior where
 * env vars override file-based config.
 *
 * Not added to default sources — opt-in only. The CLI handles env vars
 * via oclif flag `env:` mappings; this source is for consumers like
 * the VS Code extension that call `resolveConfig()` directly.
 *
 * @example
 * ```typescript
 * import { resolveConfig, EnvSource } from '@salesforce/b2c-tooling-sdk/config';
 *
 * const config = resolveConfig({}, {
 *   sourcesBefore: [new EnvSource()],
 * });
 * ```
 *
 * @internal
 */
export class EnvSource {
    name = 'EnvSource';
    priority = -10;
    env;
    /**
     * @param env - Environment object to read from. Defaults to `process.env`.
     */
    constructor(env) {
        this.env = env ?? process.env;
    }
    load(_options) {
        const logger = getLogger();
        const config = {};
        for (const [envVar, configField] of Object.entries(ENV_VAR_MAP)) {
            const value = this.env[envVar];
            if (value === undefined || value === '')
                continue;
            if (BOOLEAN_FIELDS.has(configField)) {
                config[configField] = value === 'true' || value === '1';
            }
            else if (ARRAY_FIELDS.has(configField)) {
                config[configField] = value
                    .split(',')
                    .map((s) => s.trim())
                    .filter(Boolean);
            }
            else {
                config[configField] = value;
            }
        }
        const fields = getPopulatedFields(config);
        if (fields.length === 0) {
            logger.trace('[EnvSource] No SFCC_* environment variables found');
            return undefined;
        }
        logger.trace({ fields }, '[EnvSource] Loaded config from environment variables');
        return { config, location: 'environment variables' };
    }
}
//# sourceMappingURL=env-source.js.map