/**
 * TypespecFreshnessDiagnostic
 *
 * Compares the most-recently-edited `.tsp` source under `tsp/` against the
 * generated `domain/generated/output.ts`. If a TypeSpec source is newer
 * than the generated file, the contributor likely forgot to run
 * `pnpm tsp:compile` and downstream type errors are imminent.
 */
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { inject, injectable } from 'tsyringe';
import { readdirSync, statSync } from 'node:fs';
import path from 'node:path';
import { DiagnosticStatus } from '../../../../domain/generated/output.js';
const GENERATED_RELATIVE = 'packages/core/src/domain/generated/output.ts';
let TypespecFreshnessDiagnostic = class TypespecFreshnessDiagnostic {
    fileSystem;
    workspaceRoot;
    name = 'typespec-freshness';
    constructor(fileSystem, workspaceRoot = process.cwd()) {
        this.fileSystem = fileSystem;
        this.workspaceRoot = workspaceRoot;
    }
    async run() {
        const tspDir = path.join(this.workspaceRoot, 'tsp');
        const generatedPath = path.join(this.workspaceRoot, GENERATED_RELATIVE);
        if (!this.fileSystem.pathExists(tspDir)) {
            return {
                name: this.name,
                status: DiagnosticStatus.Warn,
                detail: 'No `tsp/` directory at workspace root — skipping freshness check',
            };
        }
        if (!this.fileSystem.pathExists(generatedPath)) {
            return {
                name: this.name,
                status: DiagnosticStatus.Fail,
                detail: 'Generated output `domain/generated/output.ts` is missing',
                fixHint: 'Run `pnpm tsp:compile` to regenerate domain types',
            };
        }
        const generatedMtime = statSync(generatedPath).mtimeMs;
        const newestTspMtime = newestMtime(tspDir);
        if (newestTspMtime > generatedMtime) {
            return {
                name: this.name,
                status: DiagnosticStatus.Warn,
                detail: 'TypeSpec sources are newer than the generated output',
                fixHint: 'Run `pnpm tsp:compile` to regenerate `domain/generated/output.ts`',
            };
        }
        return {
            name: this.name,
            status: DiagnosticStatus.Ok,
            detail: 'Generated TypeSpec output is up to date',
        };
    }
};
TypespecFreshnessDiagnostic = __decorate([
    injectable(),
    __param(0, inject('IFileSystemService')),
    __metadata("design:paramtypes", [Object, String])
], TypespecFreshnessDiagnostic);
export { TypespecFreshnessDiagnostic };
function newestMtime(root) {
    let newest = 0;
    for (const entry of readdirSync(root, { withFileTypes: true })) {
        const child = path.join(root, entry.name);
        if (entry.isDirectory()) {
            newest = Math.max(newest, newestMtime(child));
        }
        else if (entry.isFile() && entry.name.endsWith('.tsp')) {
            newest = Math.max(newest, statSync(child).mtimeMs);
        }
    }
    return newest;
}
