<template>
  <a
    v-if="type === 'external'"
    :href="pathExternal"
    :target="target"
    :class="['default', look, { disabled }]"
    :title="titleImg"
    v-bind="$attrs"
    @click="triggerButton"
  >
    <i v-if="icon" :class="[icon, 'icon']"></i>
    <img v-if="image" :src="img(image.url)" :alt="image.alt" class="logo" />
    <span v-if="text" class="text">{{ text }}</span>
    <slot name="content"></slot>
  </a>
  <router-link
    v-else
    :to="route"
    :target="target"
    :class="['default', look, { disabled }]"
    :title="text"
  >
    <i v-if="icon" :class="[icon, 'icon']"></i>
    <img v-if="image" :src="img(image.url)" :alt="image.alt" class="logo" />
    <span v-if="text" class="text">{{ text }}</span>
    <slot name="content"></slot>
  </router-link>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
import { computed } from 'vue'
const emit = defineEmits<{ (e: 'trigger'): void }>()
import type { PropType } from 'vue'
import type { RouteLocationRaw } from 'vue-router'
import { img } from '../utils/image'

const { t } = useI18n()
interface ImageInterface {
  url: string
  alt: string
}

const props = defineProps({
  text: { type: String, default: null },
  route: { type: Object as PropType<RouteLocationRaw>, default: {} },
  pathExternal: { type: String, default: '' },
  look: { type: String, default: '' },
  type: { type: String, default: null },
  alt: { type: String, default: '' },
  icon: { type: String, default: null },
  target: { type: String, default: null },
  image: { type: Object as PropType<ImageInterface>, default: null },
  disabled: { type: Boolean, default: false }
})

const titleImg = computed<string>(() =>
  props.disabled ? t('general.header.about_text') : ''
)
function triggerButton() {
  if (props.disabled) return
  emit('trigger')
}
</script>

<style lang="scss" scoped>
.default {
  @include text(m-r-regular);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--black);
  text-decoration: none;
  height: inherit;
  width: inherit;
  .icon {
    margin-right: toRem(1);
  }
  &:hover {
    opacity: 0.8;
  }
}
.button {
  height: toRem(4);
  border-radius: toRem(1);
  padding: toRem(1.5);
  background-color: var(--black);
  color: var(--white);
  text-align: center;
  .icon {
    margin-right: 0;
  }
  &.disabled {
    background-color: var(--grey);
    opacity: 0.5;
  }
}
.icon {
  color: var(--black);
  font-size: toRem(2.4);
  margin-right: toRem(1);
}
.logo {
  height: inherit;
  border-radius: toRem(0.5);
  margin-right: toRem(1);
}
.text {
  width: 100%;
  white-space: break-spaces;
}
.link:hover {
  background-color: transparent;
  text-decoration: underline;
}
.link--grey {
  color: var(--grey-semi-dark);
  text-decoration: underline;
  font-weight: inherit;
}
.link--blue-dark {
  color: var(--blue-dark);
  .icon {
    color: var(--blue-dark);
  }
}
.link--blue {
  color: var(--blue);
  .icon {
    color: var(--blue);
  }
}
.button--white {
  background-color: var(--white);
  color: var(--black);
  border: toRem(0.1) solid var(--black);
  .icon {
    font-size: toRem(1.6);
  }
  &:hover {
    background-color: var(--black);
    color: var(--white);
    .icon {
      color: var(--white);
    }
  }
}
.button--white-blue {
  background-color: var(--white);
  border: toRem(0.1) solid var(--blue);
  color: var(--blue);
  .icon {
    font-size: toRem(1.4);
    color: var(--blue);
  }
}
.button--blue {
  background-color: var(--blue);
  border: toRem(0.1) solid var(--blue);
  .icon {
    font-size: toRem(1.4);
    color: var(--white);
  }
}
.button--blue-bleu {
  background-color: var(--blue-semi);
  color: var(--blue);
}
.disabled {
  color: grey;
  cursor: not-allowed;
  .icon {
    color: grey;
    cursor: not-allowed;
  }
  &:hover {
    text-decoration: none;
  }
}
.button--rounded {
  background-color: var(--black);
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  padding: 0;
  height: toRem(4);
  width: toRem(4);
  .icon {
    color: var(--white);
    font-size: toRem(2.8);
  }
}
.no-text {
  height: toRem(3);
  width: toRem(3);
  padding: 0;
  .icon {
    margin: auto;
  }
}
@media (max-width: toRem(50)) {
  .icon {
    margin-right: toRem(0.5);
  }
  .button {
    padding-right: toRem(1);
    padding-left: toRem(1);
  }
  .disable-mobile {
    pointer-events: none;
  }
}
</style>
