import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _cloneDeep from "lodash/cloneDeep";

/* eslint-disable */
import { tagNameMaps, transformCharMaps, firstToLower } from "./util";
import { defaultCols, settingKeys } from "../../config/pluginConfig";
import { judgeIfBlockElement, getBlockFromChildId } from "../blockUtil";
import formatXml from "./formatXml";
var settings = {};
var excludeKeys = ['columns', 'columnsRender', 'formInstance', 'canEdit', 'staticOptions', 'remote', 'horizontalAlign', 'verticalAlign', 'horizontalAlignFlex', 'animation', 'background', 'border', 'margin', 'padding', 'shadow'];
var defaultSettings = {
  formatOutput: true,
  formatTextNodes: false,
  indentString: '  ',
  ignore: [],
  replace: [],
  nodes: [],
  ///TODO: exceptions system
  exceptions: [],
  tagNameKey: 'type',
  tagNameKeyForBlock: 'name',
  childrenKey: 'elements',
  styleKey: 'data'
};
var formItemType = 'Form.Item';

var splitFormComponent = function splitFormComponent(obj) {
  var childData = {};
  var parentData = {}; // let buildMode;

  Object.keys(obj.data).forEach(function (key) {
    if (key === 'formSettings') {
      parentData[key] = obj.data[key];
    } else if (key === 'settings') {
      childData[key] = _cloneDeep(obj.data[key]);
    } else {
      childData[key] = obj.data[key];
    }
  });
  obj.data = childData;
  var formItem = {
    type: formItemType,
    elements: [obj],
    data: _objectSpread(_objectSpread({}, parentData), {}, {
      settings: _objectSpread({}, parentData.settings || {})
    })
  };
  return formItem;
};

var splitPickerComponent = function splitPickerComponent(obj) {
  var _obj$data, _obj$data$formItemSet;

  var childData = {
    type: 'listItem',
    elements: [],
    data: {
      componentSettings: {
        text: (_obj$data = obj.data) === null || _obj$data === void 0 ? void 0 : (_obj$data$formItemSet = _obj$data.formItemSettings) === null || _obj$data$formItemSet === void 0 ? void 0 : _obj$data$formItemSet.placeholder,
        arrow: 'horizontal'
      }
    }
  };

  var parent = _cloneDeep(obj);

  delete parent.data.formItemSettings.placeholder;
  parent.elements = [childData];
  return parent;
};

var splitRadioComponent = function splitRadioComponent(obj) {
  var _obj$data2;

  var parent = _cloneDeep(obj);

  var _ref = ((_obj$data2 = obj.data) === null || _obj$data2 === void 0 ? void 0 : _obj$data2.formItemSettings) || {},
      _ref$options = _ref.options,
      options = _ref$options === void 0 ? [] : _ref$options;

  if (options.length > 0) {
    options.forEach(function (v) {
      parent.elements.push({
        type: 'radio',
        elements: [],
        data: {
          settings: {},
          formItemSettings: {
            value: v.value,
            text: v.label
          }
        }
      });
    });
  }

  delete parent.data.formItemSettings.options;
  return parent;
};

var transformTagName = function transformTagName(tagName) {
  var name = tagNameMaps[tagName] ? tagNameMaps[tagName] : tagName;
  return name;
};

var judgeIfBlock = function judgeIfBlock(json) {
  var _json$data, _json$data$blockSetti;

  return json.type === 'document' && ((_json$data = json.data) === null || _json$data === void 0 ? void 0 : (_json$data$blockSetti = _json$data.blockSettings) === null || _json$data$blockSetti === void 0 ? void 0 : _json$data$blockSetti.isBlock) && !settings.ignoreBlock;
};

var getTagName = function getTagName(json) {
  var tagName;

  if (judgeIfBlock(json)) {
    // block区块取name属性
    tagName = json[settings.tagNameKeyForBlock];
  } else if (json.type === 'rowCell') {
    var _json$data2, _json$data2$component;

    // rowCell单元格取单元格内元素类型
    if ((_json$data2 = json.data) !== null && _json$data2 !== void 0 && (_json$data2$component = _json$data2.componentSettings) !== null && _json$data2$component !== void 0 && _json$data2$component.to && (json.data.componentSettings.to.includes('mailto:') || json.data.componentSettings.to.includes('http'))) {
      tagName = 'a';
    } else {
      var _json$data3, _json$data3$component;

      tagName = firstToUpper(((_json$data3 = json.data) === null || _json$data3 === void 0 ? void 0 : (_json$data3$component = _json$data3.componentSettings) === null || _json$data3$component === void 0 ? void 0 : _json$data3$component.renderType) || 'text');
    }
  } else {
    // 其他取type属性
    tagName = firstToUpper(json[settings.tagNameKey]);
  }

  return tagName ? transformTagName(tagName) : tagName;
};

