<script>import { createPopper } from "@popperjs/core";
import { onDestroy, onMount } from "svelte";
import popperFrames from "./popper-frames";
const vertical = /* @__PURE__ */ new Set(["top", "bottom"]);
export let target = null;
export let placement = "top";
export let html;
export let offset = 0;
$: offsetOptions = {
  name: "offset",
  options: {
    offset: [vertical.has(placement) ? 0 : offset, vertical.has(placement) ? offset : 0]
  }
};
let container = null;
let popper;
const preventOverflowOptions = {
  name: "preventOverflow",
  options: { padding: 4 }
};
function highlight(html2) {
  return html2.replace(/<highlight>/g, '<span class="highlight">').replace(/<\/highlight>/g, "</span>");
}
async function updatePlacement() {
  if (popper) popper.update();
}
function updateTooltip() {
  if (popper) {
    popper.destroy();
  }
  if (!container || !target) return;
  popper = createPopper(target, container, {
    placement,
    modifiers: [popperFrames, preventOverflowOptions, offsetOptions]
  });
}
onMount(() => {
  updatePlacement();
  updateTooltip();
  setTimeout(updateTooltip, 100);
});
onDestroy(() => {
  try {
    if (popper) {
      popper.destroy();
      popper = null;
    }
  } catch (err) {
  }
});
</script>

<div bind:this={container} class={`${placement} tooltip in`} role="tooltip">
  <div data-popper-arrow class="tooltip-arrow" />
  <div class="tooltip-inner">{@html highlight(html)}</div>
</div>

<style>
  .tooltip {
    position: absolute;
    z-index: 1070;
    display: block;
    font-family: var(--font-family-ui);
    font-style: normal;
    font-weight: normal;
    letter-spacing: normal;
    line-break: auto;
    line-height: normal;
    text-align: left;
    text-align: start;
    text-decoration: none;
    text-shadow: none;
    text-transform: none;
    white-space: normal;
    word-break: normal;
    word-spacing: normal;
    font-size: 0.875rem;
    word-wrap: break-word;
    opacity: 1;
    pointer-events: none;
    position: relative;
  }

  .tooltip.in {
    animation: animate-in linear 0.8s;
  }

  @keyframes animate-in {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  /* These selectors are not working */
  .tooltip.top,
  .tooltip[data-popper-placement^='top'] {
    padding-bottom: 5px;
    margin-bottom: -4px !important;
  }
  .tooltip-arrow {
    border-style: solid;
    border-color: transparent;
  }
  .tooltip.top .tooltip-arrow,
  .tooltip[data-popper-placement^='top'] .tooltip-arrow {
    width: 0.75rem;
    height: 0.75rem;
    bottom: -0.4rem;
    left: 50%;
    margin-left: 0px;
    border-width: 6px 6px 0;
    border-top-color: var(--tooltip-background);
  }
  .tooltip.right,
  .tooltip[data-popper-placement^='right'] {
    padding-left: 5px;
    /* margin-left: 9px; */
  }
  .tooltip.right .tooltip-arrow,
  .tooltip[data-popper-placement^='right'] .tooltip-arrow {
    left: -0.4rem;
    top: 50%;
    margin-top: 1px;
    width: 0.75rem;
    height: 0.75rem;
    border-width: 6px 6px 6px 0;
    border-right-color: var(--tooltip-background);
  }
  .tooltip.bottom,
  .tooltip[data-popper-placement^='bottom'] {
    padding-top: 5px;
    margin-top: 3px;
  }
  .tooltip.bottom .tooltip-arrow,
  .tooltip[data-popper-placement^='bottom'] .tooltip-arrow {
    top: 0;
    left: 50%;
    margin-left: -3px;
    border-width: 0 6px 6px;
    border-bottom-color: var(--tooltip-background);
  }
  .tooltip.left,
  .tooltip[data-popper-placement^='left'] {
    padding-right: 5px;
  }
  .tooltip.left .tooltip-arrow,
  .tooltip[data-popper-placement^='left'] .tooltip-arrow {
    right: -0.4rem;
    top: 50%;
    margin-top: 1px;
    width: 0.75rem;
    height: 0.75rem;
    border-width: 6px 0 6px 6px;
    border-left-color: var(--tooltip-background);
  }
  .tooltip-inner {
    max-width: 200px;
    padding: 6px 12px;
    color: var(--tooltip-text-color);
    text-align: center;
    background-color: var(--tooltip-background);
    border-radius: 0.25rem;
  }
  .tooltip-inner .highlight {
    font-weight: bold;
    color: var(--tooltip-highlight);
    text-shadow:
      0 0 2px rgba(0, 0, 0, 0.6),
      0 1px 2px rgba(0, 0, 0, 0.5);
  }
</style>
