<template>
  <div class="paragraph" :class="{ 'edit-mode': editMode }">
    <div 
      class="paragraph-content" 
      :contenteditable="editMode" 
      @blur="handleBlur"
      v-html="translatedText"
    ></div>
    <div v-if="editMode" class="block-header-actions">
      <button class="action-btn drag-handle" :title="t('Drag')">
        <Icon icon="tabler:grip-vertical" />
      </button>
      <n-dropdown trigger="click" :options="menuOptions" @select="handleMenuSelect">
        <button class="action-btn" :title="t('More')">
          <Icon icon="tabler:dots" />
        </button>
      </n-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, h } from 'vue'
import { Icon } from '@iconify/vue'
import { NDropdown } from 'naive-ui'
import { t } from '@/shared/i18n'

const props = defineProps<{
  text: string
  editMode: boolean
}>()

const emit = defineEmits<{
  'update:text': [value: string]
  'delete': []
}>()

const translatedText = computed(() => {
  if (!props.text) return ''
  return t(props.text)
})

const menuOptions = computed(() => [
  { label: t('Delete'), key: 'delete', icon: () => h(Icon, { icon: 'tabler:trash', size: 16 }) }
])

function handleBlur(e: FocusEvent) {
  const target = e.target as HTMLElement
  emit('update:text', target.innerText)
}

function handleMenuSelect(key: string) {
  if (key === 'delete') {
    emit('delete')
  }
}
</script>

<style scoped>
.paragraph {
  margin: 16px 0;
  position: relative;
}

.paragraph-content {
  font-size: 14px;
  line-height: 1.6;
  color: #334155;
}

.edit-mode .paragraph {
  background: #fff;
  border: 1px dashed #d1d5db;
  border-radius: 4px;
  padding: 16px;
  min-height: 60px;
}

.edit-mode .paragraph-content[contenteditable="true"] {
  outline: none;
  cursor: text;
}

.edit-mode .paragraph-content[contenteditable="true"]:focus {
  background: #fafafa;
  border-radius: 4px;
  padding: 4px;
}

.block-header-actions {
  display: flex;
  gap: 4px;
  align-items: center;
  opacity: 0;
  transition: opacity 0.2s;
  position: absolute;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
}

.edit-mode:hover .block-header-actions {
  opacity: 1;
}

.action-btn {
  background: transparent;
  border: none;
  color: #64748b;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  font-size: 16px;
}

.action-btn:hover {
  background: #f1f5f9;
  color: #0f172a;
}

.drag-handle {
  cursor: grab;
}

.drag-handle:active {
  cursor: grabbing;
}
</style>
