/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { getCurrentInstance, h } from 'vue';
import isFunction from 'lodash/isFunction';
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';
import { getParams, getDefaultNode } from '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isString';
import 'lodash/isObject';

function handleSlots(instance, name, params) {
  var _instance$slots$camel, _instance$slots, _instance$slots$kebab, _instance$slots2;
  var finalParams = new Function("return ".concat(h.toString()))();
  if (params) Object.assign(finalParams, params);
  var node = (_instance$slots$camel = (_instance$slots = instance.slots)[camelCase(name)]) === null || _instance$slots$camel === void 0 ? void 0 : _instance$slots$camel.call(_instance$slots, finalParams);
  if (node) return node;
  node = (_instance$slots$kebab = (_instance$slots2 = instance.slots)[kebabCase(name)]) === null || _instance$slots$kebab === void 0 ? void 0 : _instance$slots$kebab.call(_instance$slots2, finalParams);
  if (node) return node;
  return null;
}
function isEmptyNode(node) {
  if ([void 0, null, ""].includes(node)) return true;
  var innerNodes = Array.isArray(node) ? node : [node];
  var r = innerNodes.filter(function (node2) {
    var _node2$type;
    return (node2 === null || node2 === void 0 ? void 0 : (_node2$type = node2.type) === null || _node2$type === void 0 ? void 0 : _node2$type.toString()) !== "Symbol(Comment)";
  });
  return !r.length;
}
var useTNodeJSX = function useTNodeJSX() {
  var instance = getCurrentInstance();
  return function (name, options) {
    var params = getParams(options);
    var defaultNode = getDefaultNode(options);
    var propsNode;
    if (Object.keys(instance.props).includes(name)) propsNode = instance.props[name];
    if (propsNode === false) return;
    if (propsNode === true) return handleSlots(instance, name, params) || defaultNode;
    if (isFunction(propsNode)) return propsNode(h, params);
    var isPropsEmpty = [void 0, params, ""].includes(propsNode);
    if (isPropsEmpty && (instance.slots[camelCase(name)] || instance.slots[kebabCase(name)])) return handleSlots(instance, name, params);
    return propsNode;
  };
};
var useTNodeDefault = function useTNodeDefault() {
  var renderTNodeJSX = useTNodeJSX();
  return function (name, options) {
    var defaultNode = getDefaultNode(options);
    return renderTNodeJSX(name, options) || defaultNode;
  };
};
var useContent = function useContent() {
  var renderTNodeJSX = useTNodeJSX();
  return function (name1, name2, options) {
    var params = getParams(options);
    var defaultNode = getDefaultNode(options);
    var toParams = params ? {
      params: params
    } : void 0;
    var node1 = renderTNodeJSX(name1, toParams);
    var node2 = renderTNodeJSX(name2, toParams);
    var res = isEmptyNode(node1) ? node2 : node1;
    return isEmptyNode(res) ? defaultNode : res;
  };
};

export { useContent, useTNodeDefault, useTNodeJSX };
//# sourceMappingURL=tnode.js.map
