7:5
 	V    // Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { getNode } from '@formkit/core'
import { type ComputedRef } from 'vue'
import { computed, nextTick, onMounted } from 'vue'

import { MutationHandler } from '#shared/server/apollo/handler/index.ts'

import { stripDataId } from './utils.ts'

import type { Props } from './EntityAttributes.vue'
import type { AttributeField } from './useDisplayEntityAttributes'

export const useInlineEditable = (props: Props, fields: ComputedRef<AttributeField[]>) => {
  const inlineEditEntityAttributes = computed(() =>
    fields.value.filter(
      ({ attribute }) => props.inlineEditable && attribute.name in props.inlineEditable,
    ),
  )

  onMounted(() => {
    nextTick(() => {
      inlineEditEntityAttributes.value.forEach(({ attribute, value }) => {
        if (!attribute?.id) return

        getNode(attribute.id)?.on('change', (event) => {
          const mutationFn = props.inlineEditable?.[attribute.name]

          const updatedValue = stripDataId(event.origin.value as string)
          const initialValue = value as string

          // Currently, we pass initial value without data-id attribute
          // TipTap adds it automatically due to the extension for internal reasons
          // So we need to strip it before comparison
          // ⚠️ Trimming won't work here as the innerHTML contain the indentation

          if (updatedValue === initialValue)
            return event.payload.submitToStopEditing(() => Promise.resolve(true))

          if (!mutationFn) {
            if (import.meta.env.DEV)
              console.warn(
                'No mutation call found for attribute:',
                attribute.name,
                `Object: ${props.object.id}`,
              )
            return
          }

          new MutationHandler(mutationFn({}))
            .send({
              id: props.object.id,
              note: event.origin.value as string,
            })
            .then(() => {
              event.payload.submitToStopEditing(() => Promise.resolve(true))
              // Update the value in memory otherwise the next call if the value stays the same trigger the update mutation again
              value = updatedValue
            })
            .catch(() => event.payload.submitToStopEditing(() => Promise.resolve(false)))
        })
      })
    })
  })
}
 
ý    	<!-- Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/ -->

<script setup lang="ts">
import { toRef } from 'vue'

import { isInlineAttributeEditable } from '#shared/components/EntityAttributes/utils.ts'
import { useSharedVisualConfig } from '#shared/composables/useSharedVisualConfig.ts'
import type { EntityAttribute } from '#shared/entities/core/attributes/types/store.ts'
import { useApplicationStore } from '#shared/stores/application.ts'
import type { ObjectLike } from '#shared/types/utils.ts'

import { useDisplayEntityAttributes } from './useDisplayEntityAttributes.ts'
import { useInlineEditable } from './useInlineEditable.ts'

import type { InlineEditable, OutputMode } from './types.ts'

export interface Props {
  mode?: OutputMode
  object: ObjectLike
  attributes: EntityAttribute[]
  skipAttributes?: string[]
  inlineEditable?: InlineEditable
  includeStatic?: boolean
  alwaysShowAfterFields?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  mode: 'view',
})

const { fields } = useDisplayEntityAttributes(props)
const { entityAttributes: entityAttributesConfig } = useSharedVisualConfig()

const config = toRef(useApplicationStore(), 'config')

const getLabel = (attribute: EntityAttribute) =>
  attribute.dataOption?.display_config
    ? config.value[attribute.dataOption.display_config]
    : attribute.display

const getDisplayLabel = (attribute: EntityAttribute) => {
  // If inline editable by default it shows then the field label
  if (isInlineAttributeEditable(attribute.name, props.inlineEditable)) return null

  return getLabel(attribute)
}

useInlineEditable(props, fields)
</script>

<template>
  <Component :is=