package org.implab.gradle.common.sources;

import org.implab.gradle.common.core.lang.Closures;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectProvider;

import groovy.lang.Closure;
import groovy.lang.DelegatesTo;

/**
 * Immutable context for a {@link GenericSourceSet} registered for a resolved
 * variant/layer pair.
 *
 * <p>Used as callback payload when source sets are registered or bound in
 * {@link VariantSourcesExtension} and then dispatched via
 * {@link VariantSourcesExtension#whenRegistered(org.gradle.api.Action)},
 * {@link VariantSourcesExtension#whenBound(org.gradle.api.Action)},
 * {@link BuildLayerBinding#whenRegistered(org.gradle.api.Action)} and
 * {@link BuildLayerBinding#whenBound(org.gradle.api.Action)}.
 *
 * @param variantName variant name from the build-variants model
 * @param roleName role name inside the resolved variant
 * @param layerName normalized layer name used to register the source set
 * @param sourceSetName source-set name registered in the container
 * @param sourceSet provider of the registered source set (realized later by Gradle on demand)
 */
public record SourceSetContext(
        String variantName,
        String roleName,
        String layerName,
        String sourceSetName,
        NamedDomainObjectProvider<GenericSourceSet> sourceSet) {
    public void configureSourceSet(Action<? super GenericSourceSet> action) {
        sourceSet.configure(action);
    }

    public void configureSourceSet(
            @DelegatesTo(value = GenericSourceSet.class, strategy = Closure.DELEGATE_FIRST) Closure<?> action) {
        configureSourceSet(Closures.action(action));
    }
}
