package org.implab.gradle.common.dsl;

import java.util.Optional;
import java.util.function.Supplier;

import org.gradle.api.provider.Provider;
import org.implab.gradle.common.exec.RedirectFrom;

public class RedirectFromSpec {
    private Supplier<RedirectFrom> streamRedirect;
    
    public boolean isRedirected() {
        return streamRedirect != null;
    }

    public Optional<RedirectFrom> getRedirection() {
        return streamRedirect != null ? Optional.ofNullable(streamRedirect.get()) : Optional.empty();
    }

    public void from(Object input) {
        if (input instanceof Provider<?> inputProvider) {
            this.streamRedirect = inputProvider.map(RedirectFrom::any)::get;
        } else {
            this.streamRedirect = () -> RedirectFrom.any(input);
        }
    }
}
