<template>
  <div v-if="display.timeline.length" class="tool-process">
    <el-button text size="small" @click="showProcess = !showProcess">
      {{ showProcess ? collapseLabel : `${expandLabel} ${display.timeline.length}` }}
    </el-button>
    <el-collapse-transition>
      <ol v-if="showProcess" class="tool-process__timeline">
        <li
          v-for="item in display.timeline"
          :key="item.key"
          class="tool-process__item"
          :class="[`is-${item.level || 'info'}`]"
        >
          <span class="tool-process__dot" />
          <span class="tool-process__main">
            <span class="tool-process__message">
              {{ item.label }}
              <small v-if="item.detail">{{ item.detail }}</small>
            </span>
            <span v-if="item.current !== undefined && item.total !== undefined" class="tool-process__count">
              {{ item.current }}/{{ item.total }}
            </span>
          </span>
        </li>
      </ol>
    </el-collapse-transition>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue';
import type { RuntimeProcessEventProjection } from '../types/index.d';
import { buildToolProcessDisplay } from '../utils/tool-process-display';

const props = defineProps<{
  events?: RuntimeProcessEventProjection[];
}>();

const showProcess = ref(false);
const events = computed(() => props.events || []);
const display = computed(() => buildToolProcessDisplay(events.value));
const collapseLabel = '\u6536\u8d77\u8fc7\u7a0b';
const expandLabel = '\u67e5\u770b\u8fc7\u7a0b';
</script>

<style lang="scss" scoped>
.tool-process {
  margin-top: 6px;
}

.tool-process__timeline {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.tool-process__item {
  display: grid;
  grid-template-columns: 10px 1fr;
  gap: 8px;
  align-items: start;
  color: var(--el-text-color-secondary);
  font-size: 12px;
  line-height: 1.45;
}

.tool-process__dot {
  width: 6px;
  height: 6px;
  margin-top: 6px;
  border-radius: 50%;
  background: var(--el-color-primary);
}

.tool-process__item.is-warning .tool-process__dot {
  background: var(--el-color-warning);
}

.tool-process__item.is-error .tool-process__dot {
  background: var(--el-color-danger);
}

.tool-process__main {
  min-width: 0;
  display: flex;
  gap: 6px;
  align-items: baseline;
}

.tool-process__message {
  min-width: 0;
  overflow-wrap: anywhere;
}

.tool-process__message small {
  display: block;
  margin-top: 1px;
  color: var(--el-text-color-placeholder);
}

.tool-process__count {
  flex: none;
  color: var(--el-text-color-placeholder);
}
</style>