function convertToXml(json, depth) {
  var _json$data$componentS, _json$data$componentS2, _json$data$componentS3, _json$data$componentS4;

  var suffix = settings.formatOutput ? '\r\n' : ''; // 区块名取name属性

  var tagNameKey = judgeIfBlock(json) ? settings.tagNameKeyForBlock : settings.tagNameKey;
  var tagName = getTagName(json);
  var xmlTag = '<' + tagName;
  var children = '';

  if (json.type === 'text') {
    var _json$data4;

    children = (_json$data4 = json.data) === null || _json$data4 === void 0 ? void 0 : _json$data4.text;
    xmlTag = '';
  }

  if (json.type === 'expression') {
    var _json$data5, _json$data5$component;

    children = "{{".concat((_json$data5 = json.data) === null || _json$data5 === void 0 ? void 0 : (_json$data5$component = _json$data5.componentSettings) === null || _json$data5$component === void 0 ? void 0 : _json$data5$component.expression, "}}");
    xmlTag = '';
  }

  if (json.type === 'rowCell' && tagName === 'a' && ((_json$data$componentS = json.data.componentSettings) === null || _json$data$componentS === void 0 ? void 0 : _json$data$componentS.renderType) === 'link' && !((_json$data$componentS2 = json.data.componentSettings) !== null && _json$data$componentS2 !== void 0 && _json$data$componentS2.renderText) && (_json$data$componentS3 = json.data.componentSettings) !== null && _json$data$componentS3 !== void 0 && (_json$data$componentS4 = _json$data$componentS3.to) !== null && _json$data$componentS4 !== void 0 && _json$data$componentS4.includes('mailto:')) {
    var _json$data$componentS5, _json$data$componentS6;

    var childrenText = (_json$data$componentS5 = json.data.componentSettings) === null || _json$data$componentS5 === void 0 ? void 0 : (_json$data$componentS6 = _json$data$componentS5.to) === null || _json$data$componentS6 === void 0 ? void 0 : _json$data$componentS6.replace('mailto:', '');
    children = "{{".concat(childrenText, "}}");
  }

  if (xmlTag) {
    Object.keys(json).forEach(function (key) {
      if (key !== tagNameKey && Object.prototype.hasOwnProperty.call(json, key)) {
        if (key === settings.childrenKey && Array.isArray(json[key])) {
          // children
          json[key].forEach(function (item) {
            var _item$data, _item$data2, _item$data3, _item$data3$component;

            if (judgeIfBlock(item)) {
              // block组件特殊处理
              children += convertToXml(item, depth + 1);
            } else if (item.type === 'radioGroup') {
              children += convertToXml(splitRadioComponent(item), depth + 1);
            } else if ((_item$data = item.data) !== null && _item$data !== void 0 && _item$data.formItemSettings && (_item$data2 = item.data) !== null && _item$data2 !== void 0 && _item$data2.formSettings && ![formItemType, 'document'].includes(json.type) && item.type !== 'button') {
              // 表单组件需要额外用FormItem包裹
              var newItem = item;

              if (item.type === 'picker') {
                newItem = splitPickerComponent(item);
              }

              var formItem = splitFormComponent(newItem);
              children += convertToXml(formItem, depth + 1);
            } else if (item.type === 'rowCell' && ((_item$data3 = item.data) === null || _item$data3 === void 0 ? void 0 : (_item$data3$component = _item$data3.componentSettings) === null || _item$data3$component === void 0 ? void 0 : _item$data3$component.renderType) === 'text') {
              // rowCell不做转换处理，直接转换子节点
              item.elements.forEach(function (v) {
                children += convertToXml(v, depth + 1);
              });
            } else {
              children += convertToXml(item, depth + 1);
            }
          });
        } else if (key === settings.styleKey) {
          // style
          var styleProps = createStyleProperty(json[key], children, depth + 1, json.type);
          xmlTag += " ".concat(styleProps.style);
          children = styleProps.children;
        } else {
          // other property
          if (key === 'path') {
            var _json$data6, _json$data6$blockSett;

            // block及其内部普通元素需要记载path路径
            if (json.type === 'document' && (_json$data6 = json.data) !== null && _json$data6 !== void 0 && (_json$data6$blockSett = _json$data6.blockSettings) !== null && _json$data6$blockSett !== void 0 && _json$data6$blockSett.isBlock || json.for) {
              xmlTag += " ".concat(getPropertyName(key), "=\"").concat(json[key], "\"");
            }
          } else {
            xmlTag += " ".concat(getPropertyName(key), "=\"").concat(json[key], "\"");
          }
        }
      }
    });
  } // close tag properly


  if (children !== '') {
    if (xmlTag) {
      xmlTag += ">".concat(children, "</").concat(tagName, ">").concat(suffix);
    } else {
      xmlTag += children;
    }
  } else if (tagName === 'Link') {
    xmlTag += "></".concat(tagName, ">");
  } else {
    xmlTag += ' />' + suffix;
  }

  return xmlTag;
}

