/**
 * GetGitStatusUseCase
 *
 * Single read-side entry point for the SmartDeployButton. The Web hook
 * polls this every few seconds via GET /api/applications/:id/git/status
 * so the button label can react to working-tree drift in near-real-time.
 *
 * Pure read — no side effects, no log entries written. Logging only kicks
 * in when the user takes a write action (Sync, Publish, Deploy).
 *
 * Two sources of truth are merged:
 *
 *   1. **Persisted Application.gitRemoteUrl** — authoritative for "does
 *      this app have a remote at all". Set when the publish flow ran
 *      successfully and never cleared by drift detection. If we have
 *      this, we know the remote exists.
 *
 *   2. **Live `git remote -v`** — authoritative for current branch +
 *      uncommitted/unpushed counts. Drives drift display only.
 *
 * Mixing these defends against a class of bugs where a transient git
 * subprocess failure (Windows path quirk, race with another git command,
 * `.git` momentarily locked) would otherwise make the UI show "No backup
 * yet" for an app that's actually fully published. The persisted URL wins
 * the hasRemote decision unconditionally.
 */
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 { ApplicationNotFoundError } from '../../../domain/errors/application-not-found.error.js';
let GetGitStatusUseCase = class GetGitStatusUseCase {
    applicationRepo;
    gitRemoteService;
    constructor(applicationRepo, gitRemoteService) {
        this.applicationRepo = applicationRepo;
        this.gitRemoteService = gitRemoteService;
    }
    async execute(input) {
        const app = await this.applicationRepo.findById(input.applicationId);
        if (!app)
            throw new ApplicationNotFoundError(input.applicationId);
        const live = await this.gitRemoteService.getStatus(app.repositoryPath);
        // Persisted URL wins for hasRemote/remoteUrl. If the Application row
        // says it's published, treat it as published — even if the live git
        // subprocess didn't see the remote this tick.
        const persistedRemoteUrl = app.gitRemoteUrl ?? null;
        if (persistedRemoteUrl && persistedRemoteUrl.length > 0) {
            return {
                ...live,
                hasRemote: true,
                remoteUrl: live.remoteUrl ?? persistedRemoteUrl,
            };
        }
        return live;
    }
};
GetGitStatusUseCase = __decorate([
    injectable(),
    __param(0, inject('IApplicationRepository')),
    __param(1, inject('IGitRemoteService')),
    __metadata("design:paramtypes", [Object, Object])
], GetGitStatusUseCase);
export { GetGitStatusUseCase };
