<template>
  <div class="asset-tree">
    <button v-if="!nested" :class="['asset-tree-root', { active: activeFolderId === null }]" type="button" @click="$emit('select', null)">
      <PackageOpen :size="16" />
      <span>All assets</span>
      <small>{{ assetCount }}</small>
    </button>
    <div v-for="folder in childFolders" :key="folder.id" class="asset-tree-node">
      <button :class="['asset-tree-folder', { active: activeFolderId === folder.id }]" type="button" @click="$emit('select', folder.id)">
        <ChevronRight :size="13" :class="{ open: openIds.includes(folder.id) }" @click.stop="toggle(folder.id)" />
        <Folder :size="15" />
        <span>{{ folder.name }}</span>
        <small>{{ folderAssetCount(folder.id) }}</small>
      </button>
      <div class="asset-tree-actions">
        <button title="Add child folder" type="button" @click="$emit('create-child', folder)"><FolderPlus :size="13" /></button>
        <button title="Edit folder" type="button" @click="$emit('edit', folder)"><Pencil :size="13" /></button>
        <button title="Delete folder" type="button" @click="$emit('delete', folder)"><Trash2 :size="13" /></button>
      </div>
      <AssetTree
        v-if="openIds.includes(folder.id)"
        :folders="folders"
        :assets="assets"
        :parent-id="folder.id"
        :active-folder-id="activeFolderId"
        :open-ids="openIds"
        nested
        @select="$emit('select', $event)"
        @toggle="$emit('toggle', $event)"
        @create-child="$emit('create-child', $event)"
        @edit="$emit('edit', $event)"
        @delete="$emit('delete', $event)"
      />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { ChevronRight, Folder, FolderPlus, PackageOpen, Pencil, Trash2 } from '@lucide/vue';

defineOptions({ name: 'AssetTree' });

const props = defineProps({
  folders: { type: Array, default: () => [] },
  assets: { type: Array, default: () => [] },
  parentId: { type: String, default: null },
  activeFolderId: { type: String, default: null },
  openIds: { type: Array, default: () => [] },
  nested: { type: Boolean, default: false }
});

const emit = defineEmits(['select', 'toggle', 'create-child', 'edit', 'delete']);

const childFolders = computed(() => props.folders.filter((folder) => (folder.parentId || null) === props.parentId));
const assetCount = computed(() => props.assets.length);

function folderAssetCount(folderId) {
  return props.assets.filter((asset) => asset.folderId === folderId).length;
}

function toggle(folderId) {
  emit('toggle', folderId);
}
</script>

<style scoped>
.asset-tree {
  display: grid;
  gap: 6px;
}

.asset-tree .asset-tree {
  margin-left: 18px;
  padding-left: 10px;
  border-left: 1px solid rgba(255, 255, 255, 0.09);
}

.asset-tree-root,
.asset-tree-folder {
  width: 100%;
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  color: rgba(255, 255, 255, 0.78);
  background: transparent;
  text-align: left;
}

.asset-tree-root {
  grid-template-columns: auto minmax(0, 1fr) auto;
}

.asset-tree-root.active,
.asset-tree-folder.active {
  border-color: rgba(95, 219, 255, 0.34);
  background: linear-gradient(135deg, rgba(58, 178, 255, 0.2), rgba(143, 93, 255, 0.18));
  color: #f6fbff;
}

.asset-tree-folder span,
.asset-tree-root span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.asset-tree-folder small,
.asset-tree-root small {
  color: rgba(255, 255, 255, 0.45);
  font-size: 0.7rem;
}

.asset-tree-node {
  position: relative;
}

.asset-tree-actions {
  display: flex;
  gap: 3px;
  justify-content: flex-end;
  margin: -3px 4px 5px 30px;
  opacity: 0;
  transition: opacity 0.16s ease;
}

.asset-tree-node:hover > .asset-tree-actions,
.asset-tree-node:focus-within > .asset-tree-actions {
  opacity: 1;
}

.asset-tree-actions button {
  width: 25px;
  height: 25px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  color: rgba(255, 255, 255, 0.76);
  background: rgba(255, 255, 255, 0.05);
}

.open {
  transform: rotate(90deg);
}
</style>
