/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.template.soy.soytree;

import static java.util.stream.Collectors.joining;

import com.google.template.soy.base.SourceLocation;
import com.google.template.soy.basetree.CopyState;
import com.google.template.soy.exprtree.StringNode;
import com.google.template.soy.exprtree.VarDefn;
import com.google.template.soy.soytree.SoyNode.Kind;
import java.util.List;

/**
 * Node representing a 'import' statement with a value expression.
 *
 * <p>Important: Do not use outside of Soy code (treat as superpackage-private).
 *
 */
public final class ImportNode extends AbstractSoyNode {

  /** The value expression that the variable is set to. */
  private final List<VarDefn> identifiers;

  private final StringNode path;

  /** Only CSS is supported right now. */
  public enum ImportType {
    CSS
  }

  public ImportNode(int id, SourceLocation location, StringNode path, List<VarDefn> defns) {
    super(id, location);
    this.identifiers = defns;
    this.path = path;
  }

  /**
   * Copy constructor.
   *
   * @param orig The node to copy.
   */
  private ImportNode(ImportNode orig, CopyState copyState) {
    super(orig, copyState);
    this.identifiers = orig.identifiers;
    this.path = orig.path.copy(copyState);
  }

  @Override
  public Kind getKind() {
    return Kind.IMPORT_NODE;
  }

  @Override
  public ImportNode copy(CopyState copyState) {
    return new ImportNode(this, copyState);
  }

  public boolean isSideEffectImport() {
    return identifiers.isEmpty();
  }

  private ImportType getImportType() {
    // TODO(tomnguyen): Throw an error if any aliases are extracted from CSS imports, as they do not
    // exist yet.
    if (path.getValue().endsWith(".gss") || path.getValue().endsWith(".sass")) {
      return ImportType.CSS;
    }
    // TODO(tomnguyen) Write a validation pass to verify imports.
    throw new UnsupportedOperationException("No other imports are supported right now.");
  }

  public String getPath() {
    return path.getValue();
  }

  public boolean isCss() {
    return getImportType() == ImportType.CSS;
  }

  @Override
  public String toSourceString() {
    String exprs = "";
    if (!identifiers.isEmpty()) {
      exprs =
          String.format(
              "{%s} from ", identifiers.stream().map(VarDefn::name).collect(joining(",")));
    }
    return String.format("import %s'%s'", exprs, path.getValue());
  }
}

@@ -1396,16 +1396,30 @@
 %0A    CSS
+,%0A    UNKNOWN,
 %0A  %7D%0A%0A  
@@ -2334,17 +2334,17 @@
 With(%22.s
-a
+c
 ss%22)) %7B%0A
@@ -2386,158 +2386,33 @@
     
-// TODO(tomnguyen) Write a validation pass to verify imports.%0A    throw new UnsupportedOperationException(%22No other imports are supported right now.%22)
+return ImportType.UNKNOWN
 ;%0A  
@@ -2549,24 +2549,95 @@
 e.CSS;%0A  %7D%0A%0A
+  public List%3CVarDefn%3E getIdentifiers() %7B%0A    return identifiers;%0A  %7D%0A%0A
   @Override%0A

Format import nodes