"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.LogicRenderService = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _conditionRenderService = require("../conditionRenderService");
var _type = require("../conditionRenderService/type");
var _utils = require("../utils");
var _type2 = require("./type");
var LogicRenderService = exports.LogicRenderService = /*#__PURE__*/function () {
  function LogicRenderService() {
    this.conditionRenderService = void 0;
    this.logicData = void 0;
    this.conditionRenderService = new _conditionRenderService.ConditionRenderService();
    this.logicData = [];
  }

  /**
   * 初始化数据内容
   * @param data 
   */
  var _proto = LogicRenderService.prototype;
  _proto.load = function load(data) {
    if (data === void 0) {
      data = [];
    }
    this.logicData = data;
  }

  /**
   * 给条件表达式重新处理一下序号，用于后期或者调试知道执行到第几步
   * @param data 
   */;
  _proto.setSequence = function setSequence(data) {
    return this.handleSequence(data);
  }

  /**
   * 创建基本条件
   * @returns 
   */;
  _proto.createData = function createData() {
    return {
      id: (0, _utils.uuid)(),
      type: _type2.SelectLogicType.IF,
      conditionConfig: {
        data: this.createConditionData(),
        resultData: this.createResultData()
      },
      returnType: _type2.ReturnType.RETURN_CONFIG,
      returnConfig: {
        data: this.createConditionData(),
        resultData: this.createResultData()
      },
      children: []
    };
  };
  _proto.createConditionData = function createConditionData(type) {
    return [this.conditionRenderService.createInitItemOneByType(type !== undefined ? type : _type.ConditionType.all)];
  };
  _proto.createResultData = function createResultData() {
    return this.conditionRenderService.createInitResultData();
  }

  /**
   * 增加
   * @param data 
   * @returns 
   */;
  _proto.addRowData = function addRowData(data) {
    this.logicData = this.handleAddRowData(this.logicData, data);
    return this.logicData;
  }

  /**
  * 删除
  * @param data 
  * @returns 
  */;
  _proto.removeRowData = function removeRowData(data) {
    this.logicData = this.handleRemoveRowData(this.logicData, data);
    return this.logicData;
  }

  /**
   * 找到指定children补充内容
   * @param data 
   * @returns 
   */;
  _proto.addChildrenLogic = function addChildrenLogic(data) {
    this.logicData = this.handleAddChildrenLogic(this.logicData, data);
    return this.logicData;
  }

  /**
    * 修改行属性
    * @param data 
    */;
  _proto.alterRowData = function alterRowData(data) {
    this.logicData = this.handleAlterRowData(this.logicData, data);
    return this.logicData;
  }

  /**
  * 判断是否是最后一个
  * @returns 
  */;
  _proto.isLastData = function isLastData(data) {
    var result = false;
    if (this.logicData.length && this.logicData.length === 1 && this.logicData[0].id === data.id) {
      result = true;
    }
    return result;
  };
  _proto.handleAnalyzeLogic = /*#__PURE__*/function () {
    var _handleAnalyzeLogic = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(data) {
      var result, i, item, conditionResult;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            result = {
              data: null,
              isBreak: false
            };
            i = 0;
          case 2:
            if (!(i < data.length)) {
              _context.next = 22;
              break;
            }
            item = data[i];
            if (!(item.type !== _type2.SelectLogicType.ElSE)) {
              _context.next = 14;
              break;
            }
            conditionResult = this.getConditionResult(item.conditionConfig);
            if (!conditionResult) {
              _context.next = 12;
              break;
            }
            _context.next = 9;
            return this.handleReturnAnalyze(item);
          case 9:
            result = _context.sent;
            if (!result.isBreak) {
              _context.next = 12;
              break;
            }
            return _context.abrupt("break", 22);
          case 12:
            _context.next = 19;
            break;
          case 14:
            _context.next = 16;
            return this.handleReturnAnalyze(item);
          case 16:
            result = _context.sent;
            if (!result.isBreak) {
              _context.next = 19;
              break;
            }
            return _context.abrupt("break", 22);
          case 19:
            i++;
            _context.next = 2;
            break;
          case 22:
            return _context.abrupt("return", result);
          case 23:
          case "end":
            return _context.stop();
        }
      }, _callee, this);
    }));
    function handleAnalyzeLogic(_x) {
      return _handleAnalyzeLogic.apply(this, arguments);
    }
    return handleAnalyzeLogic;
  }()
  /**
   * 处理返回逻辑，可能内嵌字逻辑也可能是返回结果
   * @param item 
   * @returns 
   */
  ;
  _proto.handleReturnAnalyze =
  /*#__PURE__*/
  function () {
    var _handleReturnAnalyze = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2(item) {
      var result;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            result = {
              data: null,
              isBreak: false
            };
            if (!(item.returnType === _type2.ReturnType.RETURN_CONFIG)) {
              _context2.next = 6;
              break;
            }
            result.data = this.getConditionResult(item.returnConfig);
            result.isBreak = true;
            _context2.next = 14;
            break;
          case 6:
            if (!(item.returnType === _type2.ReturnType.RETURN_LOGIC)) {
              _context2.next = 13;
              break;
            }
            if (!item.children.length) {
              _context2.next = 11;
              break;
            }
            _context2.next = 10;
            return this.handleAnalyzeLogic(item.children);
          case 10:
            result = _context2.sent;
          case 11:
            _context2.next = 14;
            break;
          case 13:
            console.error("--Invalid return type :" + item.returnType);
          case 14:
            return _context2.abrupt("return", result);
          case 15:
          case "end":
            return _context2.stop();
        }
      }, _callee2, this);
    }));
    function handleReturnAnalyze(_x2) {
      return _handleReturnAnalyze.apply(this, arguments);
    }
    return handleReturnAnalyze;
  }();
  _proto.handleAddRowData = function handleAddRowData(oldData, data) {
    var _this = this;
    var index = this.getIndexById(oldData, data.id);
    var newData = this.createData();
    if (index !== -1) {
      var newIndex = index + 1;
      newIndex === oldData.length ? oldData.push(newData) : oldData.splice(newIndex, 0, newData);
    } else {
      oldData = oldData.map(function (item) {
        if (item.children.length) {
          item.children = _this.handleAddRowData(item.children, data);
        }
        return item;
      });
    }
    return oldData;
  };
  _proto.handleRemoveRowData = function handleRemoveRowData(oldData, data) {
    var _this2 = this;
    var index = this.getIndexById(oldData, data.id);
    if (index !== -1) {
      oldData.splice(index, 1);
    } else {
      oldData = oldData.map(function (item) {
        if (item.children.length) {
          item.children = _this2.handleRemoveRowData(item.children, data);
        }
        return item;
      });
    }
    return oldData;
  };
  _proto.handleAddChildrenLogic = function handleAddChildrenLogic(oldData, data) {
    var _this3 = this;
    var index = this.getIndexById(oldData, data.id);
    if (index !== -1) {
      oldData[index].children.push(this.createData());
    } else {
      oldData = oldData.map(function (item) {
        if (item.children.length) {
          item.children = _this3.handleAddChildrenLogic(item.children, data);
        }
        return item;
      });
    }
    return oldData;
  };
  _proto.handleAlterRowData = function handleAlterRowData(oldData, data) {
    var _this4 = this;
    var index = this.getIndexById(oldData, data.id);
    if (index !== -1) {
      oldData[index] = data;
    } else {
      oldData = oldData.map(function (item) {
        var _item$children;
        if ((_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length) {
          item.children = _this4.handleAlterRowData(item.children, data);
        }
        return item;
      });
    }
    return oldData;
  };
  _proto.getIndexById = function getIndexById(data, id) {
    var result = -1;
    for (var i = 0; i < data.length; i++) {
      if (data[i].id === id) {
        result = i;
        break;
      }
    }
    return result;
  }

  /**
   * 调用 ConditionRenderService 去做处理
   * @param config 
   * @returns 
   */;
  _proto.getConditionResult = function getConditionResult(config) {
    this.conditionRenderService.load(config.data);
    return this.conditionRenderService.getComputedResult();
  };
  _proto.handleSequence = function handleSequence(data, parentIndex) {
    var _this5 = this;
    return data.map(function (item, index) {
      item.seq = _this5.getSeqString(index, parentIndex);
      if (item.children && item.children.length > 0) {
        item.children = _this5.handleSequence(item.children, item.seq);
      }
      return item;
    });
  };
  _proto.getSeqString = function getSeqString(index, parentIndex) {
    return parentIndex ? parentIndex + "-" + (index + 1) : "" + (index + 1);
  };
  return LogicRenderService;
}();