<script setup lang="ts">
import type {ChatTimelineToolApprovalPayload} from './types';

defineProps<{
  loading?: boolean;
  payload: ChatTimelineToolApprovalPayload;
}>();

const emit = defineEmits<{
  approve: [payload: ChatTimelineToolApprovalPayload];
  reject: [payload: ChatTimelineToolApprovalPayload];
}>();

function formatPayload(value: unknown) {
  if (value === null || value === undefined || value === '') {
    return '';
  }
  if (typeof value === 'string') {
    return value;
  }
  try {
    return JSON.stringify(value, null, 2);
  } catch {
    return String(value);
  }
}
</script>

<template>
  <section class="chat-tool-approval">
    <div class="chat-tool-approval__header">
      <span class="chat-tool-approval__icon" aria-hidden="true"></span>
      <div class="chat-tool-approval__title-wrap">
        <div class="chat-tool-approval__title">
          {{ payload.toolDisplayName || payload.toolName }}
        </div>
        <div class="chat-tool-approval__desc">需要确认后执行</div>
      </div>
    </div>
    <pre v-if="payload.input" class="chat-tool-approval__payload">{{
      formatPayload(payload.input)
    }}</pre>
    <div class="chat-tool-approval__actions">
      <button
        type="button"
        class="chat-tool-approval__button"
        :disabled="loading"
        @click="emit('reject', payload)"
      >
        拒绝
      </button>
      <button
        type="button"
        class="chat-tool-approval__button is-primary"
        :disabled="loading"
        @click="emit('approve', payload)"
      >
        批准
      </button>
    </div>
  </section>
</template>

<style scoped>
.chat-tool-approval {
  max-width: min(78%, 680px);
  padding: 12px;
  background: var(--el-color-primary-light-9);
  border: 1px solid var(--el-color-primary-light-7);
  border-radius: 8px;
}

.chat-tool-approval__header {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.chat-tool-approval__icon {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
  margin-top: 2px;
  background: var(--el-color-primary);
  border-radius: 4px;
}

.chat-tool-approval__title-wrap {
  min-width: 0;
}

.chat-tool-approval__title {
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  white-space: nowrap;
}

.chat-tool-approval__desc {
  margin-top: 2px;
  font-size: 12px;
  color: var(--el-text-color-secondary);
}

.chat-tool-approval__payload {
  max-height: 160px;
  padding: 8px;
  margin: 10px 0 0;
  overflow: auto;
  font-size: 12px;
  line-height: 18px;
  color: var(--el-text-color-secondary);
  white-space: pre-wrap;
  background: var(--el-bg-color);
  border-radius: 6px;
}

.chat-tool-approval__actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 12px;
}

.chat-tool-approval__button {
  min-width: 64px;
  height: 32px;
  padding: 0 12px;
  font-size: 13px;
  color: var(--el-text-color-primary);
  cursor: pointer;
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color);
  border-radius: 6px;
}

.chat-tool-approval__button:hover {
  border-color: var(--el-color-primary-light-5);
}

.chat-tool-approval__button:focus-visible {
  outline: 2px solid var(--el-color-primary-light-5);
  outline-offset: 2px;
}

.chat-tool-approval__button:disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.chat-tool-approval__button.is-primary {
  color: var(--el-color-white);
  background: var(--el-color-primary);
  border-color: var(--el-color-primary);
}
</style>
