/**
 * ldesign v0.2.3
 * (c) 2022年9月24日 swimly
 * @license MIT
 */

import { h } from '@vue/composition-api';
import isEmpty from 'lodash/isEmpty';
import isString from 'lodash/isString';
import isFunction from 'lodash/isFunction';
import isObject from 'lodash/isObject';
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';

const isVNode = obj => {
  const vNode = h("span", "");
  const VNode2 = vNode.constructor;
  return obj instanceof VNode2;
};
function getDefaultNode(options) {
  let defaultNode;

  if (isObject(options) && "defaultNode" in options) {
    defaultNode = options.defaultNode;
  } else if (isVNode(options) || isString(options)) {
    defaultNode = options;
  }

  return defaultNode;
}
function getParams(options) {
  return isObject(options) && "params" in options ? options.params : null;
}
function handleSlots(instance, params, name) {
  var _instance$$slots$came, _instance$$slots, _instance$$slots$keba, _instance$$slots2;

  let node = (_instance$$slots$came = (_instance$$slots = instance.$slots)[camelCase(name)]) === null || _instance$$slots$came === void 0 ? void 0 : _instance$$slots$came.call(_instance$$slots, params);
  if (node) return node;
  node = (_instance$$slots$keba = (_instance$$slots2 = instance.$slots)[kebabCase(name)]) === null || _instance$$slots$keba === void 0 ? void 0 : _instance$$slots$keba.call(_instance$$slots2, params);
  if (node) return node;
  return null;
}
const renderLNodeJSX = (instance, name, options) => {
  const params = getParams(options);
  const defaultNode = getDefaultNode(options);
  let propsNode;

  if (name in instance) {
    propsNode = instance[name];
  }

  if (propsNode === false) return;

  if (propsNode === true && defaultNode) {
    return handleSlots(instance, params, name) || defaultNode;
  }

  if (isFunction(propsNode)) return propsNode(h, params);
  const isPropsEmpty = [void 0, params, ""].includes(propsNode);

  if (isPropsEmpty && (instance.$slots[camelCase(name)] || instance.$slots[kebabCase(name)])) {
    return handleSlots(instance, params, name);
  }

  return propsNode;
};
const renderLNodeJSXDefault = (vm, name, options) => {
  const defaultNode = getDefaultNode(options);
  return renderLNodeJSX(vm, name, options) || defaultNode;
};
const renderContent = (vm, name1, name2, options) => {
  const params = getParams(options);
  const defaultNode = getDefaultNode(options);
  const toParams = params ? {
    params
  } : void 0;
  const node1 = renderLNodeJSX(vm, name1, toParams);
  const node2 = renderLNodeJSX(vm, name2, toParams);
  const res = isEmpty(node1) ? node2 : node1;
  return isEmpty(res) ? defaultNode : res;
};

export { getDefaultNode, getParams, handleSlots, isVNode, renderContent, renderLNodeJSX, renderLNodeJSXDefault };
//# sourceMappingURL=render-lnode.js.map
