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

const props = defineProps({
  title: String,
  noMarginTop: Boolean,
  extraBesideText: {
    type: Boolean,
    default: false,
  },
  extraWidthPercent: {
    type: Number,
    default: 30,
  },
  titleInSplitRight: {
    type: Boolean,
    default: false,
  },
});

const normalizedExtraWidth = computed(() => {
  const width = Number.isFinite(props.extraWidthPercent) ? props.extraWidthPercent : 30;

  return Math.min(100, Math.max(0, width));
});

const textWidthPercent = computed(() => 100 - normalizedExtraWidth.value);

const extraWidthStyle = computed(() => ({
  flexBasis: `${normalizedExtraWidth.value}%`,
  maxWidth: `${normalizedExtraWidth.value}%`,
}));

const textWidthStyle = computed(() => ({
  flexBasis: `${textWidthPercent.value}%`,
  maxWidth: `${textWidthPercent.value}%`,
}));
</script>

<template>
  <div :class="{ 'mt-6': !noMarginTop }">
    <template v-if="props.extraBesideText">
      <h3 v-if="!props.titleInSplitRight" class="text-h4 font-weight-bold">
        {{ props.title }}
      </h3>
      <div class="entry-content entry-content--split">
        <h3
          v-if="props.titleInSplitRight"
          class="text-h4 font-weight-bold entry-content__title--mobile"
        >
          {{ props.title }}
        </h3>
        <div class="entry-content__extra" :style="extraWidthStyle">
          <slot name="extra" />
        </div>
        <div class="entry-content__text" :style="textWidthStyle">
          <h3
            v-if="props.titleInSplitRight"
            class="text-h4 font-weight-bold entry-content__title--desktop"
          >
            {{ props.title }}
          </h3>
          <p class="text-subtitle-1">
            <slot name="text" />
          </p>
        </div>
      </div>
    </template>
    <template v-else>
      <h3 class="text-h4 font-weight-bold">{{ props.title }}</h3>
      <p class="text-subtitle-1">
        <slot name="text" />
      </p>
      <slot name="extra" />
    </template>
  </div>
</template>

<style scoped>
.entry-content {
  display: flex;
  gap: 1.5rem;
  align-items: flex-start;
}

.entry-content--split {
  align-items: center;
}

.entry-content__extra,
.entry-content__text {
  min-width: 0;
}

.entry-content__extra {
  flex: 0 0 auto;
}

.entry-content__text {
  flex: 1 1 auto;
}

/* Desktop/Mobile title visibility helpers */
.entry-content__title--mobile {
  display: none;
}
.entry-content__title--desktop {
  display: block;
}

@media (max-width: 600px) {
  .entry-content--split {
    flex-direction: column;
    align-items: stretch;
  }

  .entry-content__extra,
  .entry-content__text {
    flex-basis: auto !important;
    max-width: 100% !important;
    width: 100%;
  }

  .entry-content__title--mobile {
    display: block;
  }

  .entry-content__title--desktop {
    display: none;
  }
}
</style>
