/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package API;

import compiler.IvannosysCompiler;
import java.util.Arrays;
import java.util.HashMap;

/**
 *
 * @author Eugenio
 */
public class Imports {

    protected static HashMap<String, String> imports = new HashMap<>();
    protected static HashMap<String, Boolean> loaded = new HashMap<>();
    protected static HashMap<String, Boolean> used = new HashMap<>();
    protected static IvannosysCompiler compiler;

    public static void _init(IvannosysCompiler compiler) {
        Imports.compiler = compiler;
    }

    public static void add(String path) {
        add(path, "");
    }

    public static boolean Load(String alias) throws Exception {
        if (!imports.containsKey(alias)) {
            return false;
        }

        if (!loaded.containsKey(alias)) {
            compiler.LoadPackage(alias, imports.get(alias));
            loaded.put(alias, true);
        }
        return true;
    }

    public static void add(String path, String alias) {
        if (alias.equals("")) {
            String[] parts = path.split("/");
            alias = parts[parts.length - 1];
        }
//        System.out.println("Add, " + path + ":" + alias);

        if (!imports.containsKey(alias)) {
            imports.put(alias, path);
        }
    }

    public static HashMap<String, String> imports() {
        return imports;
    }

}
