<!--
  Generic "share to chat" button for entity detail pages (order / shipment /
  invoice / product / partner).

  Deliberately CHAT-AGNOSTIC: it imports no chat code and only fires a window
  CustomEvent ('logship:chat-share') that the staff-chat drawer listens for. So:
   - removing the chat module leaves this button building fine (it just hides,
     because config.public.chatEnabled is unset, and the event has no listener);
   - the app→chat isolation rule (CLAUDE.md) is preserved — no app page imports chat.

  Shown only to non-limited staff roles (getMenuType() !== 'c') and only when the
  chat module is enabled. Auto-imported as <ShareToChatButton>.
-->
<script setup>
const props = defineProps({
  entityType: { type: String, required: true },     // 'order' | 'shipment' | 'invoice' | 'product' | 'partner'
  entityId: { type: [String, Number], required: true },
  label: { type: String, default: '' },             // human text shown in the chat card / notification
  url: { type: String, default: '' },               // in-app deep link to the record
  iconOnly: { type: Boolean, default: false }       // icon-only (for square icon-toolbars, e.g. edit save-bars)
})

const config = useRuntimeConfig()
const { getMenuType } = useRoleMenu()
const { t } = useTranslation()

const visible = computed(() =>
  config.public.chatEnabled === true && getMenuType() !== 'c' && !!props.entityId)

const buttonText = computed(() => t.value?.shareToChat || 'Per Chat teilen')

const share = () => {
  if (typeof window === 'undefined') return
  try {
    window.dispatchEvent(new CustomEvent('logship:chat-share', {
      detail: {
        kind: 'entity',
        entityType: props.entityType,
        entityId: Number(props.entityId) || props.entityId,
        label: props.label || `${props.entityType} ${props.entityId}`,
        url: props.url || ''
      }
    }))
  } catch (e) { /* no-op */ }
}
</script>

<template>
  <button
    v-if="visible"
    type="button"
    :class="['button', iconOnly ? 'icon-btn' : 'is-light', 'share-to-chat-btn']"
    :title="buttonText"
    @click="share">
    <span class="tf-icon ti ti-share ti-xs color-inherit" :class="{ 'me-1': !iconOnly }"></span>
    <template v-if="!iconOnly">{{ buttonText }}</template>
  </button>
</template>
