package org.implab.gradle.variants.model;

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

import groovy.lang.Closure;

/**
 * Root extension:
 *
 * variants {
 *     layers { ... }
 *     roles { ... }
 *
 *     variant("browser") {
 *         role("production") {
 *             layers("main", "generated", "mainRjs")
 *         }
 *     }
 * }
 */
public interface VariantsExtension {

    /**
     * Domain of layers.
     */
    NamedDomainObjectContainer<Layer> getLayers();

    /**
     * Domain of roles.
     */
    NamedDomainObjectContainer<Role> getRoles();

    /**
     * Declared variants.
     */
    NamedDomainObjectContainer<Variant> getVariantDefinitions();

    /**
     * Creates or returns an existing variant and configures it.
     */
    default Variant variant(String name) {
        return getVariantDefinitions().maybeCreate(name);
    }

    /**
     * Creates or returns an existing variant and configures it.
     */
    default Variant variant(String name, Action<? super Variant> action) {
        var variant = variant(name);
        action.execute(variant);
        return variant;
    }

    default Variant variant(String name, Closure<?> closure) {
        return variant(name, Closures.action(closure));
    }
}