import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _get from "lodash/get";
import dslParser from "./parser/dslParser";
import { updateChildPaths, addElementByPath } from "../editor/utils";
import cloneDeep from 'lodash/cloneDeep'; // 区块blockJson加入编辑器前处理

export var signBlockJson = function signBlockJson(json, blockInfo) {
  var newBlock = cloneDeep(json);

  if (!newBlock.data) {
    newBlock.data = {};
  }

  newBlock.data.blockSettings = {
    isBlock: true
  };
  newBlock.name = blockInfo.name; // 区块内的静态内容不可点击和调整，动态placeholder及其内部children可增删改

  var mapChildren = function mapChildren(element, parent) {
    var _element$elements = element.elements,
        elements = _element$elements === void 0 ? [] : _element$elements,
        type = element.type;
    delete element.id;

    if (!element.data) {
      element.data = {};
    }

    if (!element.data.blockSettings) {
      element.data.blockSettings = {};
    }

    if (type === 'placeholder') {
      element.data.blockSettings.canEdit = true;
    } else if (parent) {
      var _parent$data, _parent$data$blockSet;

      element.data.blockSettings.canEdit = (_parent$data = parent.data) === null || _parent$data === void 0 ? void 0 : (_parent$data$blockSet = _parent$data.blockSettings) === null || _parent$data$blockSet === void 0 ? void 0 : _parent$data$blockSet.canEdit;
    } else {
      element.data.blockSettings.canEdit = false;
    }

    elements.forEach(function (child) {
      mapChildren(child, element);
    });
  };

  mapChildren(newBlock);
  return newBlock;
}; // 判断当前元素是否为block元素

export var judgeIfBlockElement = function judgeIfBlockElement(element) {
  if (element.type === 'document') {
    var _element$data;

    var _ref = ((_element$data = element.data) === null || _element$data === void 0 ? void 0 : _element$data.blockSettings) || {},
        isBlock = _ref.isBlock;

    return isBlock;
  }

  if (element.data && element.data.blockSettings && Object.prototype.hasOwnProperty.call(element.data.blockSettings, 'canEdit')) {
    return true;
  }

  return false;
}; // 根据childElement查找block

export var getBlockFromChildId = function getBlockFromChildId(json, childElement) {
  var _parentElement$data, _parentElement$data$b;

  if (!childElement.path) {
    return null;
  }

  var parentPaths = childElement.path.split(".").slice(0, -1);

  if (parentPaths.length <= 1) {
    return null;
  }

  var parentElement = _get(json, parentPaths.join(".elements.").slice(2));

  if (parentElement && parentElement.type === 'document' && (_parentElement$data = parentElement.data) !== null && _parentElement$data !== void 0 && (_parentElement$data$b = _parentElement$data.blockSettings) !== null && _parentElement$data$b !== void 0 && _parentElement$data$b.isBlock) {
    return parentElement;
  }

  return getBlockFromChildId(json, parentElement);
}; // 判断当前元素是否为block内的静态元素

export var judgeIfBlockStaticElement = function judgeIfBlockStaticElement(element) {
  if (judgeIfBlockElement(element)) {
    var _element$data2;

    var _ref2 = ((_element$data2 = element.data) === null || _element$data2 === void 0 ? void 0 : _element$data2.blockSettings) || {},
        canEdit = _ref2.canEdit;

    return !canEdit;
  }

  return false;
}; // 将block转换为真实json

export var transformBlockToJson = function transformBlockToJson(data, allBlocks) {
  var newJson = {};
  var commonElementsInBlock = [];

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

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

    var isBlockElement = judgeIfBlockElement(element);

    if (element.type === 'document') {
      if (isBlockElement) {
        // 当前element为block元素
        var name = element.name;

        var _ref3 = allBlocks.find(function (v) {
          return v.name === name;
        }) || {},
            metaData = _ref3.metaData;

        if (metaData) {
          var blockJson = signBlockJson(dslParser(metaData), {
            name: name
          });
          blockJson.path = element.path;

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

          json.elements.push(blockJson); // const length = json.elements.length;
          // targetJson = json.elements[length - 1];

          targetJson = json;
        }
      } else {
        json.elements = [];
        Object.keys(restAttrs).forEach(function (key) {
          json[key] = restAttrs[key];
        });
        targetJson = json;
      }
    } else if (forBlock) {
      // block内的普通元素
      commonElementsInBlock.push(element);
      targetJson = json;
    } else if (!isBlockElement) {
      // 页面内的普通元素
      json.elements.push(_objectSpread(_objectSpread({}, restAttrs), {}, {
        elements: []
      }));
      var length = json.elements.length;
      targetJson = json.elements[length - 1];
    }

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

  recurseFun(data, newJson);
  updateChildPaths(newJson);

  if (commonElementsInBlock.length > 0) {
    // 根据path将页面元素添加至block内
    commonElementsInBlock.forEach(function (child) {
      newJson = addElementByPath(child, newJson, child.path);
    });
  }

  return newJson;
};
//# sourceMappingURL=blockUtil.js.map