/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import * as fs from 'fs';
import * as path from 'path';
import { CLI_CONFIG_DIRECTORY, USER_SETTINGS_FILE } from '../Constants.js';
import { printWarning } from '../Utils/ConsoleOutput.js';
/**
 * Checks if a string represents a truthy value (1, true, yes, on).
 */
function isTruthy(value) {
    if (!value) {
        return false;
    }
    const normalized = value.trim().toLowerCase();
    return normalized === '1' || normalized === 'true' || normalized === 'yes' || normalized === 'on';
}
/**
 * Checks if a string represents a falsy value (0, false, no, off).
 */
function isFalsy(value) {
    if (!value) {
        return false;
    }
    const normalized = value.trim().toLowerCase();
    return (normalized === '0' || normalized === 'false' || normalized === 'no' || normalized === 'off');
}
/**
 * Determines whether PAC CLI telemetry is enabled by checking environment variables and PAC CLI config.
 *
 * Checks in order:
 * 1. PAC_CLI_TELEMETRY_OPTOUT environment variable (if truthy, returns false)
 * 2. PAC CLI usersettings.json file in %LOCALAPPDATA%\Microsoft\PowerAppsCli
 *
 * @returns `false` if explicitly disabled, `true` if explicitly enabled, `undefined` if not configured
 */
function isPacCliTelemetryEnabled() {
    if (isTruthy(process.env.PAC_CLI_TELEMETRY_OPTOUT)) {
        return false;
    }
    const localAppData = process.env.LOCALAPPDATA;
    if (!localAppData) {
        return undefined;
    }
    const userSettingsPath = path.join(localAppData, 'Microsoft', 'PowerAppsCli', 'usersettings.json');
    if (!fs.existsSync(userSettingsPath)) {
        return undefined;
    }
    try {
        const raw = fs.readFileSync(userSettingsPath, 'utf-8');
        const parsed = JSON.parse(raw);
        return typeof parsed.telemetryEnabled === 'boolean' ? parsed.telemetryEnabled : undefined;
    }
    catch (error) {
        printWarning(`Failed to read PAC CLI telemetry settings: ${error.message}`);
        return undefined;
    }
}
/**
 * Resolves the effective user telemetry settings by merging defaults, user config file, and environment overrides.
 *
 * Resolution priority (highest to lowest):
 * 1. Environment variable overrides:
 *    - POWERAPPS_CLI_TELEMETRY=0/false/no/off forces `enabled: false`
 *    - PAC_CLI_TELEMETRY_OPTOUT or PAC CLI usersettings.json `telemetryEnabled: false` forces `enabled: false`
 * 2. PowerApps CLI user settings file (~/.powerapps-cli/settings.json)
 * 3. Default values: `{ enabled: true, consoleOnly: false, outputToConsole: false }`
 *
 * @returns Resolved telemetry configuration with enabled, consoleOnly, and outputToConsole flags
 */
export function resolveUserTelemetrySettings() {
    const resolved = {
        enabled: true,
        consoleOnly: false,
        outputToConsole: false,
    };
    const cliUserSettingsPath = path.join(CLI_CONFIG_DIRECTORY, USER_SETTINGS_FILE);
    if (fs.existsSync(cliUserSettingsPath)) {
        try {
            const fileContents = fs.readFileSync(cliUserSettingsPath, 'utf-8');
            const parsed = JSON.parse(fileContents);
            resolved.enabled = parsed.enabled ?? resolved.enabled;
            resolved.consoleOnly = parsed.consoleOnly ?? resolved.consoleOnly;
            resolved.outputToConsole = parsed.outputToConsole ?? resolved.outputToConsole;
        }
        catch (error) {
            printWarning(`Failed to read CLI telemetry settings: ${error.message}`);
        }
    }
    if (process.env.POWERAPPS_CLI_TELEMETRY !== undefined &&
        isFalsy(process.env.POWERAPPS_CLI_TELEMETRY)) {
        resolved.enabled = false;
    }
    const pacCliSetting = isPacCliTelemetryEnabled();
    if (typeof pacCliSetting === 'boolean' && pacCliSetting === false) {
        resolved.enabled = false;
    }
    return resolved;
}
//# sourceMappingURL=LoggerSettings.js.map