<script setup lang="ts">
import { Layers3 } from "@lucide/vue";
import { computed } from "vue";

import type { UserEntitlementSummary } from "../types/api";

const props = defineProps<{
  used: number;
  limit: number;
  entitlement?: UserEntitlementSummary | null;
  finishing?: boolean;
}>();

defineEmits<{
  finishTopic: [];
  openProfile: [];
}>();

const hasEntitlement = computed(() => Boolean(props.entitlement));
const displayUsed = computed(() => props.entitlement?.monthlyTopicUsed ?? props.used);
const displayLimit = computed(() => props.entitlement?.monthlyTopicLimit ?? props.limit);
const exhausted = computed(() => displayLimit.value !== null && displayLimit.value > 0 && displayUsed.value >= displayLimit.value);
const title = computed(() => props.entitlement ? "本月主题额度" : "当前会话额度");
const limitText = computed(() => displayLimit.value === null ? "不限" : String(displayLimit.value));
</script>

<template>
  <section class="session-quota" :class="{ exhausted }" :aria-label="title">
    <div>
      <Layers3 :size="18" aria-hidden="true" />
      <span>{{ title }}</span>
      <small v-if="hasEntitlement">{{ entitlement?.name }}</small>
    </div>
    <div class="session-quota-actions">
      <button
        v-if="entitlement?.enableGrowthProfile"
        type="button"
        class="quota-link"
        @click="$emit('openProfile')"
      >
        我的档案
      </button>
      <button type="button" class="quota-link" :disabled="finishing" @click="$emit('finishTopic')">
        {{ finishing ? '沉淀中' : '沉淀本主题' }}
      </button>
      <strong>{{ displayUsed }}/{{ limitText }}</strong>
    </div>
  </section>
</template>
