using System;
using System.Collections.Generic;
using FluentValidation;
using MfGames.Gallium;
using Zio;
namespace MfGames.Nitride.IO.Paths;
///
/// A pipeline operation that replaces the UPath of the given entity
/// with the results of a lambda output. Entities without a path component
/// are passed on without touching.
///
[WithProperties]
public partial class ReplacePath : IOperation
{
private readonly IValidator validator;
public ReplacePath(IValidator validator)
{
this.validator = validator;
}
///
/// Gets or sets the replacement callback to alter the paths.
///
public Func Replacement { get; set; } = null!;
///
/// Performs the replacement on the input streams and outputs the
/// resulting entities. Only entities that have had their paths changed
/// will be updated, the others will be passed on as-is.
///
/// The list of input entities.
/// The output entities.
public IEnumerable Run(IEnumerable input)
{
this.validator.ValidateAndThrow(this);
return input.SelectEntity(
(
entity,
oldPath) =>
{
UPath newPath = this.Replacement(entity, oldPath);
return newPath != oldPath ? entity.Set(newPath) : entity;
});
}
}