"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDockerConfigSummary = exports.getInstalledCcsVersion = exports.getDockerAssetPaths = exports.DOCKER_LOG_FILES = exports.DOCKER_DEFAULT_PROXY_PORT = exports.DOCKER_DEFAULT_DASHBOARD_PORT = exports.DOCKER_CONTAINER_NAME = exports.DOCKER_COMPOSE_SERVICE = exports.DOCKER_REMOTE_DIR = void 0;
const path = __importStar(require("path"));
const config_loader_facade_1 = require("../config/config-loader-facade");
exports.DOCKER_REMOTE_DIR = '~/.ccs/docker';
exports.DOCKER_COMPOSE_SERVICE = 'ccs-cliproxy';
exports.DOCKER_CONTAINER_NAME = 'ccs-cliproxy';
exports.DOCKER_DEFAULT_DASHBOARD_PORT = 3000;
exports.DOCKER_DEFAULT_PROXY_PORT = 8317;
exports.DOCKER_LOG_FILES = {
    ccs: '/var/log/ccs/ccs-dashboard.log',
    cliproxy: '/var/log/ccs/cliproxy.log',
};
let hasWarnedAboutVersionFallback = false;
function getPackageRoot() {
    return path.resolve(__dirname, '..', '..');
}
function ensureBundledAsset(assetPath) {
    try {
        require.resolve(assetPath);
    }
    catch {
        throw new Error(`Missing bundled Docker asset: ${assetPath}\nReinstall CCS or use a package build that includes docker/ assets.`);
    }
}
function warnAboutVersionFallback(error) {
    if (hasWarnedAboutVersionFallback) {
        return;
    }
    hasWarnedAboutVersionFallback = true;
    const detail = error instanceof Error ? error.message : String(error);
    console.error(`[!] Failed to determine the installed CCS version from package metadata; falling back to latest.\n${detail}`);
}
function getDockerAssetPaths() {
    const packageRoot = getPackageRoot();
    const dockerDir = path.join(packageRoot, 'docker');
    const assets = {
        dockerDir,
        composeFile: path.join(dockerDir, 'docker-compose.integrated.yml'),
        dockerfile: path.join(dockerDir, 'Dockerfile.integrated'),
        supervisordConfig: path.join(dockerDir, 'supervisord.conf'),
        entrypoint: path.join(dockerDir, 'entrypoint-integrated.sh'),
    };
    ensureBundledAsset(assets.composeFile);
    ensureBundledAsset(assets.dockerfile);
    ensureBundledAsset(assets.supervisordConfig);
    ensureBundledAsset(assets.entrypoint);
    return assets;
}
exports.getDockerAssetPaths = getDockerAssetPaths;
function getInstalledCcsVersion() {
    const packageJsonPath = path.join(getPackageRoot(), 'package.json');
    try {
        const packageJson = require(packageJsonPath);
        return packageJson.version?.trim() || 'latest';
    }
    catch (error) {
        warnAboutVersionFallback(error);
        return 'latest';
    }
}
exports.getInstalledCcsVersion = getInstalledCcsVersion;
function createDockerConfigSummary(options) {
    const assets = getDockerAssetPaths();
    return {
        host: options.host,
        remote: Boolean(options.host),
        ccsDir: (0, config_loader_facade_1.getCcsDir)(),
        dockerDir: assets.dockerDir,
        composeFile: assets.composeFile,
        dockerfile: assets.dockerfile,
        supervisordConfig: assets.supervisordConfig,
        entrypoint: assets.entrypoint,
        remoteDeployDir: exports.DOCKER_REMOTE_DIR,
        composeService: exports.DOCKER_COMPOSE_SERVICE,
        containerName: exports.DOCKER_CONTAINER_NAME,
        dashboardPort: options.port ?? exports.DOCKER_DEFAULT_DASHBOARD_PORT,
        proxyPort: options.proxyPort ?? exports.DOCKER_DEFAULT_PROXY_PORT,
    };
}
exports.createDockerConfigSummary = createDockerConfigSummary;
//# sourceMappingURL=docker-assets.js.map