/**
 * Add Repository Use Case
 *
 * Creates a new Repository entity from a filesystem path.
 * Normalizes the path and returns existing repository if path already tracked.
 */
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 { injectable, inject } from 'tsyringe';
import { randomUUID } from 'node:crypto';
import { normalizePath } from '../../../domain/shared/normalize-path.js';
let AddRepositoryUseCase = class AddRepositoryUseCase {
    repositoryRepo;
    constructor(repositoryRepo) {
        this.repositoryRepo = repositoryRepo;
    }
    async execute(input) {
        const normalizedPath = normalizePath(input.path);
        // Check for existing active repository with same path
        const existing = await this.repositoryRepo.findByPath(normalizedPath);
        if (existing) {
            return existing;
        }
        // Check for soft-deleted repository — restore it instead of creating a duplicate.
        // Reset createdAt so the restored repo sorts as "newest" on the canvas
        // (otherwise the old createdAt would place it above repos added later).
        const deleted = await this.repositoryRepo.findByPathIncludingDeleted(normalizedPath);
        if (deleted) {
            const now = new Date();
            await this.repositoryRepo.restore(deleted.id);
            return { ...deleted, deletedAt: undefined, createdAt: now, updatedAt: now };
        }
        const now = new Date();
        const name = input.name ?? normalizedPath.split('/').pop() ?? normalizedPath;
        const repository = {
            id: randomUUID(),
            name,
            path: normalizedPath,
            bedrockEnabled: false,
            createdAt: now,
            updatedAt: now,
        };
        await this.repositoryRepo.create(repository);
        return repository;
    }
};
AddRepositoryUseCase = __decorate([
    injectable(),
    __param(0, inject('IRepositoryRepository')),
    __metadata("design:paramtypes", [Object])
], AddRepositoryUseCase);
export { AddRepositoryUseCase };
