<script setup lang="ts">
import { ref, computed } from 'vue'
export interface Props {
  items: InventoryItem[]
  shown: boolean
}

const props = defineProps<Props>();
const emit = defineEmits<{
  (event: 'selection', value: InventoryItem | null): void
}>()

// inventory size is 15
const slots = Array(15)
const selectedIndex = ref(0)

const inventory = computed(() => {
  const inventory = [...props.items, ...slots]
  inventory.length = slots.length
  return inventory
})

function setSelection(i: number) {
  selectedIndex.value = i
  emit('selection', inventory.value[i])
}
</script>

<template>
  <section id="inventory" class="screen" :class="{ shown }">
    <header>
      <h1>Inventory</h1>
    </header>

    <ol>
      <template v-for="(item,i) in inventory">
        <li v-if="!item" class="empty">
          <i>(empty)</i>
        </li>
        <li v-else
          :class="['item', `${item.type}-${item.icon}-${item.quality}`, {
            selected: selectedIndex === i
          }]"
          @click="setSelection(i)"
        >
          <b>
            {{ item.name }}
            <template v-if="item.amount > 1">
              (×{{ item.amount }})
            </template>
          </b>
        </li>
      </template>
    </ol>
  </section>

</template>

<style scoped>
#inventory {
  width: 346px;
  transition: transform .3s ease-out;
  transform: translate(-50vw);
}
#inventory.shown {
  transform: translate(0px);
}
ol {
  list-style: none;
  display: flex;
  flex-flow: row wrap;
  gap: 1em;
  margin: 0;
  padding: 0;
}
li {
  position: relative;
  display: inline-block;
  width: 100px;
  height: 100px;
  text-align: center;
  border: 2px solid white;
  border-radius: 6px;
  background: transparent center no-repeat;
  background-size: contain;
  cursor: pointer;
}
li:not(.empty):hover, li.selected {
  background-color: #FFCA;
}
li.empty {
  cursor: not-allowed;
}
li > i, li > b {
  position: absolute;
  bottom: 0;
  right: 1px;
  padding: .25em .5em;
  background: #0008;
  font-size: .8em;
}
</style>
