/**
 * generated by Xtext 2.12.0
 */
package com.minres.rdl.scoping;

import com.google.common.collect.Iterables;
import com.minres.rdl.rdl.ComponentDefinition;
import com.minres.rdl.rdl.Include;
import com.minres.rdl.rdl.Root;
import com.minres.rdl.scoping.AbstractRDLScopeProvider;
import java.util.Arrays;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

/**
 * This class contains custom scoping description.
 * 
 * see : http://www.eclipse.org/Xtext/documentation.html#scoping
 * on how and when to use it
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping
 * on how and when to use it.
 */
@SuppressWarnings("all")
public class RDLScopeProvider extends AbstractRDLScopeProvider {
  protected IScope _getScopeWithComponentDefinition(final ComponentDefinition componentDef) {
    return Scopes.scopeFor(componentDef.getComponentDefinitions(), this.getScopeWithComponentDefinition(componentDef.eContainer()));
  }
  
  protected IScope _getScopeWithComponentDefinition(final Root root) {
    EList<ComponentDefinition> compDefs = root.getComponentDefinitions();
    EList<Include> _includes = root.getIncludes();
    for (final Include incl : _includes) {
      {
        Resource resource = EcoreUtil2.getResource(root.eResource(), incl.getImportURI());
        EObject _head = IterableExtensions.<EObject>head(resource.getContents());
        final Root r = ((Root) _head);
        EList<ComponentDefinition> _componentDefinitions = r.getComponentDefinitions();
        Iterables.<ComponentDefinition>addAll(compDefs, _componentDefinitions);
      }
    }
    return Scopes.scopeFor(compDefs);
  }
  
  public IScope getScopeWithComponentDefinition(final EObject componentDef) {
    if (componentDef instanceof ComponentDefinition) {
      return _getScopeWithComponentDefinition((ComponentDefinition)componentDef);
    } else if (componentDef instanceof Root) {
      return _getScopeWithComponentDefinition((Root)componentDef);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(componentDef).toString());
    }
  }
}
