import { uuid } from "../../../_utils/utils";
import { DataSourceType } from "../../../_utils/formKeyType";
import { LogicRenderService } from "../../../_utils/logicRenderService";
import { ConditionType } from "../../../_utils/conditionRenderService/type";
import { cloneDeep } from "lodash";
export var AlterStateService = /*#__PURE__*/function () {
  function AlterStateService() {
    this.logicRenderService = void 0;
    this.logicRenderService = new LogicRenderService();
  }
  var _proto = AlterStateService.prototype;
  _proto.addRowData = function addRowData(data) {
    data.push(this.createRowData());
    return [].concat(data);
  };
  _proto.copyRowData = function copyRowData(data, index) {
    var copyData = cloneDeep(data[index]);
    copyData.id = uuid();
    data.splice(index + 1, 0, copyData);
    return [].concat(data);
  };
  _proto.removeRowData = function removeRowData(data, id) {
    return data.filter(function (item) {
      return item.id !== id;
    });
  };
  _proto.resetRowData = function resetRowData(data, id) {
    var newData = this.createRowData();
    var newResult = data.map(function (item) {
      if (item.id === id) {
        item = newData;
      }
      return item;
    });
    return newResult;
  };
  _proto.alterRowData = function alterRowData(config) {
    var oldData = config.oldData,
      id = config.id,
      logic = config.logic,
      value = config.value;
    return oldData.map(function (item) {
      if (item.id === id) {
        if (logic) {
          item.alterLogic = logic;
        }
        if (value) {
          item.alterValue = value;
        }
      }
      return item;
    });
  };
  _proto.getCopyAlterStateList = function getCopyAlterStateList(list) {
    var _this = this;
    var result = cloneDeep(list);
    return result.map(function (item) {
      item.id = uuid();
      item.alterLogic = _this.handleCopyAlterLogic(item.alterLogic);
      return item;
    });
  };
  _proto.handleCopyAlterLogic = function handleCopyAlterLogic(logic) {
    var result = logic;
    result.data = this.logicRenderService.conditionRenderService.getCopyConditionData(result.data);
    return result;
  };
  _proto.createRowData = function createRowData() {
    return {
      id: uuid(),
      alterValue: {
        formKey: DataSourceType.currentPageType,
        data: '',
        treeData: []
      },
      alterLogic: {
        data: this.logicRenderService.createConditionData(ConditionType.computed),
        resultData: this.logicRenderService.createResultData()
      }
    };
  };
  return AlterStateService;
}();