var createRowCellStyleProperty = function createRowCellStyleProperty() {
  var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var children = arguments.length > 1 ? arguments[1] : undefined;
  var rowCellProps = '';
  Object.keys(obj).forEach(function (key) {
    if (key !== 'renderType') {
      if (key === 'renderText') {
        children += "".concat(obj[key]);
      } else if (key === 'to' && obj[key] && (obj[key].includes(['mailto:']) || obj[key].includes(['http']))) {
        rowCellProps += " href=\"".concat(obj[key], "\"");
      } else if (typeof obj[key] === 'string') {
        rowCellProps += " ".concat(key, "=\"").concat(obj[key], "\"");
      }
    }
  });
  return {
    style: rowCellProps,
    children: children
  };
};

var createStyleProperty = function createStyleProperty(style) {
  var children = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var elementType = arguments.length > 3 ? arguments[3] : undefined;
  var styleStr = '';
  var classStr = '';
  var childrenStyleStr = ''; // let newChildren = children;

  Object.keys(style).forEach(function (key) {
    if (Object.prototype.hasOwnProperty.call(style, key) && !excludeKeys.includes(key)) {
      if (settingKeys.includes(key)) {
        if (elementType === 'rowCell' && key === 'componentSettings') {
          // rowCell内部属性需转换为标签
          var styleProps = createRowCellStyleProperty(style[key], children);
          childrenStyleStr += " ".concat(styleProps.style);
          children = styleProps.children;
        } else {
          var _styleProps = createStyleProperty(style[key], children, depth, elementType);

          childrenStyleStr += " ".concat(_styleProps.style);
          children = _styleProps.children;
        }
      } else if (key === 'display') {
        classStr += " ".concat(createDisplayClassProperty(style[key]));
      } else if (key === 'className') {
        classStr += " ".concat(style[key]);
      } else if (['visible', 'disabled'].includes(key)) {
        var propertyValue = getPropertyValue(key, style);
        styleStr += " v-bind:".concat(key, "=\"").concat(propertyValue, "\"");
      } else if (key === 'cols') {
        styleStr += " ".concat(getColProps(style[key]));
      } else if (key === 'height') {
        styleStr += " ".concat(geHeightProps(style[key]));
      } else if (key === 'icon') {
        var iconProps = getIconProps(style[key], children, depth);
        styleStr += " ".concat(iconProps.style);
        children = iconProps.children;
      } else if (key === 'width') {
        styleStr += " ".concat(getWidthProps(style[key]));
      } else if (key === 'image') {
        styleStr += " ".concat(getImageProps(style[key]));
      } else if (key === 'text') {
        var _getTextProps = getTextProps(style[key]),
            text = _getTextProps.text,
            textProps = _getTextProps.textProps;

        children = text;
        styleStr += textProps;
      } else if (key === 'events') {
        styleStr += " ".concat(getEventsRender(style[key]));
      } else if (['dataSource', 'dataSourceVariable'].includes(key)) {
        var getVar = style[key === 'dataSource' ? "".concat(key, "Variable") : key]; // styleStr += ` ${getPropertyName(key)}="{{${getVar || '[]'}}}"`;

        styleStr += " v-bind:dataSource=\"".concat(getVar, "\"");
      } else if (key === 'v-if') {
        styleStr += " ".concat(key, "=\"").concat(typeof style[key] === 'boolean' ? style[key] : transformDoubleToSingle(transformSpecialChar(style[key])), "\"");
      } else if (key === 'v-else') {
        styleStr = "".concat(key).concat(styleStr);
      } else if (typeof style[key] === 'string') {
        if (style[key].length > 0 && !key.includes('Variable')) {
          var _propertyValue = getPropertyValue(key, style);

          styleStr += " ".concat(getPropertyName(key), "=\"").concat(_propertyValue, "\"");
        }
      } else if (style[key] && typeof style[key] === 'object') {
        if (Object.keys(style[key]).length > 0) {
          var str = transformSpecialChar(JSON.stringify(style[key])); // if (!Array.isArray(style[key])) {
          //   // 对象类型转字符串后，需在最后一项末尾加 ","
          //   str = str.replace(/(.*)}/,'$1,}');
          // }

          styleStr += " ".concat(getPropertyName(key), "=\"{{").concat(transformDoubleToSingle(str), "}}\"");
        }
      } else if (key && style[key] !== null && style[key] !== undefined) {
        styleStr += " ".concat(getPropertyName(key), "=\"{{").concat(style[key], "}}\"");
      }
    }
  });

  if (childrenStyleStr !== '') {
    styleStr += " ".concat(childrenStyleStr);
  }

  if (classStr && deleteSpace(classStr).length > 0) {
    styleStr = "class=\"".concat(trim(classStr), "\" ").concat(trim(styleStr));
  }

  return {
    style: trim(styleStr),
    children: children
  };
};

