"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.envLoader = exports.DEFAULT_ENV = void 0;
const dotenv = __importStar(require("dotenv"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const path_helpers_1 = require("./path-helpers");
const tool_state_1 = require("./tool-state");
exports.DEFAULT_ENV = {
    HOST_DOMAIN: 'local.developers-homelab.net',
    PREFIX: 'runestone',
    HTTPS_PORT: '443',
    HTTP_PORT: '80',
    SMTP_PORT: '1025',
    RUNESTONE_IMAGE: 'cymondez/runestone',
    RUNESTONE_TAG: '5.2',
    MKCERT_INSTALLED: 'false',
    RUNESTONE_VERSION: '5',
    WEB_ENTRYPOINT_PORT: '80',
    WEB_ENTRYPOINT_NAME: 'web',
    WEB_SECURE_ENTRYPOINT_PORT: '443',
    WEB_SECURE_ENTRYPOINT_NAME: 'websecure'
};
function readEnvFile(envPath) {
    if (!fs.existsSync(envPath)) {
        return {};
    }
    return dotenv.parse(fs.readFileSync(envPath));
}
exports.envLoader = {
    load(cliEnvPath, projectArg) {
        const projectDir = path_helpers_1.pathHelpers.resolveProjectDir(projectArg);
        const envPath = path_helpers_1.pathHelpers.resolveEnvPath(cliEnvPath, projectDir);
        const envFileExists = fs.existsSync(envPath);
        const parsed = readEnvFile(envPath);
        const merged = { ...exports.DEFAULT_ENV, ...parsed };
        const prefix = merged.PREFIX || exports.DEFAULT_ENV.PREFIX;
        return {
            HOST_DOMAIN: merged.HOST_DOMAIN || exports.DEFAULT_ENV.HOST_DOMAIN,
            PREFIX: prefix,
            HTTPS_PORT: merged.HTTPS_PORT || exports.DEFAULT_ENV.HTTPS_PORT,
            HTTP_PORT: merged.HTTP_PORT || exports.DEFAULT_ENV.HTTP_PORT,
            SMTP_PORT: merged.SMTP_PORT || exports.DEFAULT_ENV.SMTP_PORT,
            RUNESTONE_IMAGE: merged.RUNESTONE_IMAGE || exports.DEFAULT_ENV.RUNESTONE_IMAGE,
            RUNESTONE_TAG: merged.RUNESTONE_TAG || exports.DEFAULT_ENV.RUNESTONE_TAG,
            MKCERT_INSTALLED: merged.MKCERT_INSTALLED || exports.DEFAULT_ENV.MKCERT_INSTALLED,
            RUNESTONE_VERSION: merged.RUNESTONE_VERSION || exports.DEFAULT_ENV.RUNESTONE_VERSION,
            RUNESTONE_LANG: tool_state_1.toolState.readLocale() || 'en',
            WEB_ENTRYPOINT_PORT: merged.WEB_ENTRYPOINT_PORT || merged.HTTP_PORT || exports.DEFAULT_ENV.WEB_ENTRYPOINT_PORT,
            WEB_ENTRYPOINT_NAME: merged.WEB_ENTRYPOINT_NAME || exports.DEFAULT_ENV.WEB_ENTRYPOINT_NAME,
            WEB_SECURE_ENTRYPOINT_PORT: merged.WEB_SECURE_ENTRYPOINT_PORT || merged.HTTPS_PORT || exports.DEFAULT_ENV.WEB_SECURE_ENTRYPOINT_PORT,
            WEB_SECURE_ENTRYPOINT_NAME: merged.WEB_SECURE_ENTRYPOINT_NAME || exports.DEFAULT_ENV.WEB_SECURE_ENTRYPOINT_NAME,
            PROJECT_DIR: projectDir,
            ENV_PATH: envPath,
            COMPOSE_FILE_PATH: path_helpers_1.pathHelpers.resolveComposePath(projectArg),
            NETWORK_NAME: `${prefix}-network`,
            SSH_VOLUME_NAME: `${prefix}-ssh`,
            ENV_FILE_EXISTS: envFileExists,
            REQUIRED_VARS_PRESENT: Boolean(parsed.HOST_DOMAIN && parsed.PREFIX)
        };
    },
    write(envPath, config) {
        const dir = path.dirname(envPath);
        fs.mkdirSync(dir, { recursive: true });
        const existing = readEnvFile(envPath);
        const merged = { ...existing, ...config };
        const content = Object.entries(merged)
            .filter(([, value]) => value !== undefined)
            .map(([key, value]) => `${key}=${value}`)
            .join('\n');
        fs.writeFileSync(envPath, `${content}\n`, 'utf8');
    },
    exists(envPath) {
        return fs.existsSync(path.resolve(envPath));
    },
    hasRequiredVars(config) {
        return config.ENV_FILE_EXISTS && config.REQUIRED_VARS_PRESENT;
    }
};
//# sourceMappingURL=env-loader.js.map