package net.northking.cctp.element.mobile.ios.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
    private final static String GLOBAL_VAR_REGEX = "#\\{([^}]*)\\}";
    private final static String SCRIPT_VAR_REGEX = "\\$\\{([^}]*)\\}";
    private final static Pattern GLOBAL_VAR_PATTERN  = Pattern.compile(GLOBAL_VAR_REGEX);
    private final static Pattern SCRIPT_VAR_PATTERN = Pattern.compile(SCRIPT_VAR_REGEX);

    private final static Logger log = LoggerFactory.getLogger(RegexUtils.class);

    private RegexUtils(){}

    public static boolean containGlobalVar(String str){
        return GLOBAL_VAR_PATTERN.matcher(str).find();
    }
    public static Set<String> extractGlobalVar(String str){
        Set<String> ret = new HashSet<>();
        Matcher matcher = GLOBAL_VAR_PATTERN.matcher(str);
        while(matcher.find()){
            String group = matcher.group();
            String var = group.substring(2,group.length()-1);
            ret.add(var);
            log.debug("find global var expr,expr:{},var:{}",group,var);
        }
        return  ret;
    }
    public static boolean containScriptVar(String str){
        return SCRIPT_VAR_PATTERN.matcher(str).find();
    }
    public static Set<String> extractScriptVar(String str){
        Set<String> ret = new HashSet<>();
        Matcher matcher = SCRIPT_VAR_PATTERN.matcher(str);
        while(matcher.find()){
            String group = matcher.group();
            String var = group.substring(2,group.length()-1);
            ret.add(var);
            log.debug("find script var expr,expr:{},var:{}",group,var);
        }
        return  ret;
    }

}
