import { Fragment, isVNode } from 'vue';
import { isOn, isEmptyElement, isValid, isValidElement, isStringElement } from '../is.mjs';
import { camelize, hyphenate } from '../lang/camelize.mjs';
import { classNames } from '../dom/class.mjs';
import { resolvePropValue } from './util.mjs';

function splitAttrs(attrs) {
  const allAttrs = Object.keys(attrs);
  const eventAttrs = {};
  const onEvents = {};
  const extraAttrs = {};
  for (let i = 0, l = allAttrs.length; i < l; i++) {
    const key = allAttrs[i];
    if (isOn(key)) {
      eventAttrs[key[2].toLowerCase() + key.slice(3)] = attrs[key];
      onEvents[key] = attrs[key];
    } else {
      extraAttrs[key] = attrs[key];
    }
  }
  return { onEvents, events: eventAttrs, extraAttrs };
}
function parseStyleText(cssText = "", camel = false) {
  const res = {};
  const listDelimiter = /;(?![^(]*\))/g;
  const propertyDelimiter = /:(.+)/;
  if (typeof cssText === "object")
    return cssText;
  cssText.split(listDelimiter).forEach((item) => {
    if (item) {
      const tmp = item.split(propertyDelimiter);
      if (tmp.length > 1) {
        const k = camel ? camelize(tmp[0].trim()) : tmp[0].trim();
        res[k] = tmp[1].trim();
      }
    }
  });
  return res;
}
function hasProp(instance, prop) {
  return instance[prop] !== void 0;
}
const skipFlattenKey = Symbol("skipFlatten");
function flattenChildren(children = [], filterEmpty2 = true) {
  const temp = Array.isArray(children) ? children : [children];
  const res = [];
  temp.forEach((child) => {
    if (Array.isArray(child)) {
      res.push(...flattenChildren(child, filterEmpty2));
    } else if (child && child.type === Fragment) {
      if (child.key === skipFlattenKey)
        res.push(child);
      else
        res.push(...flattenChildren(child.children, filterEmpty2));
    } else if (child && isVNode(child)) {
      if (filterEmpty2 && !isEmptyElement(child))
        res.push(child);
      else if (!filterEmpty2)
        res.push(child);
    } else if (isValid(child)) {
      res.push(child);
    }
  });
  return res;
}
function getSlot(self, name = "default", options = {}) {
  if (isVNode(self)) {
    if (self.type === Fragment)
      return name === "default" ? flattenChildren(self.children) : [];
    else if (self.children && self.children[name])
      return flattenChildren(self.children[name](options));
    else
      return [];
  } else {
    const res = self.$slots[name] && self.$slots[name](options);
    return flattenChildren(res);
  }
}
function findDOMNode(instance) {
  var _a;
  let node = ((_a = instance == null ? void 0 : instance.vnode) == null ? void 0 : _a.el) || instance && (instance.$el || instance);
  while (node && !node.tagName)
    node = node.nextSibling;
  return node;
}
function getOptionProps(instance) {
  const res = {};
  if (instance.$ && instance.$.vnode) {
    const props = instance.$.vnode.props || {};
    Object.keys(instance.$props).forEach((k) => {
      const v = instance.$props[k];
      const hyphenateKey = hyphenate(k);
      if (v !== void 0 || hyphenateKey in props)
        res[k] = v;
    });
  } else if (isVNode(instance) && typeof instance.type === "object") {
    const originProps = instance.props || {};
    const props = {};
    Object.keys(originProps).forEach((key) => {
      props[camelize(key)] = originProps[key];
    });
    const options = instance.type.props || {};
    Object.keys(options).forEach((k) => {
      const v = resolvePropValue(options, props, k, props[k]);
      if (v !== void 0 || k in props)
        res[k] = v;
    });
  }
  return res;
}
function getComponent(instance, prop = "default", options = instance, execute = true) {
  let com;
  if (instance.$) {
    const temp = instance[prop];
    if (temp !== void 0) {
      return typeof temp === "function" && execute ? temp(options) : temp;
    } else {
      com = instance.$slots[prop];
      com = execute && com ? com(options) : com;
    }
  } else if (isVNode(instance)) {
    const temp = instance.props && instance.props[prop];
    if (temp !== void 0 && instance.props !== null) {
      return typeof temp === "function" && execute ? temp(options) : temp;
    } else if (instance.type === Fragment) {
      com = instance.children;
    } else if (instance.children && instance.children[prop]) {
      com = instance.children[prop];
      com = execute && com ? com(options) : com;
    }
  }
  if (Array.isArray(com)) {
    com = flattenChildren(com);
    com = com.length === 1 ? com[0] : com;
    com = com.length === 0 ? void 0 : com;
  }
  return com;
}
function getKey(ele) {
  const key = ele.key;
  return key;
}
function getEvents(ele = {}, on = true) {
  let props = {};
  if (ele.$)
    props = { ...props, ...ele.$attrs };
  else
    props = { ...props, ...ele.props };
  return splitAttrs(props)[on ? "onEvents" : "events"];
}
function getClass(ele) {
  const props = (isVNode(ele) ? ele.props : ele.$attrs) || {};
  const tempCls = props.class || {};
  let cls = {};
  if (typeof tempCls === "string") {
    tempCls.split(" ").forEach((c) => {
      cls[c.trim()] = true;
    });
  } else if (Array.isArray(tempCls)) {
    classNames(tempCls).split(" ").forEach((c) => {
      cls[c.trim()] = true;
    });
  } else {
    cls = { ...cls, ...tempCls };
  }
  return cls;
}
function getStyle(ele, camel) {
  const props = (isVNode(ele) ? ele.props : ele.$attrs) || {};
  let style = props.style || {};
  if (typeof style === "string") {
    style = parseStyleText(style, camel);
  } else if (camel && style) {
    const res = {};
    Object.keys(style).forEach((k) => res[camelize(k)] = style[k]);
    return res;
  }
  return style;
}
function getComponentName(opts) {
  return opts && (opts.Ctor.options.name || opts.tag);
}
function filterEmpty(children = []) {
  const res = [];
  children.forEach((child) => {
    if (Array.isArray(child))
      res.push(...child);
    else if ((child == null ? void 0 : child.type) === Fragment)
      res.push(...filterEmpty(child.children));
    else
      res.push(child);
  });
  return res.filter((c) => !isEmptyElement(c));
}
function filterEmptyWithUndefined(children) {
  if (children) {
    const coms = filterEmpty(children);
    return coms.length ? coms : void 0;
  } else {
    return children;
  }
}
function getPropsSlot(slots, props, prop = "default") {
  var _a, _b;
  return (_b = props[prop]) != null ? _b : (_a = slots[prop]) == null ? void 0 : _a.call(slots);
}
function getTextFromElement(ele) {
  if (isValidElement(ele) && isStringElement(ele[0]))
    return ele[0].children;
  return ele;
}

export { filterEmpty, filterEmptyWithUndefined, findDOMNode, flattenChildren, getClass, getComponent, getComponentName, getEvents, getKey, getOptionProps, getPropsSlot, getSlot, getStyle, getTextFromElement, hasProp, parseStyleText, skipFlattenKey, splitAttrs };
//# sourceMappingURL=props.mjs.map
