package org.implab.gradle.variants.sources;

import org.gradle.api.Action;
import org.implab.gradle.common.core.lang.Closures;
import org.implab.gradle.variants.core.Layer;
import org.implab.gradle.variants.core.Variant;

import groovy.lang.Closure;

/**
 * Source-set materialization context for one compile unit.
 *
 * <p>This type keeps compile-unit identity next to the materialized
 * {@link GenericSourceSet} without turning the selector DSL into a second
 * source-set API. Use {@link #getCompileUnit()} for selection metadata and
 * {@link #sourceSet(Action)} when the underlying source set body must be
 * configured.
 */
public interface CompileUnitSourceSetSpec {
    /**
     * Returns the compile unit represented by this source set.
     *
     * @return compile-unit identity
     */
    CompileUnit getCompileUnit();

    /**
     * Returns the variant part of the compile-unit identity.
     *
     * @return variant identity
     */
    default Variant getVariant() {
        return getCompileUnit().variant();
    }

    /**
     * Returns the layer part of the compile-unit identity.
     *
     * @return layer identity
     */
    default Layer getLayer() {
        return getCompileUnit().layer();
    }

    /**
     * Returns the materialized source set body.
     *
     * @return source set body
     */
    GenericSourceSet getSourceSet();

    /**
     * Configures the materialized source set body.
     *
     * @param action source set configuration action
     */
    default void sourceSet(Action<? super GenericSourceSet> action) {
        action.execute(getSourceSet());
    }

    /**
     * Configures the materialized source set body.
     *
     * @param closure source set configuration closure
     */
    default void sourceSet(Closure<?> closure) {
        sourceSet(Closures.action(closure));
    }
}
