<template>
  <foreignObject
    :x="Math.round(aspect_ratio * rectangle.left)"
    :y="rectangle.top"
    :width="Math.round(aspect_ratio * rectangle.width)"
    :height="rectangle.height"
    :style="`transform: scaleX(${1 / aspect_ratio})`"
  >
    <div
      xmlns="http://www.w3.org/1999/xhtml"
      :class="`px-2 is-flex is-align-items-center is-justify-content-center is-size-2 has-text-weight-bold ${variant} ${
        visible ? 'visible' : ''
      }`"
      style="height: inherit"
      v-bind="$attrs"
    >
      <slot name="default" />
    </div>
  </foreignObject>
</template>

<script setup lang="ts">
import { type VueLike, unwrap_loading } from "@/lib/helpers";
import { Rectangle } from "@/lib/rects/rectangle";
import { advent22Store } from "@/lib/store";
import { computed } from "vue";

const store = advent22Store();

type BulmaVariant =
  | "primary"
  | "link"
  | "info"
  | "success"
  | "warning"
  | "danger";

withDefaults(
  defineProps<{
    variant: BulmaVariant;
    visible?: boolean;
    rectangle: VueLike<Rectangle>;
  }>(),
  {
    visible: true,
  },
);

const aspect_ratio = computed(() => {
  try {
    return unwrap_loading(store.background_image).aspect_ratio;
  } catch {
    return 1;
  }
});
</script>

<style lang="scss" scoped>
@use "@/bulma-scheme" as scheme;

foreignObject > div {
  &:not(.visible, :hover):deep() > * {
    display: none;
  }

  &.visible,
  &:hover {
    border-width: 2px;
    border-style: solid;

    @each $name, $color in scheme.$colors {
      &.#{$name} {
        background-color: rgba($color, 0.3);
        border-color: rgba($color, 0.9);
      }
    }
  }
}
</style>
