"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDownCommand = createDownCommand;
const docker_compose_1 = require("../services/docker-compose");
const docker_network_1 = require("../services/docker-network");
const docker_volume_1 = require("../services/docker-volume");
const env_loader_1 = require("../utils/env-loader");
const logger_1 = require("../utils/logger");
const i18n_1 = require("../i18n");
const command_1 = require("../utils/command");
function ignoreMissingResource(error) {
    const message = error instanceof Error ? error.message : String(error);
    return /not found|no such/i.test(message);
}
function createDownCommand() {
    return (0, command_1.createCommand)('down')
        .description((0, i18n_1.t)('commands.down.description'))
        .option('--remove-network', (0, i18n_1.t)('options.removeNetwork.description'))
        .option('--remove-volumes', (0, i18n_1.t)('options.removeVolumes.description'))
        .option('--purge', (0, i18n_1.t)('options.purge.description'))
        .action((options) => {
        try {
            const config = env_loader_1.envLoader.load();
            const removeVolumes = Boolean(options.removeVolumes || options.purge);
            const removeNetwork = Boolean(options.removeNetwork || options.purge);
            docker_compose_1.composeService.down(config.COMPOSE_FILE_PATH, {
                removeVolumes,
                removeImages: Boolean(options.purge)
            });
            if (removeNetwork) {
                try {
                    docker_network_1.networkService.removeNetwork(config.NETWORK_NAME);
                }
                catch (error) {
                    if (!ignoreMissingResource(error)) {
                        throw error;
                    }
                }
            }
            if (removeVolumes) {
                try {
                    docker_volume_1.volumeService.removeVolume(config.SSH_VOLUME_NAME);
                }
                catch (error) {
                    if (!ignoreMissingResource(error)) {
                        throw error;
                    }
                }
            }
            logger_1.logger.success(options.purge ? 'Full purge complete.' : 'Teardown complete.');
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            logger_1.logger.error(`Failed to tear down environment: ${message}`);
            process.exit(1);
        }
    });
}
//# sourceMappingURL=down.js.map