<template>
  <div
    @mouseenter="onMouseenter"
    @mouseleave="onMouseleave"
  >
    <button
      ref="trigger"
      class="button-unstyled popover-trigger-button"
      type="button"
      @click="onClick"
    >
      <slot name="trigger" />
    </button>
    <div
      v-if="!hidden"
      ref="content"
      :style="styles"
      class="popover"
      :class="popoverClass || 'popover-default'"
    >
      <slot
        name="content"
        class="popover-inner"
        :close="hidePopover"
      />
    </div>
  </div>
</template>

<script src="./popover.js" />

<style lang="scss">
@import '../../_variables.scss';

.popover-trigger-button {
  display: inline-block;
}

.popover {
  z-index: 500;
  position: absolute;
  min-inline-size: 0;
}

.popover-default {
  transition: opacity 0.3s;

  &:after {
    content: '';
    position: absolute;
    inset-block-start: 0;
    inset-block-end: 0;
    inset-inline-start: 0;
    inset-inline-end: 0;
    z-index: 3;
    box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.6);
    box-shadow: var(--panelShadow);
    pointer-events: none;
  }

  border-radius: $fallback--btnRadius;
  border-radius: var(--btnRadius, $fallback--btnRadius);

  background-color: $fallback--bg;
  background-color: var(--popover, $fallback--bg);
  color: $fallback--text;
  color: var(--popoverText, $fallback--text);
  --faint: var(--popoverFaintText, $fallback--faint);
  --faintLink: var(--popoverFaintLink, $fallback--faint);
  --lightText: var(--popoverLightText, $fallback--lightText);
  --postLink: var(--popoverPostLink, $fallback--link);
  --postFaintLink: var(--popoverPostFaintLink, $fallback--link);
  --icon: var(--popoverIcon, $fallback--icon);
}

.dropdown-menu {
  display: block;
  padding-block: .5rem;
  padding-inline: 0;
  font-size: 1em;
  text-align: start;
  list-style: none;
  max-inline-size: 100vi;
  z-index: 200;
  white-space: nowrap;

  .dropdown-divider {
    block-size: 0;
    margin-block: .5rem;
    margin-inline: 0;
    overflow: hidden;
    border-block-start: 1px solid $fallback--border;
    border-block-start: 1px solid var(--border, $fallback--border);
  }

  .dropdown-item {
    line-height: 21px;
    overflow: hidden;
    display: block;
    padding-block: 0.5em;
    padding-inline: 0.75em;
    clear: both;
    font-weight: 400;
    text-align: inherit;
    white-space: nowrap;
    border: none;
    border-radius: 0px;
    background-color: transparent;
    box-shadow: none;
    inline-size: 100%;
    block-size: 100%;
    box-sizing: border-box;

    --btnText: var(--popoverText, $fallback--text);

    &-icon {
      svg {
        inline-size: 22px;
        margin-inline-end: 0.75rem;
        color: var(--menuPopoverIcon, $fallback--icon)
      }
    }

    &:active, &:hover {
      background-color: $fallback--lightBg;
      background-color: var(--selectedMenuPopover, $fallback--lightBg);
      box-shadow: none;
      --btnText: var(--selectedMenuPopoverText, $fallback--link);
      --faint: var(--selectedMenuPopoverFaintText, $fallback--faint);
      --faintLink: var(--selectedMenuPopoverFaintLink, $fallback--faint);
      --lightText: var(--selectedMenuPopoverLightText, $fallback--lightText);
      --icon: var(--selectedMenuPopoverIcon, $fallback--icon);
      svg {
        color: var(--selectedMenuPopoverIcon, $fallback--icon);
        --icon: var(--selectedMenuPopoverIcon, $fallback--icon);
      }
    }

    .menu-checkbox {
      display: inline-block;
      vertical-align: middle;
      min-inline-size: 22px;
      max-inline-size: 22px;
      min-block-size: 22px;
      max-block-size: 22px;
      line-height: 22px;
      text-align: center;
      border-radius: 0px;
      background-color: $fallback--fg;
      background-color: var(--input, $fallback--fg);
      box-shadow: 0px 0px 2px black inset;
      box-shadow: var(--inputShadow);
      margin-inline-end: 0.75em;

      &.menu-checkbox-checked::after {
        font-size: 1.25em;
        content: '✓';
      }

      &.-radio {
        border-radius: 9999px;

        &.menu-checkbox-checked::after {
          font-size: 2em;
          content: '•';
        }
      }
    }

  }

  .button-default.dropdown-item {
    &,
    i[class*=icon-] {
      color: $fallback--text;
      color: var(--btnText, $fallback--text);
    }

    &:active {
      background-color: $fallback--lightBg;
      background-color: var(--selectedMenuPopover, $fallback--lightBg);
      color: $fallback--link;
      color: var(--selectedMenuPopoverText, $fallback--link);
    }

    &:disabled {
      color: $fallback--text;
      color: var(--btnDisabledText, $fallback--text);
    }

    &.toggled {
      color: $fallback--text;
      color: var(--btnToggledText, $fallback--text);
    }
  }
}
</style>
