<template>
  <v-tooltip location="bottom" open-delay="1000">
    <template #activator="{ props }">
      <v-btn
        v-bind="mergeProps($attrs, props)"
        data-testid="visibility"
        :icon="true"
        size="24"
        variant="text"
        @click="toggle"
      >
        <v-icon :icon="icon" size="16" />
      </v-btn>
    </template>
    <span>{{ t('labels.toggleVisibility') }}</span>
  </v-tooltip>
</template>

<script setup lang="ts">
import { computed, mergeProps } from 'vue';
import { useViewer3cr } from '@/composables/useViewer3cr';
import { useI18n } from 'vue-i18n';
import { DataOverlayMeasurement } from '@3cr/viewer-types-ts';

interface Props {
  measurement: DataOverlayMeasurement;
}

const props = defineProps<Props>();

const { t } = useI18n();
const viewer3cr = useViewer3cr();

const visibility = computed(() => {
  const { Visibility2d, Visibility3d } = props.measurement;
  return !!Visibility2d && !!Visibility3d;
});

const icon = computed(() => {
  return visibility.value ? 'visibility' : 'visibility_off';
});

async function toggle(): Promise<void> {
  const message = {
    Version: '0.0.0',
    Id: props.measurement.Id,
    Visibility2d: !visibility.value,
    Visibility3d: !visibility.value
  };
  await viewer3cr.setMeasurementVisibility({ message: [message] });
  await viewer3cr.setMeasurementVisibility({ message: [message] });
}
</script>
