/*
 * Copyright (c) 2025, Salesforce, Inc.
 * SPDX-License-Identifier: Apache-2
 * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
 */
/**
 * dw.json configuration source.
 *
 * @internal This module is internal to the SDK. Use ConfigResolver instead.
 */
import { loadDwJson, loadFullDwJson, addInstance, removeInstance, setActiveInstance } from '../dw-json.js';
import { getPopulatedFields, mapDwJsonToNormalizedConfig, mapNormalizedConfigToDwJson } from '../mapping.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Configuration source that loads from dw.json files.
 *
 * @internal
 */
export class DwJsonSource {
    name = 'DwJsonSource';
    priority = 0;
    load(options) {
        const logger = getLogger();
        const result = loadDwJson({
            instance: options.instance,
            path: options.configPath,
            projectDirectory: options.projectDirectory ?? options.workingDirectory,
        });
        if (!result) {
            return undefined;
        }
        const config = mapDwJsonToNormalizedConfig(result.config);
        const fields = getPopulatedFields(config);
        logger.trace({ location: result.path, fields }, '[DwJsonSource] Loaded config');
        return { config, location: result.path };
    }
    /**
     * List all instances from dw.json.
     */
    listInstances(options) {
        const result = loadFullDwJson({
            path: options?.configPath,
            projectDirectory: options?.projectDirectory ?? options?.workingDirectory,
        });
        if (!result) {
            return [];
        }
        const instances = [];
        const { config, path: dwJsonPath } = result;
        // Add root config if it has a name
        if (config.name) {
            instances.push({
                name: config.name,
                hostname: config.hostname,
                active: config.active,
                source: this.name,
                location: dwJsonPath,
            });
        }
        // Add configs array entries
        if (config.configs) {
            for (const c of config.configs) {
                if (c.name) {
                    instances.push({
                        name: c.name,
                        hostname: c.hostname,
                        active: c.active,
                        source: this.name,
                        location: dwJsonPath,
                    });
                }
            }
        }
        return instances;
    }
    /**
     * Create a new instance in dw.json.
     */
    createInstance(options) {
        const dwJsonConfig = mapNormalizedConfigToDwJson(options.config, options.name);
        addInstance(dwJsonConfig, {
            path: options.configPath,
            projectDirectory: options.projectDirectory ?? options.workingDirectory,
            setActive: options.setActive,
        });
    }
    /**
     * Remove an instance from dw.json.
     */
    removeInstance(name, options) {
        removeInstance(name, {
            path: options?.configPath,
            projectDirectory: options?.projectDirectory ?? options?.workingDirectory,
        });
    }
    /**
     * Set an instance as active in dw.json.
     */
    setActiveInstance(name, options) {
        setActiveInstance(name, {
            path: options?.configPath,
            projectDirectory: options?.projectDirectory ?? options?.workingDirectory,
        });
    }
}
//# sourceMappingURL=dw-json-source.js.map