import _get from "lodash/get";
import invariant from "invariant";
import { getPlugin } from "../../plugins";
export var getPluginType = function getPluginType(name) {
  var plugin = getPlugin(name);
  return plugin ? plugin.type : null;
};
/**
 * Get editor `ui` state
 */

export var getUi = function getUi(state) {
  return state.ui || {};
};
/**
 * Get editor `page` state
 */

export var getPage = function getPage(state) {
  return state.page || state.appBuilder.page || {};
};
export var getElements = function getElements(state) {
  var _state$appBuilder;

  return ((_state$appBuilder = state.appBuilder) === null || _state$appBuilder === void 0 ? void 0 : _state$appBuilder.elements) || {};
};
export var getAppBuilder = function getAppBuilder(state) {
  return state.appBuilder || {};
};
export var getConfig = function getConfig(state) {
  var _state$appBuilder2;

  return ((_state$appBuilder2 = state.appBuilder) === null || _state$appBuilder2 === void 0 ? void 0 : _state$appBuilder2.config) || {};
};
/**
 * Get editor `revisions` state
 */

export var getRevisions = function getRevisions(state) {
  return state.revisions || [];
};
/**
 * Get editor content.
 */

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

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

  var document = getPlugin("pb-editor-page-element-document");
  invariant(document, "\"pb-editor-page-element-document\" plugin must exist for Page Builder to work!");
  return document.create();
};
/**
 * 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) {
  var element = getElement(state, id);

  if (!element) {
    return null;
  }

  var content = getContent(state);

  if (element.path.length <= 1) {
    return content;
  }

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

export var getElement = function getElement(state, id) {
  var _ref = state.appBuilder || {},
      _ref$elements = _ref.elements,
      elements = _ref$elements === void 0 ? {} : _ref$elements;

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


  return Object.values(elements).find(function (el) {
    return el.path === id;
  });
};
export var getElementByElements = function getElementByElements(elements, id) {
  if (elements.hasOwnProperty(id)) {
    return elements[id];
  } // Find by path


  return Object.values(elements).find(function (el) {
    return el.path === id;
  });
}; // 获取当前element的树形结构json

export var getElementWidthChildren = function getElementWidthChildren(state, id) {
  var element = getElement(state, id);
  var content = getContent(state);

  if (!element) {
    return {};
  }

  var elementPaths = element.path.split(".");

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

  return _get(content, elementPaths.join(".elements.").slice(2));
};
/**
 * Get parent element and all of its children recursively
 * WARNING: use carefully as this makes render optimization really difficult when used in `connect`!
 * @param state
 * @param id
 * @returns {*}
 */

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

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

  return _get(content, parentPaths.join(".elements.").slice(2));
}; // 根据children查找form

export var getParentFormWidthChildren = function getParentFormWidthChildren(state, id) {
  var element = getElement(state, id);
  var content = getContent(state);
  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);
}; // 根据childId查找容器元素

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

  if (element.path.length <= 1) {
    return content;
  }

  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);
}; // 根据子节点id寻找当前table element

export var getTableElementWithChildren = function getTableElementWithChildren(state, id) {
  var element = getElement(state, id);
  var content = getContent(state);

  if (element.path.length <= 1) {
    return content;
  }

  if (element.type === 'table') {
    return _get(content, element.path.split(".").join(".elements.").slice(2));
  }

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

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

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

  return getTableElementWithChildren(state, parentElement.id);
};
export var getActiveElement = function getActiveElement(state) {
  return getElement(state, getActiveElementId(state));
};
export var getActiveElementWithChildren = function getActiveElementWithChildren(state) {
  return getElementWithChildren(state, getActiveElementId(state));
};
/**
 * Get active element ID.
 */

export var getActiveElementId = function getActiveElementId(state) {
  return getUi(state).activeElement;
};
export var getHighlightElement = function getHighlightElement(state) {
  return getElement(state, getHighlightElementId(state));
};
export var getHighlightElementId = function getHighlightElementId(state) {
  return getUi(state).highlightElement;
};
/**
 * Get editor plugins (this mostly contains UI state).
 */

export var getPlugins = function getPlugins(state) {
  return getUi(state).plugins || {};
};
/**
 * Get editor plugins of certain type (this mostly contains UI state).
 */

export var getPluginsByType = function getPluginsByType(type) {
  return function (state) {
    return getPlugins(state)[type] || [];
  };
};
/**
 * Get an active plugin of the given type.
 */

export var getActivePlugins = function getActivePlugins(type) {
  var pluginsByType = getPluginsByType(type);
  return function (state) {
    return pluginsByType(state) || [];
  };
};
/**
 * Get active plugin params
 */

export var getActivePluginParams = function getActivePluginParams(name) {
  var type = getPluginType(name);

  if (typeof type !== "string") {
    return null;
  }

  var pluginsByType = getPluginsByType(type);
  return function (state) {
    var plugins = pluginsByType(state);

    if (plugins) {
      var plugin = plugins.find(function (pl) {
        return pl.name === name;
      });
      return plugin ? plugin.params : null;
    }

    return null;
  };
};
export var isPluginActive = function isPluginActive(name) {
  var type = getPluginType(name);

  if (typeof type !== "string") {
    // eslint-disable-next-line
    return function (state) {
      return false;
    };
  }

  var pluginsByType = getPluginsByType(type);
  return function (state) {
    return Boolean(pluginsByType(state).find(function (pl) {
      return pl.name === name;
    }));
  };
};
/**
 * Get dragging state.
 */

export var getIsDragging = function getIsDragging(state) {
  return getUi(state).dragging;
};
/**
 * Get props that need to be passed to an <Element>.
 */

export var getElementProps = function getElementProps(state, _ref2) {
  var id = _ref2.id;

  var _getUi = getUi(state),
      activeElement = _getUi.activeElement,
      highlightElement = _getUi.highlightElement,
      resizing = _getUi.resizing,
      dragging = _getUi.dragging;

  var element = state.appBuilder.elements[id] || {};
  var active = activeElement && activeElement === element.id;
  var highlight = active || highlightElement && highlightElement === id;
  return {
    active: active,
    highlight: highlight && !dragging && !resizing
  };
};
//# sourceMappingURL=index.js.map