import _get from "lodash/get";
export var getUi = function getUi(state) {
  return state.ui || {};
};
/**
 * Get editor `page` state
 */

export var getPage = function getPage(state, nameSpace) {
  var _state;

  return state.page || ((_state = state[nameSpace || 'appRender']) === null || _state === void 0 ? void 0 : _state.page) || {};
};
/**
 * Get editor content.
 */

export var getContent = function getContent(state, nameSpace) {
  var page = getPage(state, nameSpace);

  if (page.content && page.content.present) {
    return page.content.present;
  } else if (page.content) {
    return page.content;
  }

  return null;
};
/**
 * Get element and all of its children recursively.
 * WARNING: use carefully as this makes render optimization really difficult when used in `connect`!
 */

export var getElementWithChildren = function getElementWithChildren(state, id, nameSpace) {
  var element = getElement(state, id, nameSpace);

  if (!element) {
    return null;
  }

  var content = getContent(state, nameSpace);
  return _get(content, element.path.replace(/\./g, ".elements.").slice(2), content);
};
/**
 * Get element by ID or path.
 * @param state
 * @param id ID or path of the element
 */

export var getElement = function getElement(state, id, nameSpace) {
  var elements = state[nameSpace].elements;

  if (elements.hasOwnProperty(id)) {
    return elements[id];
  } // Find by path


  return Object.values(elements).find(function (el) {
    return el.path === id;
  });
}; // 根据children查找form

export var getParentFormWidthChildren = function getParentFormWidthChildren(state, id, nameSpace) {
  var element = getElement(state, id, nameSpace);
  var content = getContent(state, nameSpace);
  var parentPaths = element.path.split(".").slice(0, -1);

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

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

  if (parentElement.type === 'form') {
    return parentElement;
  }

  return getParentFormWidthChildren(state, parentElement.id, nameSpace);
}; // 根据children查找parent

export var getParentElementWithChildren = function getParentElementWithChildren(state, id, nameSpace) {
  var element = getElement(state, id, nameSpace);
  var content = getContent(state, nameSpace);
  var parentPaths = element.path.split(".").slice(0, -1);

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

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

  return parentElement;
}; // 根据childId查找容器元素

export var getContainerElementWithChildren = function getContainerElementWithChildren(state, id, nameSpace) {
  nameSpace = nameSpace || 'appRender';
  var element = getElement(state, id, nameSpace);
  var content = getContent(state, nameSpace);

  if (['form', 'block'].includes(element.type)) {
    var newElement = _get(content, element.path.split(".").join(".elements.").slice(2));

    return newElement;
  }

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

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

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

  if (['form', 'block'].includes(parentElement.type)) {
    return parentElement;
  }

  return getContainerElementWithChildren(state, parentElement.id, nameSpace);
};
//# sourceMappingURL=index.js.map