"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const is = require("./is.js");
const dom = require("./dom.js");
const isElement = (vnode) => {
  return Boolean(
    vnode && vnode.shapeFlag & 1
    /* ELEMENT */
  );
};
const isTextElement = (vnode) => {
  return Boolean(
    vnode && vnode.shapeFlag & 8
    /* TEXT_CHILDREN */
  );
};
function isComponent(vnode, _type) {
  return Boolean(
    vnode && vnode.shapeFlag & 6
    /* COMPONENT */
  );
}
const isSlotsChildren = (vnode, _children) => {
  return Boolean(
    vnode && vnode.shapeFlag & 32
    /* SLOTS_CHILDREN */
  );
};
const isArrayChildren = (vn, _children) => {
  return Boolean(
    vn && vn.shapeFlag & 16
    /* ARRAY_CHILDREN */
  );
};
function isComponentPublicInstance(val) {
  return Boolean(val == null ? void 0 : val.$el);
}
function getFirstComponent(vn) {
  var _a, _b;
  if (is.isArray(vn)) {
    for (const child of vn) {
      const result = getFirstComponent(child);
      if (result) {
        return result;
      }
    }
  } else if (isElement(vn) || isComponent(vn) || isTextElement(vn) && vn.type !== vue.Comment) {
    return vn;
  } else if (isArrayChildren(vn, vn.children)) {
    for (const child of vn.children) {
      const result = getFirstComponent(child);
      if (result) {
        return result;
      }
    }
  } else if (isSlotsChildren(vn, vn.children)) {
    const children = (_b = (_a = vn.children).default) == null ? void 0 : _b.call(_a);
    if (children) {
      const result = getFirstComponent(children);
      if (result) {
        return result;
      }
    }
  }
  return null;
}
const resolveHtmlElement = (elRef) => {
  const queryElement = (el) => {
    if (typeof el === "string") {
      return document.querySelector(el);
    } else if (dom.isHtmlElement(el)) {
      return el;
    }
    return null;
  };
  return new Promise((resolve) => {
    if (vue.isRef(elRef)) {
      vue.watchEffect(() => {
        const { value } = elRef;
        if (value) {
          if (isComponentPublicInstance(value)) {
            resolve(value.$el);
          } else {
            resolve(queryElement(value));
          }
        }
      });
    } else if (isComponentPublicInstance(elRef)) {
      resolve(elRef.$el);
    } else {
      resolve(queryElement(elRef));
    }
  });
};
const isEmptySlot = (slot) => {
  var _a;
  if (!slot) {
    return true;
  }
  const children = slot();
  if (children.length > 1) {
    return false;
  }
  if (children.length === 0) {
    return true;
  }
  if (isTextElement(children[0]) && !children[0].children) {
    return true;
  }
  if (children[0].type === vue.Comment) {
    return true;
  }
  if (children[0].type === vue.Fragment) {
    return !((_a = children[0].children) == null ? void 0 : _a.length);
  }
  return false;
};
function filterSlots(slots, slotNames) {
  const names = Object.values(slotNames);
  const keys = Object.keys(slots);
  const r = keys.filter((item) => names.includes(item));
  return r || [];
}
exports.filterSlots = filterSlots;
exports.getFirstComponent = getFirstComponent;
exports.isArrayChildren = isArrayChildren;
exports.isComponent = isComponent;
exports.isComponentPublicInstance = isComponentPublicInstance;
exports.isElement = isElement;
exports.isEmptySlot = isEmptySlot;
exports.isSlotsChildren = isSlotsChildren;
exports.isTextElement = isTextElement;
exports.resolveHtmlElement = resolveHtmlElement;
