package com.reason.bs.hints;

import com.reason.lang.core.HMSignature;

import java.util.HashMap;
import java.util.Map;

public class InferredTypesImplementation implements BsQueryTypesService.InferredTypes {
    private final Map<String, HMSignature> m_let = new HashMap<>();
    private final Map<String, InferredTypesImplementation> m_modules = new HashMap<>();

    public void add(String type) {
        try {
            if (type.startsWith("val")) {
                int colonPos = type.indexOf(':');
                if (colonPos < type.length()) {
                    m_let.put(type.substring(4, colonPos - 1), new HMSignature(true, type.substring(colonPos + 1)));
                } else {
                    System.out.println("TYPE ERR: " + type + " " + colonPos);
                }
            } else if (type.startsWith("module")) {
                int colonPos = type.indexOf(':');
                if (0 <= colonPos) {
                    int sigPos = type.indexOf("sig");
                    InferredTypesImplementation moduleTypes = new InferredTypesImplementation();
                    m_modules.put(type.substring(7, colonPos - 1), moduleTypes);
                    String sigTypes = type.substring(sigPos + 3, type.length() - 3);
                    String[] moduleSigTypes = sigTypes.trim().split("(?=module|val|type)");
                    for (String moduleSigType : moduleSigTypes) {
                        moduleTypes.add(moduleSigType);
                    }
                }
            }
        } catch (Error e) {
            System.out.println(e);
            System.out.println("type: " + type);
        }
    }

    public HMSignature getLetType(String name) {
        return m_let.get(name);
    }

    public BsQueryTypesService.InferredTypes getModuleType(String name) {
        return m_modules.get(name);
    }
}

@@ -63,16 +63,48 @@
 gnature;
+%0Aimport org.apache.log4j.Logger;
 %0A%0Aimport
@@ -552,16 +552,32 @@
     if (
+0 %3C colonPos && 
 colonPos
@@ -758,34 +758,56 @@
         
-System.out.println
+Logger.getLogger(%22ReasonML.types%22).error
 (%22TYPE E
@@ -810,16 +810,17 @@
 PE ERR: 
+%5B
 %22 + type
@@ -823,16 +823,17 @@
 type + %22
+%5D
  %22 + col

indexof can be negative