var createDisplayClassProperty = function createDisplayClassProperty(value) {
  if (typeof value === 'string') {
    return value;
  }

  return '';
};

var getColProps = function getColProps(obj) {
  var colProps = '';

  if (obj.span) {
    colProps += "span=\"{{".concat(obj.span, "}}\"");
  }

  if (obj.offset) {
    colProps += " offset=\"{{".concat(obj.offset, "}}\"");
  }

  return trim(colProps);
};

var getImageProps = function getImageProps(obj) {
  if (obj.file) {
    return "src=\"".concat(obj.file.path, "\"");
  }

  return '';
};

var getTextProps = function getTextProps(obj) {
  var text = '';
  var textProps = '';

  if (typeof obj === 'string') {
    // const suffix = '\r\n'
    text = obj;
    textProps += "text=\"".concat(obj, "\"");
  } else if (obj.document) {
    var _obj$document$nodes = obj.document.nodes,
        nodes = _obj$document$nodes === void 0 ? [] : _obj$document$nodes;
    nodes.forEach(function (node, index) {
      text += "".concat(node.nodes[0].leaves[0].text).concat(index < node.length - 1 ? " \n" : '');
    });
  }

  return {
    text: text,
    textProps: textProps
  };
};

var getIconProps = function getIconProps(obj) {
  var children = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var iconPropsStr = '';
  var newChildren = children;

  if (obj.id) {
    var idValue = Array.isArray(obj.id) ? JSON.stringify(obj.id) : obj.id.toString();
    iconPropsStr += "iconId=\"".concat(idValue.replace(/\"/g, "'"), "\"");
  }

  if (obj.svg) {
    newChildren += "".concat(obj.svg);
  }

  if (obj.width) {
    iconPropsStr += " width=\"".concat(obj.width, "\"");
  }

  return {
    style: trim(iconPropsStr),
    children: newChildren
  };
};

var geHeightProps = function geHeightProps(obj) {
  if (obj && obj.value) {
    return "height=\"".concat(obj.value, "\"");
  }

  return '';
};

var getWidthProps = function getWidthProps(obj) {
  if (obj && obj.value) {
    return "width=\"".concat(obj.value, "\"");
  }

  return '';
};

var getEventKey = function getEventKey(key) {
  var eventKey = key;

  if (eventKey.startsWith('on')) {
    eventKey = eventKey.substr(2);
  }

  return "v-on:".concat(firstToLower(eventKey));
};

var getEventsRender = function getEventsRender() {
  var events = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var eventsPropsStr = '';
  var pathsStr = '';

  if (events.length > 0) {
    events.forEach(function (event) {
      var parameterArr = [];
      var eventKey = getEventKey(event.event);

      if (event.function.relation && Object.keys(event.function.relation).length > 0) {
        var _event$function$relat = event.function.relation[event.function.value].split('-'),
            _event$function$relat2 = _slicedToArray(_event$function$relat, 2),
            _ = _event$function$relat2[0],
            eventType = _event$function$relat2[1];

        if (event.parameter) {
          Object.keys(event.parameter).forEach(function (key) {
            parameterArr.push(event.parameter[key] === undefined ? 'undefined' : event.parameter[key]);
          });
        }

        if (eventType === 'page') {
          var pathParameter = parameterArr.join('/');
          pathsStr = "href=\"{{".concat(event.function.value, "/").concat(pathParameter, "}}\"");
        } else {
          var actionParameter = parameterArr.join(',');

          if (actionParameter) {
            // eventsPropsStr += ` ${eventKey}="{{this.${event.function.value}(${actionParameter})}}"`;
            eventsPropsStr += " ".concat(eventKey, "=\"").concat(event.function.value, "(").concat(actionParameter, ")\"");
          } else {
            eventsPropsStr += " ".concat(eventKey, "=\"").concat(event.function.value, "\"");
          }
        }
      } else if (event.function.value && event.function.value === 'url') {
        if (event.parameter) {
          Object.keys(event.parameter).forEach(function (key) {
            if (key === 'URL') {
              pathsStr = "href=\"".concat(event.parameter[key].value, "\"");
            }
          });
        }
      } else {
        eventsPropsStr += " ".concat(eventKey, "=\"").concat(event.function, "\"");
      }
    });
  }

  if (pathsStr) {
    eventsPropsStr += " ".concat(pathsStr);
  }

  return trim(eventsPropsStr);
};

var getPropertyName = function getPropertyName(name) {
  if (name.indexOf('v-') > -1) {
    return name;
  }

  var re = /(-)(\w)/g;
  return name.replace(re, function ($0, $1, $2) {
    return $2.toUpperCase();
  });
};

var getPropertyValue = function getPropertyValue(propertyName, obj) {
  var val = obj[propertyName];

  if (typeof val === 'string') {
    val = transformSpecialChar(val);
    return val;
  }

  return '';
};

var transformSpecialChar = function transformSpecialChar(str) {
  // 特殊字符需要转义
  var mapKeys = Object.keys(transformCharMaps).filter(function (v) {
    return str.includes(v);
  });

  if (mapKeys.length > 0) {
    mapKeys.forEach(function (key) {
      str = str.replace(new RegExp(key, 'g'), transformCharMaps[key]);
    });
  }

  return str;
};

var trim = function trim(str) {
  // return str.replace(/^\s+/, "").replace(/\s+$/, "");
  return str.replace(/^\s+/, "");
};

var deleteSpace = function deleteSpace(str) {
  return str.replace(/^\s+/, "").replace(/\s+$/, "");
}; // 将字符串双引号替换为单引号


var transformDoubleToSingle = function transformDoubleToSingle(str) {
  return str.replace(/\"/g, "'");
};

var firstToUpper = function firstToUpper(str) {
  // return str.toLowerCase().replace(/( |^)[a-z]/g,(L) => L.toUpperCase());
  return str.replace(/( |^)[a-z]/g, function (L) {
    return L.toUpperCase();
  });
}; // 转换block区块元素


var transformBlockInData = function transformBlockInData(data) {
  var target = {};

  var recurseFun = function recurseFun(element, json) {
    var targetJson;

    var _element$elements = element.elements,
        elements = _element$elements === void 0 ? [] : _element$elements,
        restAttrs = _objectWithoutProperties(element, ["elements"]);

    if (element.type === 'document' && judgeIfBlockElement(element)) {
      // 当前元素为block根结点
      var newBlock = _cloneDeep(element);

      newBlock.elements = [];
      json.elements.push(newBlock);
      var length = json.elements.length;
      targetJson = json.elements[length - 1];
    } else {
      var ifCommon = !judgeIfBlockElement(element); // 标记是否为不在block内的普通页面元素

      var parentBlock = getBlockFromChildId(data, element);

      if (parentBlock) {
        targetJson = json;

        if (judgeIfBlockElement(parentBlock)) {
          ifCommon = false;

          if (!judgeIfBlockElement(element)) {
            // 在block内部的普通页面元素
            json.elements.push(_objectSpread(_objectSpread({}, restAttrs), {}, {
              for: parentBlock.name,
              elements: []
            }));
            var _length = json.elements.length;
            targetJson = json.elements[_length - 1];
          }
        }
      }

      if (ifCommon) {
        if (!json.elements) {
          json.elements = [];
          Object.keys(restAttrs).forEach(function (key) {
            json[key] = restAttrs[key];
          });
          targetJson = json;
        } else {
          json.elements.push(_objectSpread(_objectSpread({}, restAttrs), {}, {
            elements: []
          }));
          var _length2 = json.elements.length;
          targetJson = json.elements[_length2 - 1];
        }
      }
    }

    elements.forEach(function (item) {
      recurseFun(item, targetJson);
    });
  };

  recurseFun(_cloneDeep(data), target);
  return target;
}; // 对json二次处理，每层data外套一层Col


var addRowColInData = function addRowColInData(data) {
  var target = {};

  var recurseFun = function recurseFun(element, json) {
    var _element$elements2 = element.elements,
        elements = _element$elements2 === void 0 ? [] : _element$elements2,
        restAttrs = _objectWithoutProperties(element, ["elements"]);

    var targetJson;

    if (element.type === 'document' && element.path.length <= 1) {
      // 最外层doucument内加一层Row
      var row = {
        type: 'row',
        elements: []
      };
      json.elements = [row];
      Object.keys(restAttrs).forEach(function (key) {
        json[key] = restAttrs[key];
      });
      targetJson = json.elements[0];
    } else {
      var _element$data2, _element$data2$compon;

      // 其他所有组件(tableCell除外)外侧需要加一层Col
      if (!json.elements) {
        json.elements = [];
      }

      if (['headerCell', 'rowCell', 'button'].includes(element.type)) {
        var _element$data, _element$data$setting;

        (_element$data = element.data) === null || _element$data === void 0 ? true : (_element$data$setting = _element$data.settings) === null || _element$data$setting === void 0 ? true : delete _element$data$setting.cols;
        json.elements.push(element);
      } else if (element.type === 'if') {
        // if容器不需要添加
        targetJson = json;
      } else if ((_element$data2 = element.data) !== null && _element$data2 !== void 0 && (_element$data2$compon = _element$data2.componentSettings) !== null && _element$data2$compon !== void 0 && _element$data2$compon.currentCondition) {
        // if内分支容器不需要添加Col
        json.elements.push(_objectSpread(_objectSpread({}, restAttrs), {}, {
          elements: []
        }));
        var length = json.elements.length;
        targetJson = json.elements[length - 1];
      } else {
        var _element$data3;

        var _ref2 = ((_element$data3 = element.data) === null || _element$data3 === void 0 ? void 0 : _element$data3.settings) || {},
            _ref2$cols = _ref2.cols,
            cols = _ref2$cols === void 0 ? defaultCols : _ref2$cols; // if (cols.span === 24) {
        //   json.elements.push(element);
        //   targetJson = json.elements[length - 1];
        // } else {


        var col = {
          type: 'col',
          data: {
            settings: {
              cols: cols
            }
          },
          elements: [_objectSpread(_objectSpread({}, restAttrs), {}, {
            elements: []
          })]
        };
        json.elements.push(col);
        var _length3 = json.elements.length;
        targetJson = json.elements[_length3 - 1].elements[0]; // }
      }
    }

    elements.forEach(function (item, childIndex) {
      if (targetJson) {
        var child = _cloneDeep(item);

        if (element.type === 'if') {
          var _element$data4, _element$data4$compon;

          // if条件控件将属性放至child
          child.data.componentSettings.currentCondition = (_element$data4 = element.data) === null || _element$data4 === void 0 ? void 0 : (_element$data4$compon = _element$data4.componentSettings) === null || _element$data4$compon === void 0 ? void 0 : _element$data4$compon.currentCondition;

          if (childIndex === 0) {
            var _element$data5, _element$data5$compon;

            child.data.componentSettings['v-if'] = (_element$data5 = element.data) === null || _element$data5 === void 0 ? void 0 : (_element$data5$compon = _element$data5.componentSettings) === null || _element$data5$compon === void 0 ? void 0 : _element$data5$compon.condition;
          } else {
            child.data.componentSettings['v-else'] = true;
          }
        }

        recurseFun(child, targetJson);
      }
    });
  };

  recurseFun(_cloneDeep(data), target);
  return target;
};

var jsonParser = function jsonParser(json) {
  var ignoreBlock = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  settings = _objectSpread(_objectSpread({}, defaultSettings), {}, {
    ignoreBlock: ignoreBlock
  });
  var handleBlockData = ignoreBlock ? json : transformBlockInData(json);
  var newData = addRowColInData(handleBlockData);
  return formatXml(convertToXml(newData, 0));
};

export default jsonParser;
//# sourceMappingURL=jsonParser.js.map