const TIMESTAMP_BY_STATE = {
    approved: 'approvedAt',
    merged: 'mergedAt',
};
export class TransitionStateUseCase {
    trackingGateway;
    constructor(trackingGateway) {
        this.trackingGateway = trackingGateway;
    }
    execute(input) {
        const mr = this.trackingGateway.getById(input.projectPath, input.mrId);
        if (!mr)
            return { ok: false, reason: 'not-found' };
        if (input.requireCurrentState && mr.state !== input.requireCurrentState) {
            return {
                ok: false,
                reason: 'invalid-current-state',
                currentState: mr.state,
            };
        }
        if (input.targetState === 'approved' && input.qualityCheck && mr.bypass === null) {
            const gateResult = input.qualityCheck(mr);
            if (!gateResult.allowed) {
                return { ok: false, reason: 'quality-gate', message: gateResult.message };
            }
        }
        const timestampField = TIMESTAMP_BY_STATE[input.targetState];
        this.trackingGateway.update(input.projectPath, input.mrId, {
            state: input.targetState,
            ...(timestampField ? { [timestampField]: new Date().toISOString() } : {}),
        });
        return { ok: true };
    }
}
//# sourceMappingURL=transitionState.usecase.js.map