<script setup lang="ts">
import { Icon } from '@iconify/vue';
import { ref } from 'vue';
type Node = {
  [key: string]: any;
  selected?: boolean;
  children?: Node[];
};

type Props = {
  level?: number;
  keyTitle?: string;
  keySubtitle?: string;
  collapse?: boolean;
  expandable?: boolean;
  decoration?: {
    level?: number;
    bg?: string;
    fg?: string;
    icon?: string;
  }[];
};

const props = defineProps<Props>();
const nodes = defineModel<Node[]>('nodes', { required: true });
const open = ref<boolean>(!props.collapse);

const dec = props.decoration?.find((v) => v.level === (props.level || 0));
</script>

<template>
  <div class="tree-container">
    <div v-for="(node, i) in nodes" class="tree-item" :key="i">
      <slot
        v-if="$slots['item']"
        name="item"
        :data="{ node, onclick: () => (node.selected = !node.selected) }"
      />

      <template v-else>
        <div
          class="item__content row items-center no-wrap"
          @click="open = expandable ? !open : open"
        >
          <label class="flex items-center item__checkbox" @click.stop>
            <input type="checkbox" v-model="node.selected" />
          </label>

          <div
            class="item__icon flex items-center justify-center"
            :style="`background: ${dec?.bg}; color: ${dec?.fg}`"
          >
            <Icon v-if="dec && dec.icon" :icon="dec.icon" />
          </div>

          <div class="column">
            <span class="item__title">
              {{ node[keyTitle || 'title'] || 'No Title' }}
            </span>
            <span class="item__subtitle">
              {{ node[keySubtitle || 'subtitle'] || 'No Subtitle' }}
            </span>
          </div>
        </div>
      </template>
      <q-separator v-if="!level"></q-separator>

      <transition name="slide">
        <div
          class="q-pl-lg q-pt-sm"
          v-if="open && node.children && node.children.length > 0"
        >
          <TreeView
            class="item__children"
            v-if="node.children"
            v-model:nodes="node.children"
            :level="(level || 0) + 1"
            :expandable
            :decoration
          />
        </div>
      </transition>
    </div>
  </div>
</template>

<style lang="css">
.tree-container {
  display: flex;
  flex-direction: column;
  user-select: none;
  gap: 8px;

  & .tree-item {
    & .item__content {
      padding: 0.1rem 0.5rem;

      &:hover {
        background: hsla(0 0% 0% / 0.1);
        border-radius: var(--radius-2);
      }
    }

    & .item__checkbox {
      padding: 0.1rem 0.5rem;
      margin-right: 1rem;
    }

    & .item__icon {
      margin-right: 1rem;
      width: 1.5rem;
      height: 1.5rem;
      border-radius: 50%;
    }

    & .item__title {
      font-weight: 600;
    }

    & .item__subtitle {
      font-size: 80%;
      color: hsla(var(--text-mute-2));
    }
  }
}

.slide-enter-active {
  transition: all 0.1s ease-out;
}

.slide-leave-active {
  transition: all 0.1s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-enter-from,
.slide-leave-to {
  transform: translateY(-20px);
  opacity: 0;
}
</style>
