/**
 * Version Service
 *
 * Infrastructure service that reads version information from package.json.
 * Follows Clean Architecture: Infrastructure layer implements data access.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { existsSync, readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { injectable } from 'tsyringe';
import { DEFAULT_VERSION_INFO } from '../../domain/value-objects/version-info.js';
/**
 * Find the root package.json by traversing up from a starting directory.
 * Skips workspace sub-packages (e.g. packages/core/package.json) that lack
 * the required version/name/description fields.
 * Works in both development (src/) and production (dist/) environments.
 */
function findPackageJson(startDir) {
    let currentDir = startDir;
    // Traverse up to find package.json (max 10 levels for packages/core depth)
    for (let i = 0; i < 10; i++) {
        const pkgPath = join(currentDir, 'package.json');
        if (existsSync(pkgPath)) {
            try {
                const content = readFileSync(pkgPath, 'utf-8');
                const parsed = JSON.parse(content);
                // Only accept package.json with all required fields (skip workspace sub-packages)
                if (typeof parsed.version === 'string' &&
                    typeof parsed.name === 'string' &&
                    typeof parsed.description === 'string') {
                    return pkgPath;
                }
            }
            catch {
                // Malformed JSON, continue traversing
            }
        }
        const parentDir = dirname(currentDir);
        if (parentDir === currentDir)
            break; // Reached filesystem root
        currentDir = parentDir;
    }
    return null;
}
/**
 * Service for reading version information from package.json
 */
let VersionService = class VersionService {
    versionInfo;
    constructor() {
        this.versionInfo = this.loadVersionInfo();
    }
    loadVersionInfo() {
        try {
            const __dirname = dirname(fileURLToPath(import.meta.url));
            const packageJsonPath = findPackageJson(__dirname);
            if (!packageJsonPath) {
                return DEFAULT_VERSION_INFO;
            }
            const content = readFileSync(packageJsonPath, 'utf-8');
            const parsed = JSON.parse(content);
            // Validate parsed JSON has required fields
            if (typeof parsed === 'object' &&
                parsed !== null &&
                'version' in parsed &&
                'name' in parsed &&
                'description' in parsed &&
                typeof parsed.version === 'string' &&
                typeof parsed.name === 'string' &&
                typeof parsed.description === 'string') {
                return {
                    version: parsed.version,
                    name: parsed.name,
                    description: parsed.description,
                };
            }
            return DEFAULT_VERSION_INFO;
        }
        catch {
            // If anything goes wrong, return defaults to keep CLI functional
            return DEFAULT_VERSION_INFO;
        }
    }
    /**
     * Get version information
     */
    getVersion() {
        return this.versionInfo;
    }
};
VersionService = __decorate([
    injectable(),
    __metadata("design:paramtypes", [])
], VersionService);
export { VersionService };
/**
 * Set version info as NEXT_PUBLIC environment variables.
 * Must be called BEFORE starting the Next.js web server
 * so the values are available to the web UI.
 */
export function setVersionEnvVars(info) {
    process.env.NEXT_PUBLIC_SHEP_VERSION = info.version;
    process.env.NEXT_PUBLIC_SHEP_PACKAGE_NAME = info.name;
    process.env.NEXT_PUBLIC_SHEP_DESCRIPTION = info.description;
    process.env.NEXT_PUBLIC_SHEP_INSTANCE_PATH = process.cwd();
    // Branch is intentionally not set in production mode — only dev mode shows it
}
