import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Lists environment variables for a project environment.
 *
 * @param options - Options specifying project and environment
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns List of environment variables
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { listEnvVars } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const result = await listEnvVars({
 *   projectSlug: 'my-storefront',
 *   environment: 'production'
 * }, auth);
 *
 * for (const envVar of result.variables) {
 *   console.log(`${envVar.name}=${envVar.value}`);
 * }
 * ```
 */
export async function listEnvVars(options, auth) {
    const logger = getLogger();
    const { projectSlug, environment, origin } = options;
    logger.debug({ projectSlug, environment }, '[MRT] Listing environment variables');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/projects/{project_slug}/target/{target_slug}/env-var/', {
        params: {
            path: {
                project_slug: projectSlug,
                target_slug: environment,
            },
        },
    });
    if (error) {
        throw new Error(`Failed to list environment variables: ${JSON.stringify(error)}`);
    }
    // The API can return two formats:
    // 1. Paginated: { count, next, previous, results: [{ "VAR_NAME": {...} }, ...] }
    // 2. Direct object: { "VAR_NAME": {...}, "VAR_NAME2": {...}, ... }
    const variables = [];
    const responseData = data;
    // Check if it's the paginated format (has 'results' array)
    if (responseData?.results && Array.isArray(responseData.results)) {
        for (const item of responseData.results) {
            const entries = Object.entries(item);
            for (const [name, metadata] of entries) {
                const meta = metadata;
                variables.push(parseEnvVarMetadata(name, meta));
            }
        }
    }
    else if (responseData) {
        // Direct object format - each key is an env var name (skip pagination fields)
        const paginationFields = ['count', 'next', 'previous', 'results'];
        for (const [name, metadata] of Object.entries(responseData)) {
            if (paginationFields.includes(name))
                continue;
            const meta = metadata;
            // Verify it looks like env var metadata (has 'value' property)
            if (meta && typeof meta === 'object' && 'value' in meta) {
                variables.push(parseEnvVarMetadata(name, meta));
            }
        }
    }
    logger.debug({ count: variables.length }, '[MRT] Listed environment variables');
    return {
        count: typeof responseData?.count === 'number' ? responseData.count : variables.length,
        variables,
    };
}
/**
 * Parses environment variable metadata from API response.
 */
function parseEnvVarMetadata(name, meta) {
    return {
        name,
        value: String(meta.value ?? ''),
        createdBy: String(meta.created_by ?? ''),
        createdAt: String(meta.created_at ?? ''),
        updatedAt: String(meta.updated_at ?? ''),
        updatedBy: String(meta.updated_by ?? ''),
        publishingStatus: Number(meta.publishing_status ?? 0),
        publishingStatusDescription: String(meta.publishing_status_description ?? ''),
    };
}
/**
 * Sets an environment variable on a project environment.
 *
 * Creates the variable if it doesn't exist, or updates it if it does.
 *
 * @param options - Options specifying project, environment, and variable
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { setEnvVar } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * await setEnvVar({
 *   projectSlug: 'my-storefront',
 *   environment: 'production',
 *   key: 'API_KEY',
 *   value: 'secret-value'
 * }, auth);
 * ```
 */
export async function setEnvVar(options, auth) {
    const logger = getLogger();
    const { projectSlug, environment, key, value, origin } = options;
    logger.debug({ projectSlug, environment, key }, '[MRT] Setting environment variable');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { error } = await client.PATCH('/api/projects/{project_slug}/target/{target_slug}/env-var/', {
        params: {
            path: {
                project_slug: projectSlug,
                target_slug: environment,
            },
        },
        body: {
            [key]: { value },
        },
    });
    if (error) {
        throw new Error(`Failed to set environment variable: ${JSON.stringify(error)}`);
    }
    logger.debug({ projectSlug, environment, key }, '[MRT] Environment variable set');
}
/**
 * Sets multiple environment variables on a project environment.
 *
 * Creates variables if they don't exist, or updates them if they do.
 *
 * @param options - Options specifying project, environment, and variables
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { setEnvVars } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * await setEnvVars({
 *   projectSlug: 'my-storefront',
 *   environment: 'production',
 *   variables: {
 *     API_KEY: 'secret-value',
 *     DEBUG: 'false'
 *   }
 * }, auth);
 * ```
 */
export async function setEnvVars(options, auth) {
    const logger = getLogger();
    const { projectSlug, environment, variables, origin } = options;
    const keys = Object.keys(variables);
    logger.debug({ projectSlug, environment, count: keys.length }, '[MRT] Setting environment variables');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    // Build body with {key: {value}} format for each variable
    const body = {};
    for (const [key, value] of Object.entries(variables)) {
        body[key] = { value };
    }
    const { error } = await client.PATCH('/api/projects/{project_slug}/target/{target_slug}/env-var/', {
        params: {
            path: {
                project_slug: projectSlug,
                target_slug: environment,
            },
        },
        body,
    });
    if (error) {
        throw new Error(`Failed to set environment variables: ${JSON.stringify(error)}`);
    }
    logger.debug({ projectSlug, environment, keys }, '[MRT] Environment variables set');
}
/**
 * Deletes an environment variable from a project environment.
 *
 * @param options - Options specifying project, environment, and variable name
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { deleteEnvVar } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * await deleteEnvVar({
 *   projectSlug: 'my-storefront',
 *   environment: 'production',
 *   key: 'OLD_API_KEY'
 * }, auth);
 * ```
 */
export async function deleteEnvVar(options, auth) {
    const logger = getLogger();
    const { projectSlug, environment, key, origin } = options;
    logger.debug({ projectSlug, environment, key }, '[MRT] Deleting environment variable');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { error } = await client.PATCH('/api/projects/{project_slug}/target/{target_slug}/env-var/', {
        params: {
            path: {
                project_slug: projectSlug,
                target_slug: environment,
            },
        },
        body: {
            [key]: { value: null },
        },
    });
    if (error) {
        throw new Error(`Failed to delete environment variable: ${JSON.stringify(error)}`);
    }
    logger.debug({ projectSlug, environment, key }, '[MRT] Environment variable deleted');
}
//# sourceMappingURL=env-var.js.map