
function isFormulaItem(ele){
  return ele.type == 'Element' || ele.type == 'ParamData'
}

//转换公式的参数
function transformArguments(children,curEle,eleMap){
  let fcReg = /(FC\.\S+\()(.+)\)/;
  let fcText = '';
  for (let i = (children.length-1); i >= 0; i--) {
    let ele = children[i];

    if(ele.isOn === false){
      //点击了开关关闭的，跳过
      continue;
    }
    //console.log(ele.name)
    let tmpArr = fcReg.exec(ele.template.ft);
    fcText = tmpArr[1] + fcText;//fc.XX( 左括号部分
    let argList = tmpArr[2].split(",");//括号里面参数部分#1,#2..
    let isNestEle = false;//是否有过嵌套的元素了
    let isNestEle2 = false;
    //console.log(argList)
    //console.log(text)
    argList.forEach((argText,index)=>{
      if(argText.indexOf('#')>-1){
        //#动态参数
        let argIndex = index;
        for (let j = 0; j < ele.template.args.length; j++) {
          if(ele.template.args[j].key == argText){
            //找到相同的#编码
            argIndex = j;
            break;
          }
        }
        let arg = ele.arguments[argIndex];
        
        if(arg && isFormulaItem(arg)){
          if(!isNestEle && !isNestEle2 && i != (children.length-1) && curEle.id == arg.id){
            //不是第一个嵌套的公式，且和当前挂载的是同一个元素
            //认为这个参数是之前公式计算的结果，不再写入元素
            isNestEle = true;
          }else{
            if(arg.type == 'Element'){
              eleMap[arg.tableElementKey] = {
                id: arg.id,
                name: arg.name,
                tableElementKey: arg.tableElementKey,
                type: "Element",
              };
              fcText += `E[${arg.tableElementKey}]`;
            }else if(arg.type == 'ParamData'){
              eleMap[arg.k] = {
                id: arg.id,
                name: arg.name,
                v:arg.v,
                k:arg.k,
                type: "ParamData",
              };
              fcText += `WP[${arg.k}]`;
            }
            
          }
        }else if(Array.isArray(arg)){
          //ifsles方法会进来
          arg.forEach((a)=>{
            if(a && isFormulaItem(a)){
              if(!isNestEle2 && i != (children.length-1) && curEle.id == a.id){
                //不是第一个嵌套的公式，且和当前挂载的是同一个元素
                //认为这个参数是之前公式计算的结果，不再写入元素
                isNestEle2 = true
              }else{
                if(a.type == 'Element'){
                  eleMap[a.tableElementKey] = {
                    id: a.id,
                    name: a.name,
                    tableElementKey: a.tableElementKey,
                    type: "Element",
                  };
                  fcText += `E[${a.tableElementKey}]`;
                }else if(a.type == 'ParamData'){
                  eleMap[a.k] = {
                    id: a.id,
                    name: a.name,
                    v:a.v,
                    k:a.k,
                    type: "ParamData",
                  };
                  fcText += `WP[${a.k}]`;
                }
              }
            }else if(a && a.type){
              fcText += a.name;
            }else{
              fcText += a?a:"''";
            }
          })

        }else{
          fcText += arg?arg:"''";
        }

      }else{
        //已有参数
        fcText += argText?argText:'';
      }
      if(index != argList.length-1){
        fcText += ',';
      }
    })
    fcText += ')';
  }

  return fcText;
}

export const formulaArrayToString = (processFormula,resultFormula) => {
  let text = '';
  let eleMap = {};//元素字典，为了回显的时候查询信息

  processFormula.forEach(item => {
    if(isFormulaItem(item)){
      //console.log(item)
      if(item.children.length <1){
        if(item.type == 'Element'){
          eleMap[item.tableElementKey] = {
            id: item.id,
            name: item.name,
            tableElementKey: item.tableElementKey,
            type: "Element",
          };
          text += `E[${item.tableElementKey}]`;
        }else if(item.type == 'ParamData'){
          eleMap[item.k] = {
            id: item.id,
            name: item.name,
            v:item.v,
            k:item.k,
            type: "ParamData",
          };
          text += `WP[${item.k}]`;
        }
        
      }else{
        text += transformArguments(item.children,item,eleMap);
      }
    }else if(item.type == 'Element'){
      text += item.template.ft;
    }else{
      text += item.name;
    }
  });

  if(resultFormula[0].children.length){
    //等号左侧部分
    let resText = transformArguments(resultFormula[0].children,resultFormula[0],eleMap);
  
    //等号左侧元素不需要，左侧的公式嵌套右侧所有结果
    text = resText.replace(`E[${resultFormula[0].tableElementKey}]`,text);
  }

  console.log(text);
  //console.log(eleMap)
  return {text,eleMap};
}
