<template>
  <span class="hidden-text-chip" :class="{ 'is-expanded': expanded }">
    <button
      v-if="!expanded"
      type="button"
      class="hidden-text-chip__trigger"
      :title="`点击展开：${rawText}`"
      @click.stop="expandText"
    >
      <span class="hidden-text-chip__visible">{{ label }}</span>
    </button>

    <span v-else class="hidden-text-chip__expanded">
      <span class="hidden-text-chip__syntax">
        <span class="hidden-text-chip__punct">(</span>
        <input
          ref="displayInputRef"
          v-model="displayedValue"
          type="text"
          class="hidden-text-chip__input hidden-text-chip__input--visible"
          placeholder="显示的文本"
          @input="syncTexts"
        >
        <span class="hidden-text-chip__punct">)</span>
        <span class="hidden-text-chip__punct">{</span>
        <input
          ref="hiddenInputRef"
          v-model="hiddenValue"
          type="text"
          class="hidden-text-chip__input hidden-text-chip__input--hidden"
          placeholder="隐藏的文本"
          @input="syncTexts"
        >
        <span class="hidden-text-chip__punct">}</span>
      </span>

      <button
        type="button"
        class="hidden-text-chip__collapse"
        title="折叠"
        @click.stop="collapseText"
      >
        <span class="hidden-text-chip__triangle">◀</span>
      </button>
    </span>
  </span>
</template>

<script setup>
import { computed, nextTick, ref, watch } from 'vue'
import { serializeHiddenTextSyntax } from '../utils/hiddenText.js'

const props = defineProps({
  displayed: { type: String, default: '' },
  hidden: { type: String, default: '' },
  expanded: { type: Boolean, default: false },
  updateTexts: { type: Function, default: null },
  updateExpanded: { type: Function, default: null },
})

const rawText = computed(() => serializeHiddenTextSyntax(props.displayed, props.hidden))
const label = computed(() => (props.displayed || '未命名文本'))

const displayInputRef = ref(null)
const hiddenInputRef = ref(null)
const displayedValue = ref(props.displayed || '')
const hiddenValue = ref(props.hidden || '')

const syncTexts = () => {
  props.updateTexts?.({
    displayed: displayedValue.value,
    hidden: hiddenValue.value,
  })
}

const expandText = async () => {
  if (props.expanded) return
  props.updateExpanded?.(true)
  await nextTick()
  displayInputRef.value?.focus?.()
  displayInputRef.value?.select?.()
}

const collapseText = () => {
  props.updateExpanded?.(false)
}

watch(
  () => props.displayed,
  (nextValue) => {
    if (nextValue !== displayedValue.value) {
      displayedValue.value = nextValue || ''
    }
  }
)

watch(
  () => props.hidden,
  (nextValue) => {
    if (nextValue !== hiddenValue.value) {
      hiddenValue.value = nextValue || ''
    }
  }
)
</script>

<style scoped>
.hidden-text-chip {
  display: inline-flex;
  align-items: center;
  vertical-align: baseline;
  max-width: 100%;
}

.hidden-text-chip__trigger {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  font: inherit;
}

.hidden-text-chip.is-expanded .hidden-text-chip__trigger {
  display: none;
}

.hidden-text-chip__visible {
  display: inline-flex;
  align-items: center;
  min-height: 1.8em;
  padding: 0.08em 0.52em;
  border-radius: 0.45em;
  background: rgba(148, 163, 184, 0.26);
  color: inherit;
  line-height: 1.5;
  white-space: pre-wrap;
}

:root[data-theme='dark'] .hidden-text-chip__visible {
  background: rgba(100, 116, 139, 0.32);
}

.hidden-text-chip__expanded {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
  flex-wrap: nowrap;
  white-space: nowrap;
}

.hidden-text-chip__syntax {
  display: inline-flex;
  align-items: center;
  gap: 0.08rem;
  flex-wrap: nowrap;
  white-space: nowrap;
}

.hidden-text-chip__punct {
  color: inherit;
  white-space: pre;
}

.hidden-text-chip__input {
  min-width: 4rem;
  max-width: 12rem;
  padding: 0.08rem 0.25rem;
  border: 1px solid rgba(148, 163, 184, 0.45);
  border-radius: 0.35rem;
  background: rgba(255, 255, 255, 0.92);
  color: inherit;
  font: inherit;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hidden-text-chip__input--visible {
  min-width: 5rem;
}

.hidden-text-chip__input--hidden {
  min-width: 5rem;
}

:root[data-theme='dark'] .hidden-text-chip__input {
  background: rgba(15, 23, 42, 0.92);
  border-color: rgba(100, 116, 139, 0.55);
}

.hidden-text-chip__input:focus {
  outline: none;
  border-color: var(--focus-ring);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.18);
}

.hidden-text-chip__collapse {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 1.2rem;
  height: 1.2rem;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.18);
  color: inherit;
  cursor: pointer;
}

.hidden-text-chip__collapse:hover {
  background: rgba(148, 163, 184, 0.3);
}

.hidden-text-chip__triangle {
  display: inline-block;
  font-size: 0.65rem;
  line-height: 1;
  transform: translateX(0.05rem);
}
</style>
