// 公式设置-多条件公式，转换数据结构，用于接口传惨
export const formulaParamsChange = (addmanyChange: any = {}) => {
  const arr = [];
  addmanyChange.innerConditionExpression.forEach((item, index) => {
    const iobj: any = {
      conditionalValues: `(${addmanyChange.outerConditionValue})`,
      conditionalNames: "条件值",
      currentFormulaType: item.select,
      currentFormulaTwo: "(条件值)"
    };
    if (item.startValue) {
      iobj.currentFormulaOne = `(${item.startValue})`;
    }
    if (item.comparisonStart) {
      iobj.operatorOne = item.comparisonStart;
    }
    if (item.comparisonEnd) {
      iobj.operatorTwo = item.comparisonEnd;
    }
    if (item.endValue) {
      iobj.currentFormulaThree = `(${item.endValue})`;
    }
    if (item.innerScore) {
      iobj.score = item.innerScore;
    }
    if (item.scoreRuleMoreInnerVO && item.scoreRuleMoreInnerVO.length) {
      iobj.nextFormatParams = [];
      item.scoreRuleMoreInnerVO.forEach(el => {
        if (el.scoreRules && el.scoreRules.length) {
          el.scoreRules.forEach(el2 => {
            const eobj: any = {
              conditionalValues: `(${el.innerConditionValue})`,
              conditionalNames: `条件值${index + 1}`,
              currentFormulaType: el2.select,
              currentFormulaTwo: `(条件值${index + 1})`
            };
            if (el2.startValue) {
              eobj.currentFormulaOne = `(${el2.startValue})`;
            }
            if (el2.comparisonStart) {
              eobj.operatorOne = el2.comparisonStart;
            }
            if (el2.comparisonEnd) {
              eobj.operatorTwo = el2.comparisonEnd;
            }
            if (el2.endValue) {
              eobj.currentFormulaThree = `(${el2.endValue})`;
            }
            if (el2.score) {
              eobj.score = el2.score;
            }
            iobj.nextFormatParams.push(eobj);
          });
        }
      });
    }
    arr.push(iobj);
  });
  return arr;
};

// 公式设置-多条件公式，转换数据结构，从接口返回数据后回显
const removeParentheses = (str = "") => {
  let newStr = str || "";
  if (str) {
    if (str.indexOf("(") > -1) {
      newStr = newStr.replace("(", "");
    }
    if (str.indexOf(")") > -1) {
      newStr = newStr.replace(")", "");
    }
  }
  return newStr;
};
export const echoParamsChange = (echoChange: any = []) => {
  const obj = {
    outerConditionValue: "",
    outerConditionValueExpress: "",
    innerConditionExpression: []
  };
  if (echoChange.length) {
    obj.outerConditionValue = removeParentheses(
      echoChange[0].conditionalValues
    );
    echoChange.forEach(item => {
      const iObj: any = {
        startValue: removeParentheses(item.currentFormulaOne),
        endValue: removeParentheses(item.currentFormulaThree),
        select: item.currentFormulaType,
        comparisonStart: item.operatorOne,
        comparisonEnd: item.operatorTwo,
        innerScore: item.score || ""
      };
      const voObj = {
        innerConditionValue: "",
        innerConditionValueExpress: null,
        scoreRules: []
      };
      if (item.nextFormatParams && item.nextFormatParams.length) {
        voObj.innerConditionValue = removeParentheses(
          item.nextFormatParams[0].conditionalValues
        );
        let scoreRules = [];
        item.nextFormatParams.forEach(el => {
          scoreRules.push({
            score: el.score,
            scoreExpress: null,
            select: el.currentFormulaType,
            startValue: removeParentheses(el.currentFormulaOne),
            endValue: removeParentheses(el.currentFormulaThree),
            comparisonStart: el.operatorOne,
            comparisonEnd: el.operatorTwo,
            startExpress: null,
            endExpress: null
          });
        });
        voObj.scoreRules = scoreRules;
        iObj.scoreRuleMoreInnerVO = [voObj];
      } else {
        iObj.scoreRuleMoreInnerVO = [];
      }
      obj.innerConditionExpression.push(iObj);
    });
  }
  return obj;
};
