<template>
  <div v-if="piniaData" class="pinia-store-viewer">
    <div class="store-content">
      <pre class="json-display">{{ formattedPiniaData }}</pre>
    </div>
  </div>
  <div v-else class="no-data">
    <span class="space-around">No frontend data available</span>
  </div>
</template>

<script>


import logger from "@/logger.js";

export default {
  name: "ErrorModalPiniaStore",
  props: {
    error: {
      type: Object,
      required: true,
    }
  },
  computed: {
    piniaData() {
      if (!this.error?.pinia) return null;

      if (typeof this.error.pinia === 'object') {
        return this.error.pinia;
      }

      if (typeof this.error.pinia === 'string') {
        try {
          return JSON.parse(this.error.pinia);
        } catch (e) {
          logger.error('Error parsing error.pinia:', e);
          return { raw: this.error.pinia };
        }
      }

      return null;
    },
    formattedPiniaData() {
      if (!this.piniaData) return '';
      return JSON.stringify(this.piniaData, null, 2);
    }
  }
}
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Cal+Sans&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap');

.pinia-store-viewer {
  margin-top: 1rem;
  height: 100%;
  overflow-y: auto;
}

.section-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: #374151;
}

.store-content {
  height: 100%
}

.json-display {
  margin: 0;
  font-family: 'Roboto mono', monospace;
  font-size: 1.2rem;
  flex: 1;
  padding: 0 1.6rem;
  color: #002F54;
  white-space: pre-wrap;
  word-break: break-word;
  height: 100%
}

.no-data {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.6rem;
}

.space-around {
  margin: 3rem;
}
</style>