<template>
  <div
    class="table-loading"
    :class="[variant, tone]"
    role="status"
    :aria-label="ariaLabel"
    aria-live="polite"
  >
    <span class="table-loading__spinner" aria-hidden="true">
      <i :class="icon"></i>
    </span>

    <div v-if="hasCopy" class="table-loading__copy">
      <strong v-if="title">{{ title }}</strong>
      <p v-if="message">{{ message }}</p>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  variant: {
    type: String,
    default: 'panel',
    validator: (value) => ['panel', 'detail', 'overlay', 'drawer', 'banner'].includes(value)
  },
  tone: {
    type: String,
    default: 'theme',
    validator: (value) => ['theme', 'sky', 'success'].includes(value)
  },
  title: { type: String, default: '' },
  message: { type: String, default: '' },
  icon: { type: String, default: 'mdi mdi-loading' },
  showSkeleton: { type: Boolean, default: true },
  skeletonRows: { type: Number, default: 5 }
})

const hasCopy = computed(() => Boolean(props.title || props.message))

const ariaLabel = computed(() => [props.title, props.message].filter(Boolean).join(', ') || 'Loading')
</script>

<style scoped>
.table-loading {
  --accent: var(--theme-primary);
  --accent-deep: var(--theme-primary-active);
  width: 100%;
  color: #64748b;
}

.table-loading.theme,
.table-loading.sky {
  --accent: var(--theme-primary);
  --accent-deep: var(--theme-primary-active);
}

.table-loading.success {
  --accent: var(--success);
  --accent-deep: var(--success-hover);
}

.table-loading.panel {
  min-height: 220px;
  display: grid;
  place-items: center;
  gap: 12px;
  padding: 28px 24px;
  text-align: center;
}

.table-loading.detail {
  min-height: 180px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 22px 24px;
  text-align: left;
}

.table-loading.overlay,
.table-loading.drawer {
  display: grid;
  place-items: center;
  gap: 12px;
  text-align: center;
}

.table-loading.overlay {
  min-height: 0;
}

.table-loading.drawer {
  min-height: 160px;
}

.table-loading.banner {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 0;
  padding: 0;
  color: #255b7d;
}

.table-loading__spinner {
  width: 38px;
  height: 38px;
  display: inline-grid;
  place-items: center;
  border: 3px solid #e2e8f0;
  border-top-color: var(--accent);
  border-radius: 50%;
  color: var(--accent-deep);
  animation: table-spinner-rotate 0.8s linear infinite !important;
}

.table-loading.detail .table-loading__spinner {
  width: 34px;
  height: 34px;
}

.table-loading.banner .table-loading__spinner {
  width: 18px;
  height: 18px;
  border-width: 2px;
}

.table-loading__spinner i {
  display: none;
}

.table-loading__copy {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.table-loading.panel .table-loading__copy,
.table-loading.overlay .table-loading__copy,
.table-loading.drawer .table-loading__copy {
  max-width: 360px;
}

.table-loading.detail .table-loading__copy {
  flex: 1;
}

.table-loading.banner .table-loading__copy {
  display: inline;
}

.table-loading__copy strong {
  color: #0f172a;
  font-size: 14px;
  font-weight: 850;
  line-height: 1.4;
}

.table-loading__copy p {
  margin: 0;
  font-size: 13px;
  line-height: 1.65;
}

.table-loading.banner .table-loading__copy strong {
  display: none;
}

.table-loading.banner .table-loading__copy p {
  font-size: 12px;
  font-weight: 700;
}

@keyframes table-spinner-rotate {
  to {
    transform: rotate(360deg);
  }
}
</style>
