<script lang="ts">
  import { computed, defineComponent, h, unref } from 'vue';
  import BasicButton from './BasicButton.vue';
  import { Popconfirm } from 'ant-design-vue';
  import { extendSlots } from '@/utils/helper/tsxHelper';
  import { omit } from 'lodash-es';
  import { useAttrs } from '@/hooks/vben/useAttrs';
  <%_ if (enableTranslation) { _%>
  import { useI18n } from '@/hooks/web/useI18nOut';
  <%_ } _%>

  const props = {
    /**
     * Whether to enable the drop-down menu
     * @default: true
     */
    enable: {
      type: Boolean,
      default: true,
    },
  };

  export default defineComponent({
    name: 'PopButton',
    inheritAttrs: false,
    props,
    setup(props, { slots }) {
      <%_ if (enableTranslation) { _%>
      const { t } = useI18n();
      <%_ } _%>
      const attrs = useAttrs();

      // get inherit binding value
      const getBindValues = computed(() => {
        return Object.assign(
          {
            okText: <% if (enableTranslation) { %>t('common.okText')<% } else { %>'t$t('common.okText')$t$'<% } %>,
            cancelText: <% if (enableTranslation) { %>t('common.cancelText')<% } else { %>'t$t('common.cancelText')$t$'<% } %>,
          },
          { ...props, ...unref(attrs) },
        );
      });

      return () => {
        const bindValues = omit(unref(getBindValues), 'icon', 'color');
        const btnBind = omit(unref(getBindValues), 'title') as any;
        if (btnBind.disabled) btnBind.color = '';
        const Button = h(BasicButton, btnBind, extendSlots(slots));

        // If it is not enabled, it is a normal button
        if (!props.enable) {
          return Button;
        }
        return h(Popconfirm, bindValues, { default: () => Button });
      };
    },
  });
</script>
