/**
 * Enable Bedrock For Target Use Case
 *
 * Generalizes the application-only EnableBedrockForApplication use case
 * to support every bedrock-capable entity: Application | Repository |
 * Feature. The use case:
 *
 *   1. Resolves the target entity via its kind + id.
 *   2. Persists `bedrockEnabled = true` on that entity (idempotent —
 *      a no-op when already enabled).
 *   3. Invokes `IBedrockIntegrationService.init()` with the entity's
 *      resolved worktree path.
 *
 * This is the single entry point used by the CLI (`shep bedrock init`)
 * and every web server action (`enableBedrockForApplication`,
 * `enableBedrockForRepository`, `enableBedrockForFeature`).
 */
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 { ApplicationNotFoundError } from '../../../domain/errors/application-not-found.error.js';
import { BedrockTargetKind } from '../../../domain/generated/output.js';
let EnableBedrockForTargetUseCase = class EnableBedrockForTargetUseCase {
    appRepo;
    repoRepo;
    featureRepo;
    bedrock;
    constructor(appRepo, repoRepo, featureRepo, bedrock) {
        this.appRepo = appRepo;
        this.repoRepo = repoRepo;
        this.featureRepo = featureRepo;
        this.bedrock = bedrock;
    }
    async execute(input) {
        const target = await this.resolveTarget(input.kind, input.id);
        if (!target.alreadyEnabled) {
            await target.persistEnabled();
        }
        return this.bedrock.init({
            cwd: target.cwd,
            onProgress: input.onProgress,
        });
    }
    async resolveTarget(kind, id) {
        switch (kind) {
            case BedrockTargetKind.Application: {
                const app = await this.appRepo.findById(id);
                if (!app)
                    throw new ApplicationNotFoundError(id);
                return {
                    cwd: app.repositoryPath,
                    alreadyEnabled: app.bedrockEnabled === true,
                    persistEnabled: async () => {
                        await this.appRepo.update(app.id, { bedrockEnabled: true });
                    },
                };
            }
            case BedrockTargetKind.Repository: {
                const repo = await this.repoRepo.findById(id);
                if (!repo)
                    throw new Error(`Repository not found: ${id}`);
                return {
                    cwd: repo.path,
                    alreadyEnabled: repo.bedrockEnabled === true,
                    persistEnabled: async () => {
                        await this.repoRepo.update(repo.id, { bedrockEnabled: true });
                    },
                };
            }
            case BedrockTargetKind.Feature: {
                const feature = await this.featureRepo.findById(id);
                if (!feature)
                    throw new Error(`Feature not found: ${id}`);
                return {
                    cwd: feature.worktreePath ?? feature.repositoryPath,
                    alreadyEnabled: feature.bedrockEnabled === true,
                    persistEnabled: async () => {
                        await this.featureRepo.update({ ...feature, bedrockEnabled: true });
                    },
                };
            }
            default: {
                const unreachable = kind;
                throw new Error(`Unhandled BedrockTargetKind: ${String(unreachable)}`);
            }
        }
    }
};
EnableBedrockForTargetUseCase = __decorate([
    injectable(),
    __param(0, inject('IApplicationRepository')),
    __param(1, inject('IRepositoryRepository')),
    __param(2, inject('IFeatureRepository')),
    __param(3, inject('IBedrockIntegrationService')),
    __metadata("design:paramtypes", [Object, Object, Object, Object])
], EnableBedrockForTargetUseCase);
export { EnableBedrockForTargetUseCase };
