<template>
  <v-arrow
    :config="{
      ...data,
      zIndex: undefined,
      stroke: data.fill,
      pointerWidth: data.pointerLength,
      hitStrokeWidth: 20,
      closed: true,
      points: flatPositions(data.points),
      ...eConfig,
      opacity: addMode ? 0.3 : data.opacity,
    }"
    ref="shape"
  />
</template>

<script lang="ts" setup>
import { ArrowData, defaultStyle, PointerPosition } from "./index.ts";
import { DC } from "@/deconstruction.js";
import { computed, ref } from "vue";
import { flatPositions } from "@/utils/shared.ts";
import { Arrow } from "konva/lib/shapes/Arrow";

const props = defineProps<{ data: ArrowData; addMode?: boolean }>();
const shape = ref<DC<Arrow>>();

const data = computed(() => ({ ...defaultStyle, ...props.data }));

const eConfig = computed(() => {
  const position =
    "pointerPosition" in data.value
      ? data.value.pointerPosition!
      : defaultStyle.pointerPosition;
  const eStart = [PointerPosition.all, PointerPosition.start].includes(position);
  const eEnd = [PointerPosition.all, PointerPosition.end].includes(position);

  return {
    pointerAtBeginning: eStart,
    pointerAtEnding: eEnd,
  };
});

defineExpose({
  get shape() {
    return shape.value;
  },
});
</script>
