@use "sass:color";
@use "functions";
@use "mixins/border-radius";
@use "mixins/box-shadow";
@use "mixins/reset-text";
@use "variables";
@use "vendor/rfs";

.popover {
  position: absolute;
  top: 0;
  left: 0;
  z-index: variables.$zindex-popover;
  display: block;
  max-width: variables.$popover-max-width;
  // Our parent element can be arbitrary since tooltips are by default inserted as a sibling of their target element.
  // So reset our font and text properties to avoid inheriting weird values.
  @include reset-text.reset-text();
  @include rfs.font-size(variables.$popover-font-size);
  // Allow breaking very long words so they don't overflow the popover's bounds
  word-wrap: break-word;
  background-color: variables.$popover-bg;
  background-clip: padding-box;
  border: variables.$popover-border-width solid variables.$popover-border-color;
  @include border-radius.border-radius(variables.$popover-border-radius);
  box-shadow: variables.$popover-box-shadow;
  padding: variables.$popover-header-padding-y variables.$popover-header-padding-x;

  .arrow {
    position: absolute;
    display: block;
    width: variables.$popover-arrow-width;
    height: variables.$popover-arrow-height;
    margin: 0 (variables.$popover-border-radius * 1.2); // 1.2 for centering

    &::before,
    &::after {
      position: absolute;
      display: block;
      content: "";
      border-color: transparent;
      border-style: solid;
    }
  }
}

.bs-popover-top {
  margin-bottom: variables.$popover-arrow-height;

  > .arrow {
    bottom: functions.subtract(-(variables.$popover-arrow-height), variables.$popover-border-width);
    clip-path: polygon(0% 0%, 100% 0%, 100% 95%, 0% 95%);

    &::before {
      bottom: 0;
      border-width: variables.$popover-arrow-height (variables.$popover-arrow-width * .5) 0;
      border-top-color: variables.$popover-arrow-outer-color;
    }

    &::after {
      bottom: variables.$popover-border-width;
      border-width: variables.$popover-arrow-height (variables.$popover-arrow-width * .5) 0;
      border-top-color: variables.$popover-arrow-color;
    }
  }
}

.bs-popover-right {
  margin-left: variables.$popover-arrow-height;

  > .arrow {
    left: functions.subtract(-(variables.$popover-arrow-height), variables.$popover-border-width);
    width: variables.$popover-arrow-height;
    height: variables.$popover-arrow-width;
    margin: (variables.$popover-border-radius * .8) 0 0 0; // make sure the arrow does not touch the popover's rounded corners
    clip-path: polygon(5% 20%, 100% 0%, 100% 100%, 5% 100%);

    &::before {
      left: 0;
      border-width: (variables.$popover-arrow-width * .5) variables.$popover-arrow-height (variables.$popover-arrow-width * .5) 0;
      border-right-color: variables.$popover-arrow-outer-color;
    }

    &::after {
      left: variables.$popover-border-width;
      border-width: (variables.$popover-arrow-width * .5) variables.$popover-arrow-height (variables.$popover-arrow-width * .5) 0;
      border-right-color: variables.$popover-arrow-color;
    }
  }
}

.bs-popover-bottom {
  margin-top: variables.$popover-arrow-height;

  > .arrow {
    top: functions.subtract(-(variables.$popover-arrow-height), variables.$popover-border-width);
    clip-path: polygon(0% 100%, 0% 5%, 100% 0%, 100% 100%);

    &::before {
      top: 0;
      border-width: 0 (variables.$popover-arrow-width * .5) variables.$popover-arrow-height (variables.$popover-arrow-width * .5);
      border-bottom-color: variables.$popover-arrow-outer-color;
    }

    &::after {
      top: variables.$popover-border-width;
      border-width: 0 (variables.$popover-arrow-width * .5) variables.$popover-arrow-height (variables.$popover-arrow-width * .5);
      border-bottom-color: variables.$popover-arrow-color;
    }
  }

  // This will remove the popover-header's border just below the arrow
  .popover-header::before {
    position: absolute;
    top: 0;
    left: 50%;
    display: block;
    width: variables.$popover-arrow-width;
    margin-left: -(variables.$popover-arrow-width) * .5;
    content: "";
    border-bottom: variables.$popover-border-width solid variables.$popover-header-bg;
  }
}

.bs-popover-left {
  margin-right: variables.$popover-arrow-height;

  > .arrow {
    right: functions.subtract(-(variables.$popover-arrow-height), variables.$popover-border-width);
    width: variables.$popover-arrow-height;
    height: variables.$popover-arrow-width;
    margin: (variables.$popover-border-radius * .8) 0 0 0; // make sure the arrow does not touch the popover's rounded corners
    clip-path: polygon(0% 0%, 95% 0%, 95% 100%, 0% 100%);

    &::before {
      right: 0;
      border-width: (variables.$popover-arrow-width * .5) 0 (variables.$popover-arrow-width * .5) variables.$popover-arrow-height;
      border-left-color: variables.$popover-arrow-outer-color;
    }

    &::after {
      right: variables.$popover-border-width;
      border-width: (variables.$popover-arrow-width * .5) 0 (variables.$popover-arrow-width * .5) variables.$popover-arrow-height;
      border-left-color: variables.$popover-arrow-color;
    }
  }
}

.bs-popover-auto {
  &[x-placement^="top"] {
    @extend .bs-popover-top;
  }
  &[x-placement^="right"] {
    @extend .bs-popover-right;
  }
  &[x-placement^="bottom"] {
    @extend .bs-popover-bottom;
  }
  &[x-placement^="left"] {
    @extend .bs-popover-left;
  }
}


// Offset the popover to account for the popover arrow
.popover-header {
  margin-bottom: .25rem;
  @include rfs.font-size(variables.$font-size-base);
  color: variables.$popover-header-color;
  background-color: variables.$popover-header-bg;
  font-weight: variables.$popover-header-font-weight;
  border-bottom: variables.$popover-border-width solid darken(variables.$popover-header-bg, 5%);
  @include border-radius.border-top-radius(variables.$popover-inner-border-radius);

  &:empty {
    display: none;
  }
}

.popover-header + .popover-body {
  padding-right: 28px; // 8px + 20px (X icon size)
}

.popover-body {
  color: variables.$popover-body-color;
}
