<template>
  <div
    class="vm-split-layout"
    :class="{ 'has-toolbar': Boolean($slots.toolbar) }"
    :style="gridStyle"
  >
    <div v-if="$slots.toolbar" class="vm-split-layout__toolbar">
      <slot name="toolbar" />
    </div>
    <aside v-if="$slots.aside" class="vm-split-layout__aside">
      <slot name="aside" />
    </aside>
    <div class="vm-split-layout__main">
      <slot />
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

defineOptions({ name: 'vm-split-layout' })

const props = withDefaults(
  defineProps<{
    asideWidth?: string
  }>(),
  {
    asideWidth: '240px',
  },
)

const gridStyle = computed(() => ({
  '--vm-split-aside': props.asideWidth,
}))
</script>

<style lang="scss" scoped>
.vm-split-layout {
  display: grid;
  width: 100%;
  flex: 1;
  min-height: 0;
  align-self: stretch;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow-soft);
  grid-template-columns: var(--vm-split-aside, 240px) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}

.vm-split-layout.has-toolbar {
  grid-template-rows: auto minmax(0, 1fr);
}

.vm-split-layout__toolbar {
  grid-row: 1;
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid color-mix(in srgb, var(--foreground) 8%, transparent);
}

.vm-split-layout__aside {
  grid-row: 1;
  grid-column: 1;
  min-height: 0;
  overflow: hidden;
  border-right: 1px solid color-mix(in srgb, var(--foreground) 8%, transparent);

  :deep(.vm-aside) {
    border-right: none;
  }
}

.has-toolbar .vm-split-layout__aside {
  grid-row: 2;
}

.vm-split-layout__main {
  grid-row: 1;
  grid-column: 2;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.has-toolbar .vm-split-layout__main {
  grid-row: 2;
}

.vm-split-layout__main :deep(.vm-crud) {
  flex: 1;
  min-height: 0;
  height: 100%;
  border-radius: 0;
  box-shadow: none;
}
</style>
