import { updateChildPaths } from "../editor/utils";
import { set } from "dot-prop-immutable";
import cloneDeep from 'lodash/cloneDeep'; // 处理旧数据，将不必要的row column移除

export var compatOldJson = function compatOldJson(element) {
  var _element$elements$, _element$elements$$el, _element$elements$2, _element$elements$2$e;

  if (((_element$elements$ = element.elements[0]) === null || _element$elements$ === void 0 ? void 0 : (_element$elements$$el = _element$elements$.elements) === null || _element$elements$$el === void 0 ? void 0 : _element$elements$$el.length) < 1) return element;

  if (((_element$elements$2 = element.elements[0]) === null || _element$elements$2 === void 0 ? void 0 : (_element$elements$2$e = _element$elements$2.elements) === null || _element$elements$2$e === void 0 ? void 0 : _element$elements$2$e.length) === 1) {
    var _element$elements$3, _element$elements$3$e;

    var column = (_element$elements$3 = element.elements[0]) === null || _element$elements$3 === void 0 ? void 0 : (_element$elements$3$e = _element$elements$3.elements[0]) === null || _element$elements$3$e === void 0 ? void 0 : _element$elements$3$e.elements[0];

    if (column.length <= 1) {
      return element;
    } else if (column.elements[0].type !== 'button') {
      return element;
    }
  }

  var container = {};
  var elementCopy = cloneDeep(element);
  Object.keys(elementCopy).forEach(function (key) {
    if (key !== 'elements') {
      container[key] = elementCopy[key];
    } else {
      container[key] = [];
    }
  }); // document => form => row => column提取

  mapContainer(elementCopy.elements[0], container); // 将组件提取

  var children = mapRows(elementCopy.elements[0].elements); // 组件children拼接到容器内

  addChildrenToParent(container, children); // path重新计算

  updateChildPaths(container);
  return container;
};
export var addChildrenToParent = function addChildrenToParent(container, children) {
  if (container) {
    var type = container.type,
        path = container.path;

    if (type === 'column' && path === '0.0.0.0') {
      if (!container.elements) {
        container.elements = [];
      }

      container.elements = children;
    } else if (path.split('.').length < 4 && container.elements) {
      addChildrenToParent(container.elements[0], children);
    }
  }
};
export var mapContainer = function mapContainer(content, container) {
  if (content) {
    var type = content.type,
        path = content.path;

    if (type === 'form' || type === 'row' && path === '0.0.0' || type === 'column' && path === '0.0.0.0') {
      var obj = {};
      Object.keys(content).forEach(function (key) {
        if (key !== 'elements') {
          obj[key] = content[key];
        } else {
          obj[key] = [];
        }
      });

      if (!container.elements) {
        container.elements = [];
      }

      container.elements.push(obj);

      if (content.elements) {
        mapContainer(content.elements[0], container.elements[0]);
      }
    }
  }
};
export var copyColumn = function copyColumn(column) {
  var _newColumn$data, _newColumn$data$setti;

  var newColumn = {};
  Object.keys(column).forEach(function (key) {
    if (!['elements', 'path', 'id'].includes(key)) {
      newColumn[key] = column[key];
    }
  });
  newColumn.elements = [];
  newColumn.data.direction = 'horizontal';
  (_newColumn$data = newColumn.data) === null || _newColumn$data === void 0 ? true : (_newColumn$data$setti = _newColumn$data.settings) === null || _newColumn$data$setti === void 0 ? true : delete _newColumn$data$setti.cols;
  return newColumn;
};
export var mapRows = function mapRows(children) {
  var rowChildren = [];

  if (Array.isArray(children)) {
    children.forEach(function (child) {
      var elements = child.elements;

      if (elements.length > 1) {
        var childs = elements.map(function (v) {
          var _v$data;

          var child = v.elements[0];

          var _v$data$settings = (_v$data = v.data) === null || _v$data === void 0 ? void 0 : _v$data.settings,
              cols = _v$data$settings.cols;

          if (cols) {
            child = set(child, 'data.settings.cols', cols);
          } else {
            child = set(child, 'data.settings.cols', {
              span: 24 / elements.length,
              offset: 0
            });
          }

          return child;
        });
        var horizontalColumn = copyColumn(cloneDeep(elements[0]));
        horizontalColumn.elements = childs;
        rowChildren.push(horizontalColumn);
      } else if (elements.length === 1 && elements[0].elements.length > 1) {
        var column = elements[0];
        delete column.id;
        column = set(column, 'data.direction', 'horizontal');
        rowChildren.push(column);
      } else {
        rowChildren.push(elements[0].elements[0]);
      }
    });
  }

  return rowChildren;
};
//# sourceMappingURL=compatOldJson.js.map