/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import camelCase from 'lodash/camelCase';

function emitEvent(props, context, evtName) {
  const apiName = camelCase(`on-${evtName}`);
  evtName.replace(/^on/, "").toLowerCase();
  for (var _len = arguments.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
    args[_key - 3] = arguments[_key];
  }
  if (typeof props[apiName] === "function") props[apiName](...args);
  context.emit(evtName, ...args);
}
function getParentsToRoot(element, root) {
  const list = [];
  let el = element;
  while (el && el.parentNode) {
    list.push(el);
    if (el === root) break;
    el = el.parentNode;
  }
  return list;
}
function getParentMarks(name, element, root) {
  const list = getParentsToRoot(element, root);
  return list.map(el => {
    const mark = {
      name,
      value: el?.getAttribute(name) || "",
      el
    };
    return mark;
  }).filter(mark => mark.value);
}
function getMark(name, element, root) {
  const list = getParentMarks(name, element, root);
  const info = list.pop() || null;
  return info;
}
function getTNode(prop, options) {
  let tnode = null;
  let item = null;
  const conf = {
    ...options
  };
  if (typeof prop === "function") item = prop(conf.createElement, conf.node?.getModel());else if (typeof prop === "string") item = prop;
  if (typeof item === "string") tnode = item;else if (item) tnode = item;
  return tnode;
}
function getLineModel(nodes, node, index) {
  const lineModel = {
    top: false,
    right: false,
    bottom: false,
    left: false
  };
  let nodeChildren = [];
  if (Array.isArray(node.children)) nodeChildren = node.children;
  const childNode = nodes[index - 1] || null;
  const nodeItemIndex = childNode ? childNode.getIndex() : 0;
  if (index === 0) {
    lineModel.left = !!node.parent;
    lineModel.bottom = node.children && node.expanded;
    lineModel.right = node.parent && !node.children;
  } else if (index === 1) {
    lineModel.top = true;
    lineModel.right = true;
    lineModel.bottom = nodeItemIndex < nodeChildren.length - 1;
  } else if (nodeItemIndex < nodeChildren.length - 1) {
    lineModel.top = true;
    lineModel.bottom = true;
  }
  return lineModel;
}
function isTreeNodeValue(item) {
  return typeof item === "string" || typeof item === "number";
}
function getNode(store, item) {
  let node = null;
  let val = null;
  if (typeof item === "string" || typeof item === "number") val = item;else if (item && isTreeNodeValue(item.value)) val = item.value;
  node = store.getNode(val);
  return node;
}

export { emitEvent, getLineModel, getMark, getNode, getParentMarks, getParentsToRoot, getTNode, isTreeNodeValue };
//# sourceMappingURL=utils.js.map
