package org.implab.gradle.variants.model;

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

import groovy.lang.Closure;

/**
 * A named variant, e.g. "browser", "electron".
 *
 * A variant does not "have a role" directly.
 * It owns a set of role bindings.
 */
public interface Variant extends Named {

    /**
     * Role bindings declared inside this variant.
     *
     * The binding name is the role name.
     */
    NamedDomainObjectContainer<RoleBinding> getRoleBindings();

    /**
     * Creates or returns an existing role binding and configures it.
     */
    default RoleBinding role(String name) {
        return getRoleBindings().maybeCreate(name);
    }

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

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