<script setup lang="ts">
import { Icon } from '@iconify/vue';

type Node = {
  [key: string]: any;
  opened?: boolean;
  selected?: boolean;
  children?: Node[];
};

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

const props = defineProps<Props>();
const nodes = defineModel<Node[]>('nodes', { required: true });

const emits = defineEmits<{ (e: 'checked'): void }>();

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

function recursiveDeselect(node: Node) {
  if (node.children) {
    node.children.forEach((v) => {
      v.selected = false;
      recursiveDeselect(v);
    });
  }
}
function toggleCheck(node: Node) {
  node.selected = !node.selected;
  if (node.selected === false) recursiveDeselect(node);
  if (node.selected === true) emits('checked');
}

function toggleExpand(node: Node) {
  node.opened = !node.opened;
}
</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, toggleExpand, toggleCheck }"
      />

      <template v-else>
        <div
          class="item__content row items-center no-wrap"
          @click="toggleExpand(node)"
        >
          <label class="flex items-center item__checkbox" @click.stop>
            <input
              type="checkbox"
              v-model="node.selected"
              @click="toggleCheck(node)"
            />
          </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="node.opened && node.children && node.children.length > 0"
        >
          <TreeView
            class="item__children"
            v-if="node.children"
            v-model:nodes="node.children"
            @checked="
              () => {
                node.selected = true;
                $emit('checked');
              }
            "
            :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>
