import { defineComponent, useSlots, useAttrs, computed, ref, resolveComponent, createBlock, openBlock, mergeProps, createSlots, withCtx, unref, renderSlot, normalizeProps, guardReactiveProps } from 'vue';
import { useRoute } from 'vue-router';
import { omit } from 'lodash-es';
import { buttonProps } from './button.mjs';
import { isBoolean } from '../../../../utils/src/is.mjs';
import { Clarity } from '../../../../utils/src/clarity/index.mjs';

var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "GmButton",
    inheritAttrs: false
  },
  __name: "Button",
  props: buttonProps,
  setup(__props, { expose: __expose }) {
    const slots = useSlots();
    const attrs = useAttrs();
    const props = __props;
    const route = useRoute();
    const bindAttrs = computed(() => {
      return { ...omit(attrs, "onClick"), ...props };
    });
    const elRef = ref();
    const isLoading = ref(false);
    const realLoading = computed(() => {
      if (isBoolean(props.loading)) {
        return props.loading;
      }
      return isLoading.value;
    });
    const sendClarityEvent = () => {
      if (!Clarity.isAvailable()) {
        return;
      }
      const buttonText = elRef.value?.$el?.innerText?.trim() || "";
      const actionName = props.eventName || buttonText;
      if (!actionName) {
        return;
      }
      const routeIdentifier = route.meta?.title || route.name || route.path;
      const clarityEventName = `${routeIdentifier} - ${actionName}`;
      Clarity.event(clarityEventName);
    };
    const handleClick = async (evt) => {
      try {
        isLoading.value = true;
        const result = await attrs.onClick?.(evt);
        sendClarityEvent();
        return result;
      } finally {
        isLoading.value = false;
      }
    };
    __expose({
      elRef
    });
    return (_ctx, _cache) => {
      const _component_el_button = resolveComponent("el-button");
      return openBlock(), createBlock(_component_el_button, mergeProps({
        ref_key: "elRef",
        ref: elRef
      }, bindAttrs.value, {
        type: _ctx.disabled ? "info" : _ctx.type,
        loading: realLoading.value,
        onClick: handleClick
      }), createSlots({
        _: 2
        /* DYNAMIC */
      }, [
        unref(slots).loading ? {
          name: "loading",
          fn: withCtx((slotValue) => [
            renderSlot(_ctx.$slots, "loading", normalizeProps(guardReactiveProps(slotValue)))
          ]),
          key: "0"
        } : void 0,
        unref(slots).default ? {
          name: "default",
          fn: withCtx(() => [
            renderSlot(_ctx.$slots, "default")
          ]),
          key: "1"
        } : void 0,
        unref(slots).icon ? {
          name: "icon",
          fn: withCtx((slotValue) => [
            renderSlot(_ctx.$slots, "icon", normalizeProps(guardReactiveProps(slotValue)))
          ]),
          key: "2"
        } : void 0
      ]), 1040, ["type", "loading"]);
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=Button.vue2.mjs.map
