import { getCurrentInstance, shallowReactive, toRaw, watchEffect, defineComponent as defineComponent$1 } from "vue";
import { toKebabCase } from "./dist/index.mjs";
import { useDefaults } from "../composables/defaults/index.mjs";
import { consoleWarn } from "./console.mjs";
function propIsDefined(vnode, prop) {
  var _a, _b;
  return ((_a = vnode.props) == null ? void 0 : _a.hasOwnProperty(prop)) || ((_b = vnode.props) == null ? void 0 : _b.hasOwnProperty(toKebabCase(prop)));
}
const defineComponent = function defineComponent2(options) {
  var _a;
  options._setup = (_a = options._setup) != null ? _a : options.setup;
  if (!options.name) {
    consoleWarn("The component is missing an explicit name, unable to generate default prop value");
    return options;
  }
  if (options._setup) {
    options.setup = function setup(props, ctx) {
      const vm = getCurrentInstance();
      const defaults = useDefaults();
      const _props = shallowReactive({
        ...toRaw(props)
      });
      watchEffect(() => {
        var _a2, _b;
        const globalDefaults = defaults.value.global;
        const componentDefaults = defaults.value[options.name];
        for (const prop of Object.keys(props)) {
          let newVal;
          if (propIsDefined(vm.vnode, prop)) {
            newVal = props[prop];
          } else {
            newVal = (_b = (_a2 = componentDefaults == null ? void 0 : componentDefaults[prop]) != null ? _a2 : globalDefaults == null ? void 0 : globalDefaults[prop]) != null ? _b : props[prop];
          }
          if (_props[prop] !== newVal) {
            _props[prop] = newVal;
          }
        }
      });
      return options._setup(_props, ctx);
    };
  }
  return options;
};
function genericComponent(exposeDefaults = true) {
  return (options) => (exposeDefaults ? defineComponent : defineComponent$1)(options);
}
export {
  defineComponent,
  genericComponent,
  propIsDefined
};
//# sourceMappingURL=define-component.mjs.map
