import { Fragment, Text, Comment, createTextVNode, isVNode } from 'vue';

const TEMPLATE = 'template';
const isFragment = node => node.type === Fragment;
const isText = node => node.type === Text;
const isComment = node => node.type === Comment;
const isTemplate = node => node.type === TEMPLATE;

/**
 * determine if the element is a valid element type rather than fragments and comment e.g. <template> v-if
 * @param node {VNode} node to be tested
 */
const isValidElementNode = node => !(isFragment(node) || isComment(node));
function getFirstValidNode(vNodes) {
  const slotContent = flatten(vNodes);
  // vue will normalize the slot, so slot must be an array
  if (slotContent.length === 1) {
    return slotContent[0];
  } else {
    console.warn('getFirstSlotVNode', `vNodes should have exactly one child`);
    return null;
  }
}
function getSlot(slots) {
  let slotName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  let props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
  const slot = slots[slotName];
  if (slot === undefined) {
    console.warn('getSlot', `slot[${slotName}] is empty.`);
    return null;
  }
  return slot(props);
}

// o(n) flatten
function flatten(vNodes) {
  let result = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  let key = arguments.length > 2 ? arguments[2] : undefined;
  vNodes.forEach(vNode => {
    if (vNode === null) {
      return;
    }
    if (typeof vNode !== 'object') {
      if (typeof vNode === 'string' || typeof vNode === 'number') {
        result.push(createTextVNode(String(vNode)));
      }
      return;
    }
    if (Array.isArray(vNode)) {
      flatten(vNode, result);
      return;
    }
    if (vNode.type === Fragment) {
      if (vNode.children === null) {
        return;
      }
      const currentKey = key ? `${key}_${String(vNode.key)}` : String(vNode.key);
      if (Array.isArray(vNode.children)) {
        vNode.children.forEach((node, index) => {
          if (isVNode(node)) {
            if (node.key === undefined || node.key === null) {
              node.key = `${currentKey}_${index}`;
            }
          }
        });
        flatten(vNode.children, result, currentKey);
      }
    } else if (vNode.type !== Comment) {
      // rawSlot
      result.push(vNode);
    }
  });
  return result;
}

export { flatten, getFirstValidNode, getSlot, isComment, isFragment, isTemplate, isText, isValidElementNode };
