/**
 * OperationLogService
 *
 * Default implementation of IOperationLogService. Delegates persistence to
 * IOperationLogRepository and provides convenience methods for each level.
 *
 * Failure-isolation rule: log writes must NEVER raise out of a use case in a
 * way that masks the original operation error. If the repository throws (DB
 * locked, disk full, etc.) we swallow and warn via ILogger so the user still
 * gets the meaningful "deploy failed" outcome — losing a log line is much
 * better than turning a failed deploy into a 500 with a stack trace from the
 * logger.
 */
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 { OperationLogLevel, } from '../../../domain/generated/output.js';
let OperationLogService = class OperationLogService {
    repo;
    logger;
    constructor(repo, logger) {
        this.repo = repo;
        this.logger = logger;
    }
    debug(kind, id, message, detail) {
        return this.appendSafe(kind, id, OperationLogLevel.Debug, message, detail);
    }
    info(kind, id, message, detail) {
        return this.appendSafe(kind, id, OperationLogLevel.Info, message, detail);
    }
    warn(kind, id, message, detail) {
        return this.appendSafe(kind, id, OperationLogLevel.Warn, message, detail);
    }
    error(kind, id, message, detail) {
        return this.appendSafe(kind, id, OperationLogLevel.Error, message, detail);
    }
    list(kind, id) {
        return this.repo.listByScope(kind, id);
    }
    async appendSafe(kind, id, level, message, detail) {
        try {
            return await this.repo.append({
                operationKind: kind,
                operationId: id,
                level,
                message,
                detail,
            });
        }
        catch (err) {
            // Don't surface — losing one log line beats turning a deploy into a 500.
            this.logger.warn('failed to append operation log entry', {
                kind,
                id,
                level,
                err: err instanceof Error ? err.message : String(err),
            });
            // Return a synthetic entry so callers don't get null. Created/updated
            // are stamped with `now` so the UI still gets a sensible ordering even
            // if the DB write was lost.
            const now = new Date();
            return {
                id: `unpersisted-${now.getTime()}`,
                operationKind: kind,
                operationId: id,
                level,
                message,
                detail,
                createdAt: now,
                updatedAt: now,
            };
        }
    }
};
OperationLogService = __decorate([
    injectable(),
    __param(0, inject('IOperationLogRepository')),
    __param(1, inject('ILogger')),
    __metadata("design:paramtypes", [Object, Object])
], OperationLogService);
export { OperationLogService };
