import { getUniqueString as m } from "../../common/utils/index.js";
import { createApp as f, h as u, getCurrentInstance as v } from "vue";
import T from "deep-equal";
import n from "../tooltip/tooltip.js";
import { TOOLTIP_DIRECTIONS as y } from "../tooltip/tooltip-constants.js";
const I = {
  name: "dt-tooltip-directive",
  install(c) {
    let a;
    const r = document.createElement("div");
    document.body.appendChild(r);
    const s = "top", d = f({
      name: "DtTooltipDirectiveApp",
      components: { DtTooltip: n },
      data() {
        return {
          tooltips: []
        };
      },
      created() {
        a = v();
      },
      methods: {
        addOrUpdateTooltip(e, t) {
          const i = this.tooltips.findIndex((o) => o.id === e);
          i !== -1 ? this.tooltips.splice(i, 1, { id: e, ...t }) : this.tooltips.push({ id: e, ...t });
        },
        removeTooltip(e) {
          this.tooltips = this.tooltips.filter((t) => t.id !== e);
        }
      },
      render() {
        return u(
          "div",
          this.tooltips.map(({ id: e, ...t }) => u(n, {
            key: e,
            ...t,
            sticky: t.sticky !== void 0 ? t.sticky : !0,
            /**
             * Set the delay to false when running tests only.
             */
            delay: t.delay !== void 0 ? t.delay : process.env.NODE_ENV !== "test",
            externalAnchor: `[data-dt-tooltip-id="${e}"]`
          }))
        );
      }
    });
    d.mount(r), c.directive("dt-tooltip", {
      beforeMount(e, t) {
        p(e, t);
      },
      updated(e, t) {
        T(t.value, t.oldValue) || p(e, t);
      },
      unmounted(e) {
        a.ctx.removeTooltip(e.getAttribute("data-dt-tooltip-id"));
      }
    });
    function p(e, t) {
      if (t.value === null || t.value === void 0) {
        const l = e.getAttribute("data-dt-tooltip-id");
        l && d.removeTooltip(l);
        return;
      }
      const i = e.getAttribute("data-dt-tooltip-id") || m();
      let o;
      if (typeof t.value == "string")
        o = {
          message: t.value,
          placement: t.arg || s
        };
      else if (typeof t.value == "object" && t.value !== null)
        o = {
          placement: t.arg || t.value.placement || s,
          ...t.value
        };
      else {
        console.error("DtTooltipDirective: binding value must be string, object, null or undefined");
        return;
      }
      Object.keys(t.modifiers).forEach((l) => {
        switch (l) {
          case "inverted":
            o.inverted = !0;
            break;
          case "no-delay":
            o.delay = !1;
            break;
          case "no-transition":
            o.transition = !1;
            break;
          default:
            y.includes(l) && (o.placement = l);
            break;
        }
      }), e.setAttribute("data-dt-tooltip-id", i), a.ctx.addOrUpdateTooltip(i, o);
    }
  }
};
export {
  I as DtTooltipDirective,
  I as default
};
//# sourceMappingURL=tooltip.js.map
